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, |
In reply to warnings pragma anomaly by Athanasius
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |