Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

die on "use of uninitialized value"?

0 views
Skip to first unread message

ivo...@gmail.com

unread,
Nov 24, 2006, 2:00:21 PM11/24/06
to

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

DJ Stunks

unread,
Nov 24, 2006, 2:26:28 PM11/24/06
to

use warnings FATAL => qw{ uninitialized };

-jp

Michele Dondi

unread,
Nov 24, 2006, 5:42:47 PM11/24/06
to

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.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,

Michele Dondi

unread,
Nov 24, 2006, 5:43:59 PM11/24/06
to
On 24 Nov 2006 11:26:28 -0800, "DJ Stunks" <DJSt...@gmail.com> wrote:

> use warnings FATAL => qw{ uninitialized };

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

DJ Stunks

unread,
Nov 24, 2006, 5:57:04 PM11/24/06
to
Michele Dondi wrote:
> On 24 Nov 2006 11:26:28 -0800, "DJ Stunks" <DJSt...@gmail.com> wrote:
>
> > use warnings FATAL => qw{ uninitialized };
>
> 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

Sherm Pendley

unread,
Nov 24, 2006, 6:54:46 PM11/24/06
to
Michele Dondi <bik....@tiscalinet.it> writes:

> On 24 Nov 2006 11:26:28 -0800, "DJ Stunks" <DJSt...@gmail.com> wrote:
>
>> use warnings FATAL => qw{ uninitialized };
>
> Whoa! I was thinking about writing a die handler myself. I didn't know
> about this semantic. Thank you!!

Kind of odd that it isn't mentioned in 'perldoc warnings', isn't it?

Granted, there's a cross-ref to 'perldoc perllexwarn' in there, but I'd
think it would worth at least a mention in 'perldoc warnings'.

Yah, I know - tell it to p5p, accompanied with a patch. Will do. :-)

sherm--

--
Web Hosting by West Virginians, for West Virginians: http://wv-www.net
Cocoa programming in Perl: http://camelbones.sourceforge.net

Brian McCauley

unread,
Nov 25, 2006, 10:30:50 AM11/25/06
to

Michele Dondi wrote:
> On 24 Nov 2006 11:00:21 -0800, ivo...@gmail.com wrote:
>
> >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.
>
> 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.

ivo...@gmail.com

unread,
Nov 25, 2006, 11:40:31 AM11/25/06
to

wonderful. thank you. regards, /iaw

Michele Dondi

unread,
Nov 26, 2006, 9:16:13 AM11/26/06
to
On 25 Nov 2006 07:30:50 -0800, "Brian McCauley" <nobu...@gmail.com>
wrote:

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

You're right, of course. But since the OP seemed to want to do so on a
regular basis, I wouldn't count that strictly as "other times". Well,
before you tell me, I'll say it in the first place: indeed "under any
circumstance" logically is a particular instance of a "in a particular
circumstance". I agree. Yet, in common sense they describe somewhat
different situations. So all in all I would say that the two concepts
are not perfectly orthogonal, but let's say that they have a very
small internal product wrt their norms, and I still find *slightly*
surprising to want to have *that* warning promoted to an error.

ivo...@gmail.com

unread,
Dec 8, 2006, 3:46:23 PM12/8/06
to

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

DJ Stunks

unread,
Dec 9, 2006, 1:10:18 AM12/9/06
to
ivo...@gmail.com 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

0 new messages