develooper Front page | perl.perl5.porters | Postings from June 2008

Re: Why $@ makes me cry (was Re: Generic system() replacements)

Thread Previous | Thread Next
From:
Mark Mielke
Date:
June 25, 2008 06:11
Subject:
Re: Why $@ makes me cry (was Re: Generic system() replacements)
Message ID:
486243F9.9080700@mark.mielke.cc
Jan Dubois wrote:
> On Tue, 24 Jun 2008, Abigail wrote:
>   
>> On Sat, Jun 21, 2008 at 12:25:53PM +1000, Paul Fenwick wrote:
>>     
>>> However I believe there may be some merit in saving $@ before a DESTROY
>>> is called, and restoring it afterwards.  That allows destruction to occur
>>> as it always has, but prevents said destruction from clobbering $@.  I
>>> imagine trying to capture a $@ from object destruction that's essentially
>>> happening "between the lines" is going to be a rather rare occurrence
>>> indeed.
>>>       
>> Eh, I know I shouldn't have done this, but I have written code that could
>> die inside a DESTROY, with an eval to capture this exception. That code
>> would fail if $@ is saved before a DESTROY and then restored.
>>     
>
> Can you post some code example that does this? A call to die($msg)
> inside DESTROY does not raise an exception, it simply does the
> equivalent of
>
>     $@ .= "\t(in cleanup) $msg";
>   

How about:
> $ perl -e 'sub DESTROY { die } eval { bless {} }; print "E: $@" if $@'
> E:      (in cleanup) Died at -e line 1.

Most people who run eval{} doesn't check for eval{} failing. They check 
for $@ being true. Whether $@ is true from eval{} catching an exception, 
or DESTROY doing $@ .=, the resulting $@ is still true.

I think Abigail already said this isn't necessary a GOOD thing to rely 
on - but it is possible that code today is protected by this behaviour. 
Personally, I dislike the idea of $@ being modified but no exception 
being thrown in the above example. a die() should be a die(). If the 
die() is already in progress, then sure, $@ .= is probably better than 
the alternative of losing one of the messages.

Cheers,
mark

-- 
Mark Mielke <mark@mielke.cc>


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