in reply to Re: Disable Perl warning
in thread Disable Perl warning

Occasionally, in very rare cases, a certain command may cause a warning that is spurious but unavoidable. In that case, you can enclose just the offending command in a lexical block and silence the warnings there only...

.

This is a necessary evil. While 99% of the time it's possible to, without jumping through coding hoops, code in such a way that produces no warnings, there is the 1% where the warning comes with the simplest use case:

use strict; use warnings; use List::Util qw( reduce ); print factorial(5), "\n"; sub factorial { return reduce { $a * $b } 1 .. $_[0] || 1; } __END__ __OUTPUT__ Name "main::a" used only once: possible typo at mytest.pl line 10. Name "main::b" used only once: possible typo at mytest.pl line 10. 120

The only reasonable way to squelch that pair of warnings is to put "no warnings;" inside reduce's code block, and yet we're not doing anything even mildly devious.


Dave

Replies are listed 'Best First'.
Re^3: Disable Perl warning
by choroba (Cardinal) on Nov 27, 2012 at 10:37 UTC
    The only reasonable way to squelch that pair of warnings is to put "no warnings;" inside reduce's code block
    Well, the other reasonable way is to put no warnings 'once'; into the block.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Within the scope of use strict qw(subs vars) the once warning is about a hundred times more annoying than it is useful.

      void and uninitialized are also of questionable value, and although the idea of numeric warnings is good, the details are silly...

      use feature qw(say); use warnings qw(numeric); my $foo = ""; say $foo+1; # why does this warn, my $bar = []; say $bar+1; # when this does not?
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'