develooper Front page | perl.perl6.language.errors | Postings from August 2000

$SIG{__DIE__} should be localized and cleared at the start of an eval block

Thread Previous | Thread Next
From:
Bart Lateur
Date:
August 25, 2000 01:15
Subject:
$SIG{__DIE__} should be localized and cleared at the start of an eval block
Message ID:
nn9cqsobi6vv066942jofkmdd597pf1rjp@4ax.com
Watch the behaviour of this code snippet in a current Perl (5.6):

	eval {
	    print STDERR "Testing...\n";
	    warn "Oops!";
	    print STDERR "Still going...\n";
	    die "Argh!!!";
	    print STDERR "I died, didn't I?\n";
	};
	print STDERR "Outcome: $@\n";
-->
	Testing...
	Oops! at test.pl line 4.
	Still going...
	Outcome: Argh!!! at test.pl line 6.

Now try again, but with this *in front* of the eval block:

	$SIG{__DIE__} = sub {
	    print STDERR "I got a fatal error: @_";
	    print STDERR "Died.\n";
	    exit;
	};

-->
Testing...
Oops! at test.pl line 10.
Still going...
I got a fatal error: Argh!!! at test.pl line 12.
Died.

The idea is to provide a way to do nice and clean fatal messages, e.g.
"fatals to browser" in a CGI environment.

But this messes up the behaviour of the eval: when the code inside the
eval block dies, the sub attached to $SIG{__DIE__} is executed, and the
script DOES die.

Putting a simple

	local $SIG{__DIE__};

at the start of the eval block rstores the old, and IMO desirable
behaviour.

My statement: this shouldn't be the programmer's responsibility. If
you're using other people's modules that depend on eval, you're in
trouble anyway. The "local $SIG{__DIE__};" statement should be implicit
at the start of the eval block.

-- 
	Bart.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About