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:
Paul Fenwick
Date:
June 20, 2008 19:26
Subject:
Re: Why $@ makes me cry (was Re: Generic system() replacements)
Message ID:
485C66B1.4070702@perltraining.com.au
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;
             $robbie->attack($spy);
             1;
         } or do {
             say "You win this time, $spy!";
             make_hasty_retreat();
         }

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

         $cleaners->steam_clean($secret_underground_base);

     }

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.

Cheerio,

	Paul

-- 
Paul Fenwick <pjf@perltraining.com.au> | http://perltraining.com.au/
Director of Training                   | Ph:  +61 3 9354 6001
Perl Training Australia                | Fax: +61 3 9354 2681

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