die on "use of uninitialized value"?

I

ivowel

dear perl experts: is it possible to instruct perl to die on run-time
if it accesses an undefined scalar ? I would rather know right away
when I have committed a programming error.

sincerely,

/iaw
 
D

DJ Stunks

dear perl experts: is it possible to instruct perl to die on run-time
if it accesses an undefined scalar ? I would rather know right away
when I have committed a programming error.

use warnings FATAL => qw{ uninitialized };

-jp
 
D

DJ Stunks

Michele said:
Whoa! I was thinking about writing a die handler myself. I didn't know
about this semantic. Thank you!!

No problem. :)

Another advantage of lexical warnings (people always ask...)

-jp
 
B

Brian McCauley

Michele said:
Yes, but that would be rather strange since that particular warning is
probably the most common and reasonable one for which people
consciously wants to temporarily disable warnings.

That does not follow.

The fact that one often wants to treat undef as "" or 0 without warning
is in now way mutually exclusive with the idea that there are other
times when you'd want that same warning promoted to an error.
 
I

ivowel

ok, now I want more than the little finger, but the entire hand. can I
get a complete backtrace when I hit an unitialized string?

use Carp;
use warnings;
warnings::warn(&testd, "uninitialized");

sub testd { confess("you hit an uninit string.");}
sub myinerror { print "$2"; }
sub myouterror { myinerror(); }
myouterror();


regards,

/iaw
 
D

DJ Stunks

(e-mail address removed) wrote:

[please! don't top post! snip all but the attribution and some
appropriate context and post your response underneath!]
ok, now I want more than the little finger, but the entire hand. can I
get a complete backtrace when I hit an unitialized string?

use Carp;
use warnings;
warnings::warn(&testd, "uninitialized");

sub testd { confess("you hit an uninit string.");}
sub myinerror { print "$2"; }
sub myouterror { myinerror(); }
myouterror();

I think now you have to write your own __WARN__ handler. Here's my
15-second crack at it, this likely needs some tweaking:

#!/usr/bin/perl

use strict;

use Carp;
use warnings;

$SIG{__WARN__} = sub {
if ( $_[0] =~ m{ uninitialized }x ) {
confess('you hit an uninitialized string');
}
else {
print STDERR $_[0];
}
};

myouterror();

sub myinerror { print "$2"; }
sub myouterror { myinerror(); }

__END__

-jp
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top