1package autodie::Scope::Guard;
2
3use strict;
4use warnings;
5
6# ABSTRACT: Wrapper class for calling subs at end of scope
7our $VERSION = '2.36'; # VERSION
8
9# This code schedules the cleanup of subroutines at the end of
10# scope.  It's directly inspired by chocolateboy's excellent
11# Scope::Guard module.
12
13sub new {
14    my ($class, $handler) = @_;
15    return bless($handler, $class);
16}
17
18sub DESTROY {
19    my ($self) = @_;
20
21    $self->();
22}
23
241;
25
26__END__
27
28=head1 NAME
29
30autodie::Scope::Guard - Wrapper class for calling subs at end of scope
31
32=head1 SYNOPSIS
33
34    use autodie::Scope::Guard;
35    $^H{'my-key'} = autodie::Scope::Guard->new(sub {
36        print "Hallo world\n";
37    });
38
39=head1 DESCRIPTION
40
41This class is used to bless perl subs so that they are invoked when
42they are destroyed.  This is mostly useful for ensuring the code is
43invoked at end of scope.  This module is not a part of autodie's
44public API.
45
46This module is directly inspired by chocolateboy's excellent
47Scope::Guard module.
48
49=head2 Methods
50
51=head3 new
52
53  my $hook = autodie::Scope::Guard->new(sub {});
54
55Creates a new C<autodie::Scope::Guard>, which will invoke the given
56sub once it goes out of scope (i.e. its DESTROY handler is called).
57
58=head1 AUTHOR
59
60Copyright 2008-2009, Paul Fenwick E<lt>pjf@perltraining.com.auE<gt>
61
62=head1 LICENSE
63
64This module is free software.  You may distribute it under the
65same terms as Perl itself.
66