I would like to increment the $SIG{__WARN__} solution. This is an example of how can you catch and analyze warnings at program run time:
$ 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.