Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

by shmem (Chancellor)
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 Hello helper cleans up after itself! bad thing at line 11 require called at 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.


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'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072579]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-05-20 12:37 GMT
Find Nodes?
    Voting Booth?