Warnings can be lexically scoped. Hence:

use strict; use warnings; my $str = "junk not junk"; { no warnings qw/void/; $str !~ s/junk//g; }

This silences the "void context" warning but only on operations that occur within the curly brackets. perllexwarn for more fun details.

I prefer this method to your if(){} method, because it is easier to understand if you come along six months down the road and wonder why on earth you wrapped a substitution in an empty if(){} statement. Looking at the no warnings code, you'll remember, "Oh, I'm squelching a void context warning." The if(){} method, you'll look and say to yourself, "What was I thinking?" My way utilizes the primary purpose of the "no warnings" construct. You were well justified in looking for a better way as the method you suggested utilizes a tertiary side-effect of the if(){} statement.

Great question, by the way.


Dave


In reply to Re: Is there a better way of dealing with !~ and "Useless use of not in void context at" warnings? by davido
in thread Is there a better way of dealing with !~ and "Useless use of not in void context at" warnings? by Plankton

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.