Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^5: Execute function before exit of do() block

by shmem (Canon)
on Jan 29, 2014 at 20:35 UTC ( #1072579=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Execute function before exit of do() block
in thread Execute function before exit of do() block

The question is how can I force the specific clean up in case of an abnormal exit (i.e. croak) of each helper script?

As always, TIMTOWTDI. Here's one approach (helper script):

use warnings; use strict; use Carp; my $cleanup = sub { print "helper cleans up after itself!\n"; }; local $SIG{__DIE__} = $cleanup; print "Hello\n"; croak "bad thing"; print "There\n"; 1;

Running that with your main script of the OP gives:

qwurx [shmem] ~ > perl main.pl helper.pl Hello helper cleans up after itself! helper.pl: bad thing at helper.pl line 11 require helper.pl called at main.pl line 6 Done with main

See %SIG. The local __DIE__ handler is made into an anonymous subroutine assigned to a lexical variable in order to not pollute any namespace.

update:

Reading the documentation I linked to, I find the following:

Due to an implementation glitch, the $SIG{__DIE__} hook is called even inside an eval(). Do not use this to rewrite a pending exception in $@ , or as a bizarre substitute for overriding CORE::GLOBAL::die() . This strange action at a distance may be fixed in a future release so that $SIG{__DIE__} is only called if your program is about to exit, as was the original intent. Any other use is deprecated.

So... that might be not that sound an advice as I thought it would be. I disagree with this being an implementation glitch, though. A do file should die in the same way as a use, which is also eval at its core. But it is not clear whether the glitchiness applies to all sorts of eval.

perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'


Comment on Re^5: Execute function before exit of do() block
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072579]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2014-09-17 21:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (99 votes), past polls