Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Execute function before exit of do() block

by moritz (Cardinal)
on Jan 29, 2014 at 19:04 UTC ( #1072561=note: print w/replies, xml ) Need Help??

in reply to Execute function before exit of do() block

One technique you can use is a "scope guard"; an object which goes out of scope when a scope is exited. When it goes out of scope, its reference count drops to zero and its DESTROY method is called:

# use 5.014; use warnings; my $CleanExit = 0; { package My::ScopeGuard; sub DESTROY { return if $CleanExit; say 'cleaning up'; } } # this is the object that will go out of scope my $guard = bless {}, 'My::ScopeGuard'; say 'doing something'; die "Oh Noez"; # or if everything works fine: $CleanExit = 1;

Now we can test it likes this:

$ perl -wE 'do ""; say "Back in master"' doing something cleaning up Back in master

You can see that the cleanup happens before the the rest of the calling code.

Replies are listed 'Best First'.
Re^2: Execute function before exit of do() block
by shmem (Canon) on Jan 29, 2014 at 20:58 UTC

    I've been thinking about this, too, prior to writing my $SIG{__DIE__} answer above.
    Creating a sentinel object... slowly, the objectification creeps on every subject...

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

      Well, in Perl 6 there are are non-object options, like a LEAVE block that is called on every scope exit, or an UNDO block for unsuccessful scope exists.

      I wonder if those are possible (and worthy) to backport to perl 5...

        Maybe that could be done in a cheap way overriding eval. Brrr... just as I am writing this, overriding eval feels very evil... ;-)

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
      You could use clear or free magic if you wanted to avoid objects.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1072561]
[makita]: Need to sign more elements but all types I put in array are ignored. And is always signed only the body
[Discipulus]: no makita sorry. i see in the synopsis of the module: "WARNING: Only limited real-life experience" might be better compose a SOPW with some code example and data
[Discipulus]: ..or inspect the source code of the module
[makita]: I am doing this:
[makita]: my $sig = $wss->signature( schema => $wsdl, token => $token, sign_types=>'wsa: Action','wsa:To', 'wsu:Timestamp', 'wsa:MessageID',' SOAP-ENV:Body', signer=>DSIG_RSA_S HA1, public_key => $cert,
[Corion]: Oof, I haven't done much with signing SOAP requests. markov is usually somebody good to ask via mail ;)
[makita]: I need to create a SOAP call where are signed more types not only the body
[makita]: I am inspecting it second day :)
[makita]: Thanks
[Discipulus]: better to ask a full SOPW

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2017-03-23 08:45 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (285 votes). Check out past polls.