Commit | Line | Data |
---|---|---|
f91d7e0d CBW |
1 | package autodie::Scope::Guard; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | # ABSTRACT: Wrapper class for calling subs at end of scope | |
35c0561a | 7 | our $VERSION = '2.27'; # VERSION |
f91d7e0d CBW |
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 | ||
13 | sub new { | |
14 | my ($class, $handler) = @_; | |
15 | return bless($handler, $class); | |
16 | } | |
17 | ||
18 | sub DESTROY { | |
19 | my ($self) = @_; | |
20 | ||
21 | $self->(); | |
22 | } | |
23 | ||
24 | 1; | |
25 | ||
26 | __END__ | |
27 | ||
28 | =head1 NAME | |
29 | ||
30 | autodie::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 | ||
41 | This class is used to bless perl subs so that they are invoked when | |
42 | they are destroyed. This is mostly useful for ensuring the code is | |
43 | invoked at end of scope. This module is not a part of autodie's | |
44 | public API. | |
45 | ||
46 | This module is directly inspired by chocolateboy's excellent | |
47 | Scope::Guard module. | |
48 | ||
49 | =head2 Methods | |
50 | ||
51 | =head3 new | |
52 | ||
53 | my $hook = autodie::Scope::Guard->new(sub {}); | |
54 | ||
55 | Creates a new C<autodie::Scope::Guard>, which will invoke the given | |
56 | sub once it goes out of scope (i.e. its DESTROY handler is called). | |
57 | ||
58 | =head1 AUTHOR | |
59 | ||
60 | Copyright 2008-2009, Paul Fenwick E<lt>pjf@perltraining.com.auE<gt> | |
61 | ||
62 | =head1 LICENSE | |
63 | ||
64 | This module is free software. You may distribute it under the | |
65 | same terms as Perl itself. |