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

Comparing Guard/Guard.pm (file contents):
Revision 1.8 by root, Sat Dec 13 18:47:40 2008 UTC vs.
Revision 1.10 by root, Sat Dec 13 18:50:31 2008 UTC

2 2
3Guard - safe cleanup blocks 3Guard - safe cleanup blocks
4 4
5=head1 SYNOPSIS 5=head1 SYNOPSIS
6 6
7 use Guard; 7 use Guard;
8 8
9 # temporarily chdir to "/etc" directory, but make sure 9 # temporarily chdir to "/etc" directory, but make sure
10 # to go back to "/" no matter how myfun exits: 10 # to go back to "/" no matter how myfun exits:
11 sub myfun { 11 sub myfun {
12 scope_guard { chdir "/" }; 12 scope_guard { chdir "/" };
13 chdir "/etc"; 13 chdir "/etc";
14 14
15 call_function_that_might_die_or_other_fun_stuff; 15 call_function_that_might_die_or_other_fun_stuff;
16 } 16 }
17 17
18=head1 DESCRIPTION 18=head1 DESCRIPTION
19 19
20This module implements so-called "guards". A guard is something (usually 20This module implements so-called "guards". A guard is something (usually
21an object) that "guards" a resource, ensuring that it is cleaned up when 21an object) that "guards" a resource, ensuring that it is cleaned up when
107 107
108See the EXCEPTIONS section for an explanation of how exceptions 108See the EXCEPTIONS section for an explanation of how exceptions
109(i.e. C<die>) are handled inside guard blocks. 109(i.e. C<die>) are handled inside guard blocks.
110 110
111Example: acquire a Coro::Semaphore for a second by registering a 111Example: acquire a Coro::Semaphore for a second by registering a
112timer. The timer callback references the guard used to unlock it again. 112timer. The timer callback references the guard used to unlock it
113again. (Please ignore the fact that C<Coro::Semaphore> has a C<guard>
114method that does this already):
113 115
116 use Guard;
114 use AnyEvent; 117 use AnyEvent;
115 use Coro::Semaphore; 118 use Coro::Semaphore;
116 119
117 my $sem = new Coro::Semaphore; 120 my $sem = new Coro::Semaphore;
118 121
119 sub lock_1s { 122 sub lock_for_a_second {
120 $sem->down; 123 $sem->down;
121 my $guard = guard { $sem->up }; 124 my $guard = guard { $sem->up };
122 125
123 my $timer; 126 my $timer;
124 $timer = AnyEvent->timer (after => 1, sub { 127 $timer = AnyEvent->timer (after => 1, sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines