Unwind::Protect - Run code after other code, even with exceptions
version 0.01
++$depth; unwind_protect { unsafe_function() } after => sub { --$depth };
Sometimes you need to run code after some other code. Two complications are preserving list; scalar; or void context, and dealing with exceptions. Unwind::Protect handles both for you.
Unwind::Protect
unwind-protect exists in Common Lisp with nearly the same interface.
unwind-protect
I strive to handle exceptions properly. It's not easy, because of all the tools Perl provides. $SIG{__DIE__} and caller make this code somewhat hairy. If you provide me a failing test where the unwind_protect stack frame is inadvertantly exposed, I will try to fix it.
$SIG{__DIE__}
caller
unwind_protect
unwind_protect takes a code block (similar to grep) then some named arguments. Currently the only named argument that is accepted is after which is a coderef to run after the primary code.
grep
after
Shawn M Moore, sartak@bestpractical.com
sartak@bestpractical.com
Context::Preserve
To install Unwind::Protect, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Unwind::Protect
CPAN shell
perl -MCPAN -e shell install Unwind::Protect
For more information on module installation, please visit the detailed CPAN module installation guide.