NAME
Scope::OnExit::Wrap - run code on scope exit (with pure Perl fallback)
SYNOPSIS
{
my $var = foo();
my $guard = on_scope_exit {
do_something($var);
};
something_else();
} # scope exit: do_something($var) is run now
DESCRIPTION
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).
Functions
- on_scope_exit BLOCK
-
(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
, byreturn
, or by throwing an exception).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).(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.)
SEE ALSO
AUTHOR
Lukas Mai, <l.mai at web.de>
COPYRIGHT & LICENSE
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.