develooper Front page | perl.perl5.porters | Postings from October 2003

DESTROY and $@

Christian Jaeger
October 19, 2003 07:00
DESTROY and $@
Message ID:

Today some perl weirdness has served me with some hours of bug hunting.

After an exception has been thrown, $@ can be erased by a destructor 
upon leaving the surrounding eval block, and thus you'll miss the 
exception entirely (and thus conclude everything inside the eval went 
fine when it didn't).

This could be viewed as only a design bug/deficiency, not an 
implementation bug.

If you think it's correct perl behaviour, please document it! Like 
"ATTENTION: always local'ize $@ in DESTROY methods unless you are 
absolutely sure that you won't enter an eval block (or anything else 
that modifies $@), be it directly or indirectly. Otherwise, your 
class will break the error handling in the code of your users."

     package Foo;
     sub new{bless{},__PACKAGE__};
     sub bala {warn "bala";}
     sub DESTROY{my$s=shift;eval{$s->bala };}
eval{ my $a=new Foo; die "bah";};
warn "OUTSIDE: $@";
warn $@;

bala at ./a line 18.
OUTSIDE:  at ./a line 22.
Warning: something's wrong at ./a line 23.

(Well, and that's perl telling me that's "wrong" there anyway?)

This is with 5.6.1 and 5.8.0

Christian. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About