Emits a warning, usually by printing it toSTDERR
.warn
interprets its operand LIST in the same way asdie
, but is slightly different in what it defaults to when LIST is empty or makes an empty string. If it is empty and$@
already contains an exception value then that value is used after appending"\t...caught"
. If it is empty and$@
is also empty then the string"Warning: Something's wrong"
is used.
By default, the exception derived from the operand LIST is stringified and printed toSTDERR
. This behaviour can be altered by installing a$SIG{__WARN__}
handler. If there is such a handler then no message is automatically printed; it is the handler's responsibility to deal with the exception as it sees fit (like, for instance, converting it into adie
). Most handlers must therefore arrange to actually display the warnings that they are not prepared to deal with, by callingwarn
again in the handler. Note that this is quite safe and will not produce an endless loop, since__WARN__
hooks are not called from inside one.
You will find this behavior is slightly different from that of$SIG{__DIE__}
handlers (which don't suppress the error text, but can instead calldie
again to change it).
Using a__WARN__
handler provides a powerful way to silence all warnings (even the so-called mandatory ones). An example:
# wipe out *all* compile-time warningsBEGIN { $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN } }my $foo = 10;my $foo = 20; # no warning about duplicate my $foo, # but hey, you asked for it!# no compile-time or run-time warnings before here$DOWARN = 1;# run-time warnings enabled after herewarn "\$foo is alive and $foo!"; # does show up
Seeperlvar for details on setting%SIG
entries and for more examples. See theCarp module for other kinds of warnings using itscarp
andcluck
functions.
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.