package Scope::OnExit::Wrap;

use warnings;
use strict;

use base 'Exporter';

our $VERSION = '0.02';

our @EXPORT = qw(on_scope_exit);

our $_backend;

if (!$ENV{SCOPE_ONEXIT_WRAP_PP} && eval { require Scope::OnExit }) {
    $_backend = 'XS';
} else {
    eval <<'EOT';
sub on_scope_exit (&) {
    my ($code) = @_;
    bless \$code, __PACKAGE__

    my $self = shift;

    die $@ if $@;
    $_backend = 'PP';



=head1 NAME

Scope::OnExit::Wrap - run code on scope exit (with pure Perl fallback)


    my $var = foo();
    my $guard = on_scope_exit {
  }  # scope exit: do_something($var) is run now


This module is a thin wrapper around L<C<Scope::OnExit>|Scope::OnExit>, which
is written in C. If L<C<Scope::OnExit>|Scope::OnExit> is not available, it
provides its own pure Perl implementation equivalent to L<C<End>|End> (which
adds a tiny bit of overhead: an object is constructed and its destructor

=head2 Functions


=item on_scope_exit BLOCK

(This function is exported by default.)

Arranges for I<BLOCK> to be executed when the surrounding scope is exited
(whether it reaches the end normally, by L<C<last>|perlfunc/last>, by
L<C<return>|perlfunc/return>, or by L<throwing an exception|perlfunc/die>).

For compatibility with the XS and pure Perl implementations, you must save the
return value of C<on_scope_exit> in a lexical (L<C<my>|perlfunc/my>) variable,
which you then ignore (i.e. don't do anything with the variable afterwards).

(The XS code in L<C<Scope::OnExit>|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.)


=head1 SEE ALSO

L<Scope::OnExit>, L<End>

=head1 AUTHOR

Lukas Mai, C<< <l.mai at> >>


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 for more information.