develooper Front page | perl.perl5.porters | Postings from September 2016

Re: [perl #123665] 'Useless use of a constant in void context' iscompile-time but not syntax warning

Thread Previous
From:
Eirik Berg Hanssen
Date:
September 15, 2016 16:21
Subject:
Re: [perl #123665] 'Useless use of a constant in void context' iscompile-time but not syntax warning
Message ID:
CAHAeAG5=4jLmEwwFpfDfJvsLroOtAyQX46DDukeUedM-P=YECg@mail.gmail.com
On Thu, Sep 15, 2016 at 11:10 AM, Abigail <abigail@abigail.be> wrote:

> >     Why does use bigint; 1; warn in void context?
>

[snip]


> However, under "use bigint", what looks like a literal 1 to the reader
> no longer looks like a plain literal to perl. It's an overloaded
> Math::BigInt object. It's no longer a literal, and perl doesn't know
> whether fechting the value has any side effects or not. So, it won't
> warn.


  Um, that's just the thing – it does warn:

$ perl -we 'use bigint; 1'
Useless use of a constant (1) in void context at -e line 1.
$

  Compare:

$ perl -we '1;' -e '2;' -e 'use bigint; 1;' -e2
Useless use of a constant (2) in void context at -e line 2.
Useless use of a constant (1) in void context at -e line 3.
Useless use of a constant (2) in void context at -e line 4.
$

  The missing warning from the first literal is, as you said, the special
case for 0 and 1.

  The warning from the penultimate literal shows that the special case for
0 and 1 does not apply when the literals are overloaded.

  Moreover, the warning from either of the last two literals also shows
that perl indeed warns, even if might not "know whether fetching the value
has any side effect or not".

  I'm not saying perl is doing the wrong thing in general to warn for
overloaded literals in void context – I'll leave that for you guys, just
noting that it does – but as long as I'm writing, I'd say it would be
_nice_ if at least the special case (the lack of warning for 0 and 1 in
void context) could be retained also when literals are overloaded.  Not
that I'm expecting it to be easy though.


Eirik

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About