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:
# slave.pl: use 5.014; use warnings; my $CleanExit = 0; { package My::ScopeGuard; sub DESTROY { return if $CleanExit; say 'cleaning up slave.pl'; } } # 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 "slave.pl"; say "Back in master"' doing something cleaning up slave.pl 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 (Chancellor) on Jan 29, 2014 at 20:58 UTC | |
by moritz (Cardinal) on Jan 29, 2014 at 21:04 UTC | |
by ikegami (Patriarch) on Jan 30, 2014 at 00:28 UTC | |
by shmem (Chancellor) on Jan 29, 2014 at 21:11 UTC | |
by ikegami (Patriarch) on Jan 30, 2014 at 00:22 UTC |