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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.