$ perl -w -MData::Dumper -E' $SIG{__WARN__} = sub { print Dumper \@_, [ caller(0) ] }; # create an +d install the handler my $undef; # obviousely undefined say "$undef undef"; # warning occurs here ' $VAR1 = [ 'Use of uninitialized value $undef in concatenation (.) or s +tring at -e line 4. ' ]; $VAR2 = [ 'main', '-e', 1, 'main::__ANON__', 1, '', undef, undef, 133376, 'UUUUUUUUUUUUU', { 'feature_unicode' => 1, 'feature_say' => 1, 'feature_state' => 1, 'feature_switch' => 1 } ]; undef
As you can see, if you have installed a subroutine as the warning handler using %SIG hash, it gets called each time you encounter a warning with the error message as the first parameter ($_[0]), and you can obtain the information on where did the warning occur using caller. No error message is printed, you can warn it from your warning handler if you want to.
It's probably a good idea to make your changes to %SIG local, otherwise you can catch warnings in code you have no control of. Also have a look at the Carp module which is very useful for error reporting from modules and can print backtraces.
In reply to Re: trap run time errors
by aitap
in thread trap run time errors
by kp2a
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |