in reply to Is there a better way of dealing with !~ and "Useless use of not in void context at" warnings?

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

  • Comment on Re: Is there a better way of dealing with !~ and "Useless use of not in void context at" warnings?
  • Select or Download Code