eval and DESTROY

Matt Lawrence matt.lawrence at virgin.net
Thu Jul 19 11:50:38 BST 2007

Hello all,

Apologies for veering wildly off-topic.

I've noticed a little edge-case with using block eval to trap fatal 
errors caused by an object going out of the scope of the eval block.

use strict;
use warnings;

sub DESTROY { die "Foo\n" };

eval { bless {}, 'main' };
print "1: $@\n";

eval { bless {}, 'main'; 1 };
print "2: $@\n";

eval { my $foo = bless {}, 'main'; 1; undef $foo };
print "3: $@\n";

I get:
1:      (in cleanup) Foo
3:      (in cleanup) Foo

The weird thing is, changing the second eval to "do" doesn't die and 
does set $@ correctly!

Is die-ing in DESTROY just a bad idea, or is this a bug?


More information about the london.pm mailing list