millardjk has asked for the wisdom of the Perl Monks concerning the following question:

I've got a fairly complex DBI extraction that "normally" completes without any "use of uninit..." warnings. I'm capturing STDERR to an email as it runs off cron, and I'd like to send just a bit more data (from my program) along with the error message so that I can better determine whether the DB, the data or my program is where things are "misbehaving." I've searched, but can't find a way to automagically trap short of some lengthy
if (!defined $var1->[$var2[{$var3}]] {...}
segments, making the reading/reviewing of the "meat" of the program a bit annoying. Suggestions?

Replies are listed 'Best First'.
Re: trapping "uninit" warnings to get more info
by dave_the_m (Monsignor) on Sep 14, 2005 at 16:27 UTC
    $ perl -we '$SIG{__WARN__} = sub { warn "got [@_]\n" }; $x=$x+1' got [Use of uninitialized value in addition (+) at -e line 1. ] $

    Dave.

      To expand on this to make it a bit more useful:

      $SIG{__WARN__} = \&_warn_mess; sub _warn_mess { require Carp; print STDERR Carp::longmess(@_); }
      This prints out the stack trace at the point of the warning - which I think is what will help the OP the most. I know I've gotten a lot of use out of that to the point where I've put it in its own module at work and can just use it when I need the help ;-)

      Update: Missed the extra underscores on __WARN__ and the print for the longmess - thanks to Hue-Bond for alerting me

Re: trapping "uninit" warnings to get more info
by millardjk (Initiate) on Sep 15, 2005 at 15:30 UTC
    Thanks for the info on the signal handler; there wasn't much documentation where I was looking on that topic. Unfortunately, I was looking for something that would let me include local variables, and the "stack trace" that Carp is supposed to show is empty data--in fact, when I use the Carp version, I get no warnings at all. I guess I need to use lots of "if defined", or replace undefined with "NULL" in my extraction...
Re: trapping "uninit" warnings to get more info
by QM (Parson) on Sep 15, 2005 at 17:49 UTC
    You may also be interested in the # line directive (see this section of perlsyn):
    # line 42 "This is where it all goes horribly wrong..." print "<",undef,">\n";
    gives
    Use of uninitialized value in print at This is where it all goes horri +bly wrong... line 42.
    This is even more useful in eval to pin down which eval caused the problem.

    Also check out Smart::Comments for even more functionality.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of