ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Guard/Guard.pm
(Generate patch)

Comparing Guard/Guard.pm (file contents):
Revision 1.22 by root, Sun Jul 26 08:07:11 2009 UTC vs.
Revision 1.26 by root, Tue Mar 21 11:39:27 2017 UTC

42package Guard; 42package Guard;
43 43
44no warnings; 44no warnings;
45 45
46BEGIN { 46BEGIN {
47 $VERSION = '1.021'; 47 $VERSION = 1.023;
48 @ISA = qw(Exporter); 48 @ISA = qw(Exporter);
49 @EXPORT = qw(guard scope_guard); 49 @EXPORT = qw(guard scope_guard);
50 50
51 require Exporter; 51 require Exporter;
52 52
55} 55}
56 56
57our $DIED = sub { warn "$@" }; 57our $DIED = sub { warn "$@" };
58 58
59=item scope_guard BLOCK 59=item scope_guard BLOCK
60
61=item scope_guard ($coderef)
60 62
61Registers a block that is executed when the current scope (block, 63Registers a block that is executed when the current scope (block,
62function, method, eval etc.) is exited. 64function, method, eval etc.) is exited.
63 65
64See the EXCEPTIONS section for an explanation of how exceptions 66See the EXCEPTIONS section for an explanation of how exceptions
82BLOCK calls C<exit>, C<goto>, C<last> or escapes via other means. 84BLOCK calls C<exit>, C<goto>, C<last> or escapes via other means.
83 85
84If multiple BLOCKs are registered to the same scope, they will be executed 86If multiple BLOCKs are registered to the same scope, they will be executed
85in reverse order. Other scope-related things such as C<local> are managed 87in reverse order. Other scope-related things such as C<local> are managed
86via the same mechanism, so variables C<local>ised I<after> calling 88via the same mechanism, so variables C<local>ised I<after> calling
87C<scope_guard> will be restored when the guard runs. 89C<scope_guard> will be restored I<before> the guard runs.
88 90
89Example: temporarily change the timezone for the current process, 91Example: temporarily change the timezone for the current process,
90ensuring it will be reset when the C<if> scope is exited: 92ensuring it will be reset when the C<if> scope is exited:
91 93
92 use Guard; 94 use Guard;
102 104
103 # do something with the new timezone 105 # do something with the new timezone
104 } 106 }
105 107
106=item my $guard = guard BLOCK 108=item my $guard = guard BLOCK
109
110=item my $guard = guard ($coderef)
107 111
108Behaves the same as C<scope_guard>, except that instead of executing 112Behaves the same as C<scope_guard>, except that instead of executing
109the block on scope exit, it returns an object whose lifetime determines 113the block on scope exit, it returns an object whose lifetime determines
110when the BLOCK gets executed: when the last reference to the object gets 114when the BLOCK gets executed: when the last reference to the object gets
111destroyed, the BLOCK gets executed as with C<scope_guard>. 115destroyed, the BLOCK gets executed as with C<scope_guard>.
193solution to the problem of exceptions. 197solution to the problem of exceptions.
194 198
195=head1 SEE ALSO 199=head1 SEE ALSO
196 200
197L<Scope::Guard> and L<Sub::ScopeFinalizer>, which actually implement 201L<Scope::Guard> and L<Sub::ScopeFinalizer>, which actually implement
198dynamic guards only, not scoped guards, and have a lot higher CPU, memory 202dynamically scoped guards only, not the lexically scoped guards that their
199and typing overhead. 203documentation promises, and have a lot higher CPU, memory and typing
204overhead.
200 205
201L<Hook::Scope>, which has apparently never been finished and can corrupt 206L<Hook::Scope>, which has apparently never been finished and can corrupt
202memory when used. 207memory when used.
203 208
209L<Scope::Guard> seems to have a big SEE ALSO section for even more
210modules like it.
211
204=cut 212=cut
205 213

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines