Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Debugging "Use of uninitialized value" warnings.

by Tobiwan (Beadle)
on Apr 04, 2007 at 11:20 UTC ( #608254=note: print w/ replies, xml ) Need Help??


in reply to Debugging "Use of uninitialized value" warnings.

Hi, there is no simple way to do that and the exact behavior as described I can't emulate (today). I believe, that it's possible with deeper knowledge in perlXS, to access the exact codeline which throw the warning and extract their content. But you can define a signalhandler and use PadWalker to print out the lexical and global variables. The following example output these warnings:

Warning: Use of uninitialized value in concatenation (.) or string at mpu.cgi line 25.
Possible variables are:
          '$GLOBAL_VARIABLE' => \'global',
          '$t' => \undef,
          '$s' => \'regular output'
#!/usr/bin/perl use strict; use warnings; use PadWalker(); use Data::Dumper(); local $SIG{'__WARN__'} = sub { if($_[0] !~ /^Use of uninitialized value/) { print @_; } else { # If there are objects, the output can be VERY large when you +increase this local $Data::Dumper::Maxdepth = 2; # takes all lexical variables from caller-nemaspace my $possibles = Data::Dumper::Dumper({ %{PadWalker::peek_my(1) +}, %{PadWalker::peek_our(1)} }); $possibles ne "\$VAR1 = {};\n" ? ($possibles =~ s/^.*?\n(.*)\n +.*?\n$/$1/ms) : ($possibles = ''); print STDERR "Warning: " . join(', ', @_) . "Possible variable +s are:\n$possibles\n"; } }; our $GLOBAL_VARIABLE = 'global'; sub mySub { my $t = undef; my $s = "regular output"; print " $s $t "; } mySub();
Warnings abouot uninitialized values are resolved, all others behave like before. This you should use only to develop. Some warings about uninitialized variables can be very large, when the caller-namespace contain many variables. Tobiwan


Comment on Re: Debugging "Use of uninitialized value" warnings.
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://608254]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (11)
As of 2014-10-20 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (93 votes), past polls