catching "Use of uninitialized value" warnings

B

badarisj

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
 
O

Oobi Van Doobi

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...
}
 
V

vparseval

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
 
B

badarisj

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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top