Here is my Perl script and its output:
use strict;
use warnings;
(undef, 1); # no output
(0, 1); # no output
(1, 1); # no output
(2, 1); # "Useless use of a constant in void context at C:\...\void.pl line 7"
(3, 1); # "Useless use of a constant in void context at C:\...\void.pl line 8"
("", 1); # "Useless use of a constant in void context at C:\...\void.pl line 9"
("0", 1); # "Useless use of a constant in void context at C:\...\void.pl line 10"
("1", 1); # "Useless use of a constant in void context at C:\...\void.pl line 11"
I would expect warnings at every line. What is special about undef
, 0
and 1
which causes this not to happen?
Documented in perldoc perldiag
complete with rationale:
This warning will not be issued for numerical constants equal to
0
or1
since they are often used in statements like1 while sub_with_side_effects();
As for undef
, it's a function that has uses even in void context. e.g. undef($x)
does something similar to —but different than— $x = undef();
. (You normally want the latter.) A warning could be issued for uses of undef
without args in void context, but it would require specialized code, and it's simply not needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With