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.
In reply to Re: Execute function before exit of do() block
by moritz
in thread Execute function before exit of do() block
by gri6507
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |