Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > catching "Use of uninitialized value" warnings

Reply
Thread Tools

catching "Use of uninitialized value" warnings

 
 
badarisj@gmail.com
Guest
Posts: n/a
 
      09-29-2006
folks,

now and then we see
Use of uninitialized value in xxx
warning messages while running a perl program.

is there a way to catch such warning messages that perl throws and
display
the stack that is leading to the warning message?

thanks,
-badari

 
Reply With Quote
 
 
 
 
Oobi Van Doobi
Guest
Posts: n/a
 
      09-30-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> folks,
>
> now and then we see
> Use of uninitialized value in xxx
> warning messages while running a perl program.
>
> is there a way to catch such warning messages that perl throws and
> display
> the stack that is leading to the warning message?
>
> thanks,
> -badari

umm, a good thing to do would be to check if variables you use are
inizialised. you can do so with for example:
if ( defined($variable) )
{
...code here...
}
 
Reply With Quote
 
 
 
 
vparseval@gmail.com
Guest
Posts: n/a
 
      09-30-2006
(E-Mail Removed) wrote:

> now and then we see
> Use of uninitialized value in xxx
> warning messages while running a perl program.
>
> is there a way to catch such warning messages that perl throws and
> display
> the stack that is leading to the warning message?


For development purposes you can install a SIGWARN handler that will
print a stacktrace when the warning message conforms to certain
criteria:

#!/usr/bin/perl

use warnings;

$SIG{__WARN__} = sub {
if ($_[0] =~ /^Use of uninitialized value/) {
require Carp;
Carp::cluck();
} else {
warn @_;
}
};

sub foo {
print undef;
}

sub bla {
foo();
}

bla(42);
__END__
at cluck.pl line 6
main::__ANON__('Use of uninitialized value in print at
cluck.pl line 13.\x{a}') called at cluck.pl line 13
main::foo() called at confess.pl line 17
main::bla(42) called at confess.pl line 20

Cheers,
Tassilo

 
Reply With Quote
 
badarisj@gmail.com
Guest
Posts: n/a
 
      10-03-2006
hi tassilo,

thanks a lot. i didn't realize that all messages that the perl
interpreter
shows can be trapped using __WARN__ or __DIE__ sighandlers.
works well for what i want.

-badari


(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>
> > now and then we see
> > Use of uninitialized value in xxx
> > warning messages while running a perl program.
> >
> > is there a way to catch such warning messages that perl throws and
> > display
> > the stack that is leading to the warning message?

>
> For development purposes you can install a SIGWARN handler that will
> print a stacktrace when the warning message conforms to certain
> criteria:
>
> #!/usr/bin/perl
>
> use warnings;
>
> $SIG{__WARN__} = sub {
> if ($_[0] =~ /^Use of uninitialized value/) {
> require Carp;
> Carp::cluck();
> } else {
> warn @_;
> }
> };
>
> sub foo {
> print undef;
> }
>
> sub bla {
> foo();
> }
>
> bla(42);
> __END__
> at cluck.pl line 6
> main::__ANON__('Use of uninitialized value in print at
> cluck.pl line 13.\x{a}') called at cluck.pl line 13
> main::foo() called at confess.pl line 17
> main::bla(42) called at confess.pl line 20
>
> Cheers,
> Tassilo


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
empty variables - getting rid of "uninitialized value" warnings? Tomasz Chmielewski Perl Misc 12 03-31-2008 11:24 PM
Have Ruby warnings about uninitialized instance variables gotten more verbose in the past year? Bill McNeill Ruby 1 10-18-2007 03:08 AM
uninitialized value warnings and run time julia Perl Misc 6 01-05-2007 03:47 AM
portable no warnings "uninitialized" DKW Perl Misc 10 01-14-2005 05:55 PM
use warnings; and use Warnings; give different results Ted Sung Perl Misc 1 08-30-2004 10:22 PM



Advertisments