How can I suppress warnings from a Perl function?

In PHP, you can use @functions before suppression to suppress returned warnings.

Is there something similar in Perl?

+6
source share
3 answers

This PHP feature is crazy and should be avoided whenever possible.

Perl has two kinds of exceptions: fatal errors and warnings. Warnings can be issued either by Perl itself or by user code.

Within a specific static / lexical scope, Perl's built-in warnings can be disabled as follows:

use warnings;

foo();

sub foo {
  no warnings 'uninitialized';  # recommended: only switch of specific categories
  warn "hello\n";
  1 + undef;  # Otherwise: Use of uninitialized value in addition (+)
}

Exit: helloto STDERR.

( )

, ( ). .

__WARN__:

use warnings;
{
  local $SIG{__WARN__} = sub { };
  foo();
  print "bye\n";
}

sub foo {
  warn "hello\n";
  1 + undef;
}

: bye STDOUT.

muffle:

sub muffle {
  my $func = shift;
  local $SIG{__WARN__} = sub { };
  return $func->(@_);
}

muffle(\&foo, 1, 2, 3); # foo(1, 2, 3)

, :

  • . , .
  • , . , , undef , .

, , , Try::Tiny.

+25

perl -X .

, FWIW .

+3

no warnings; . perldoc. , , .

.

{
     no warnings;
     <statement giving warning>
}
0

All Articles