Scope::OnExit::Wrap - run code on scope exit (with pure Perl fallback)
{ my $var = foo(); my $guard = on_scope_exit { do_something($var); }; something_else(); } # scope exit: do_something($var) is run now
This module is a thin wrapper around Scope::OnExit, which is written in C. If Scope::OnExit is not available, it provides its own pure Perl implementation equivalent to End (which adds a tiny bit of overhead: an object is constructed and its destructor invoked).
Scope::OnExit
End
(This function is exported by default.)
Arranges for BLOCK to be executed when the surrounding scope is exited (whether it reaches the end normally, by last, by return, or by throwing an exception).
last
return
For compatibility with the XS and pure Perl implementations, you must save the return value of on_scope_exit in a lexical (my) variable, which you then ignore (i.e. don't do anything with the variable afterwards).
on_scope_exit
my
(The XS code in Scope::OnExit will register the block directly and not return anything useful, but the pure Perl substitute will return an object here whose destructor invokes the block.)
Scope::OnExit, End
Lukas Mai, <l.mai at web.de>
<l.mai at web.de>
Copyright 2013 Lukas Mai.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Scope::OnExit::Wrap, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Scope::OnExit::Wrap
CPAN shell
perl -MCPAN -e shell install Scope::OnExit::Wrap
For more information on module installation, please visit the detailed CPAN module installation guide.