Athanasius has asked for the wisdom of the Perl Monks concerning the following question:
It is completely unimportant. That is why it is so interesting.
— Hercule Poirot in The Murder of Roger Ackroyd by Agatha Christie
While researching Re^3: Perl assign scalar to array I noticed something strange. In this one-liner:
13:54 >perl -we "my $scalar = (2, 3, 4); print $scalar;" Useless use of a constant (2) in void context at -e line 1. Useless use of a constant (3) in void context at -e line 1. 4 13:54 >
the term (2, 3, 4) is evaluated in scalar context, so the comma operator (which is left-associative) twice “evaluates its left argument, throws that value away, then evaluates its right argument and returns that value.” The first two constants are thrown away, so the warning messages are exactly as expected.
But when certain constants are used, the warning messages for those constants disappear. I have found three such constants: 0, 1, and undef. For example:
14:00 >perl -we "my $scalar = (2, 1, 4); print $scalar;" Useless use of a constant (2) in void context at -e line 1. 4 14:00 >
Deparsing sheds no light, since the result is the same in all cases:
14:00 >perl -MO=Deparse -we "my $scalar = (2, 1, 4); print $scalar;" Useless use of a constant (2) in void context at -e line 1. BEGIN { $^W = 1; } my $scalar = ('???', '???', 4); print $scalar; -e syntax OK 14:01 >perl -MO=Deparse -we "my $scalar = (2, 3, 4); print $scalar;" Useless use of a constant (2) in void context at -e line 1. Useless use of a constant (3) in void context at -e line 1. BEGIN { $^W = 1; } my $scalar = ('???', '???', 4); print $scalar; -e syntax OK 14:01 >
I tested this on the earliest and latest Perls currently installed on my system: v5.14.4 and v5.22.0 (both Strawberry Perl running under Windows 8.1, 64-bit).
Any ideas as to why these three particular constants should be treated differently by the warnings pragma?
I tried looking at the code in warnings.pm, but, as far as I can see, the only effect of use warnings is to call warnings::import, which in turn sets ${^WARNING_BITS}. Presumably, this variable affects the behaviour of the perl executable? Does anyone know where in the source code the real functionality behind use warnings resides?
Cheers,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: warnings pragma anomaly
by BrowserUk (Patriarch) on Oct 17, 2015 at 04:32 UTC | |
|
Re: warnings pragma anomaly
by LanX (Saint) on Oct 17, 2015 at 08:48 UTC | |
|
Re: warnings pragma anomaly
by AppleFritter (Vicar) on Oct 17, 2015 at 09:37 UTC | |
|
Re: warnings pragma anomaly ( missing Useless use of %s in void context not issued)
by Anonymous Monk on Oct 17, 2015 at 09:18 UTC | |
|
Re: warnings pragma anomaly
by ikegami (Patriarch) on Oct 19, 2015 at 16:21 UTC | |
|
Re: warnings pragma anomaly
by muba (Priest) on Oct 19, 2015 at 17:23 UTC | |
by choroba (Cardinal) on Oct 19, 2015 at 17:40 UTC | |
by BrowserUk (Patriarch) on Oct 19, 2015 at 21:59 UTC | |
by choroba (Cardinal) on Oct 19, 2015 at 22:25 UTC | |
by muba (Priest) on Oct 19, 2015 at 23:22 UTC | |
| |
by BrowserUk (Patriarch) on Oct 19, 2015 at 22:40 UTC |