Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://608254]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2018-06-25 05:01 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.