In PHP you might use @
in front of function call to suppress warnings returned.
Is there something similar in Perl?
Using legacy __attribute__ to provide more information to the compiler. Using _Pragma to suppress the warning. Using #pragma to suppress the warning. Using command line options to suppress the warning.
Strict and Warning are probably the two most commonly used Perl pragmas, and are frequently used to catch “unsafe code.” When Perl is set up to use these pragmas, the Perl compiler will check for, issue warnings against, and disallow certain programming constructs and techniques.
The strict pragma disables certain Perl expressions that could behave unexpectedly or are difficult to debug, turning them into errors. The effect of this pragma is limited to the current file or scope block. If no import list is supplied, all possible restrictions are assumed.
Another option is the --no-messages flag, shorthand -s . This will also get rid of the Is a directory messages, but it also suppresses other messages which might be more useful. For example, if you're doing a nested search in */*/* and no such file of that pattern exists, it won't tell you that.
This feature of PHP is crazy and should be avoided whenever possible.
Perl has two kinds of exceptions: Fatal errors and warnings. Warnings may either be emitted by Perl itself, or by user code.
Inside a certain static/lexical scope, Perl's builtin warnings can be switched off like:
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 (+)
}
Output: hello
on STDERR.
(A list of all warnings categories can be found here )
But this can't be used to remove warnings from code you are calling (dynamic scope). This also doesn't silence user-defined warnings.
In this case, you can write a handler for the __WARN__
pseudo-signal:
use warnings;
{
local $SIG{__WARN__} = sub { };
foo();
print "bye\n";
}
sub foo {
warn "hello\n";
1 + undef;
}
Output: bye
on STDOUT.
We can abstract that into a function muffle
:
sub muffle {
my $func = shift;
local $SIG{__WARN__} = sub { };
return $func->(@_);
}
muffle(\&foo, 1, 2, 3); # foo(1, 2, 3)
However, this is an incredibly dumb thing to do:
undef
value stringifies to the empty string, and don't want any warning for that.The strategies outlined here do not handle fatal exceptions, use Try::Tiny
instead.
You could also just run perl -X
and disable all warnings.
I think there are perfectly valid reasons for doing this FWIW.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With