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
    So, either you're using some other version (please state which), or the code you posted is not the code that generated that warning.
    Or option 3: The point of the OP wasn't "why did I get this warning?", but, rather, "I made this mistake and didn't get a warning. Maybe it should issue one?"

      Firstly, my apologies for the late reply. I usually log in daily; this week I skipped a couple of days.

      I read the quote in the title ("Useless use of a constant") as the message received after making "a stupid mistake"; and the quote in the body ("Probably useless use of a constant ...") as the preferred message.

      On rereading, I think you're right and my interpretation was incorrect.

      — Ken