perlfaq7.pod has a very related advice:
=head2 How can I catch accesses to undefined variables/functions/metho +ds? The AUTOLOAD method, discussed in L<perlsub/"Autoloading"> and L<perltoot/"AUTOLOAD: Proxy Methods">, lets you capture calls to undefined functions and methods. When it comes to undefined variables that would trigger a warning under C<-w>, you can use a handler to trap the pseudo-signal C<__WARN__> like this: $SIG{__WARN__} = sub { for ( $_[0] ) { # voici un switch statement /Use of uninitialized value/ && do { # promote warning to a fatal die $_; }; # other warning cases to catch could go here; warn $_; } };

Adopt that code, and you will be able to "intercept" your warnings and use a value of "$!" at your will
Also, do not forget to protect your code by

try { } if ($@) { ... }
Jim

In reply to Re: Trapping a warning by Jim Morrison
in thread Trapping a warning by blackjudas

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.