in reply to "Useless use of a constant" in grep block?
G'day perlancar,
I was unable to reproduce your warning ("Useless use of a constant") or, indeed, any warnings:
$ perl -wE 'my @ary = qw{a b}; push @ary, "foo" unless grep { "foo" } +@ary; say "@ary"' a b
I'm using 5.26.0. Thinking it might be version-related, I also tried the same line with 5.24.0, 5.22.0, 5.20.0, 5.18.0, and 5.14.0. None produced any warnings; all produced identical output: "a b".
So, either you're using some other version (please state which), or the code you posted is not the code that generated that warning.
The constant "foo" should be perfectly valid as a condition in the grep block (evaluating to TRUE). Similarly, a FALSE constant would be just as valid.
$ perl -wE 'my @ary = qw{a b}; push @ary, "foo" unless grep { "0" } @a +ry; say "@ary"' a b foo
Perl often provides helpful hints in its warning messages: "Missing semicolon ...", "Might be a runaway multi-line ...", and so on. If your message is from an old version of Perl, there's little you can do about that (beyond upgrading, of course). If your message is from different code to that shown, suggestions for improvements can be made when we know what that code is.
— Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: "Useless use of a constant" in grep block?
by dsheroh (Monsignor) on Jul 30, 2017 at 09:48 UTC | |
by kcott (Archbishop) on Aug 02, 2017 at 01:44 UTC |