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


In reply to Re: "Useless use of a constant" in grep block? by kcott
in thread "Useless use of a constant" in grep block? by perlancar

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.