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

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

Thread Previous | Thread Next
Paul Fenwick
June 20, 2008 19:26
Re: Why $@ makes me cry (was Re: Generic system() replacements)
Message ID:
David Nicol wrote:

> How hard would it be to defer destruction of lexicals within evals
> until the statement containing the eval ends?  What if anything would
> that break?

Consider the following code:

     while (my $spy = detect_intruders()) {

         eval {
             my $robbie = Killer::Robot->new;
         } or do {
             say "You win this time, $spy!";

         my $cleaners = Professional::Cleaners->new;



Under Perl's current implementation, I know my killer robot will be 
deactivated as soon as its dealt with the pesky intruder.  However if 
lexicals don't get destroyed until the end of the surrounding block, I'd 
have a killer robot on the loose while my carpets are being steam cleaned. 
That's unlikely to produce the level of cleanliness needed to impress the 
administrators of Evil Inc who are coming around for dinner next Thursday.

Indeed, with the current implementation, I can be certain that any lexical I 
create will be closed, disconnected, powered-down, or otherwise rendered 
harmless as soon as it goes out of scope.  I have quite a bit of code that 
relies upon this with filehandles in particular.

And what if we have an eval inside an eval inside an eval?  Oh my!

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.



Paul Fenwick <> |
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

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