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

Comparing Guard/README (file contents):
Revision 1.2 by root, Sat Dec 13 17:50:29 2008 UTC vs.
Revision 1.7 by root, Sat Jul 2 00:38:44 2011 UTC

1NAME 1NAME
2 Guard - safe cleanup blocks 2 Guard - safe cleanup blocks
3 3
4SYNOPSIS 4SYNOPSIS
5 use Guard; 5 use Guard;
6
7 # temporarily chdir to "/etc" directory, but make sure
8 # to go back to "/" no matter how myfun exits:
9 sub myfun {
10 scope_guard { chdir "/" };
11 chdir "/etc";
12
13 code_that_might_die_or_does_other_fun_stuff;
14 }
15
16 # create an object that, when the last reference to it is gone,
17 # invokes the given codeblock:
18 my $guard = guard { print "destroyed!\n" };
19 undef $guard; # probably destroyed here
6 20
7DESCRIPTION 21DESCRIPTION
8 This module implements so-called "guards". A guard is something (usually 22 This module implements so-called "guards". A guard is something (usually
9 an object) that "guards" a resource, ensuring that it is cleaned up when 23 an object) that "guards" a resource, ensuring that it is cleaned up when
10 expected. 24 expected.
16FUNCTIONS 30FUNCTIONS
17 This module currently exports the "scope_guard" and "guard" functions by 31 This module currently exports the "scope_guard" and "guard" functions by
18 default. 32 default.
19 33
20 scope_guard BLOCK 34 scope_guard BLOCK
35 scope_guard ($coderef)
21 Registers a block that is executed when the current scope (block, 36 Registers a block that is executed when the current scope (block,
22 function, method, eval etc.) is exited. 37 function, method, eval etc.) is exited.
38
39 See the EXCEPTIONS section for an explanation of how exceptions
40 (i.e. "die") are handled inside guard blocks.
23 41
24 The description below sounds a bit complicated, but that's just 42 The description below sounds a bit complicated, but that's just
25 because "scope_guard" tries to get even corner cases "right": the 43 because "scope_guard" tries to get even corner cases "right": the
26 goal is to provide you with a rock solid clean up tool. 44 goal is to provide you with a rock solid clean up tool.
27 45
28 This is similar to this code fragment: 46 The behaviour is similar to this code fragment:
29 47
30 eval ... code following scope_guard ... 48 eval ... code following scope_guard ...
31 { 49 {
32 local $@; 50 local $@;
33 eval BLOCK; 51 eval BLOCK;
37 55
38 Except it is much faster, and the whole thing gets executed even 56 Except it is much faster, and the whole thing gets executed even
39 when the BLOCK calls "exit", "goto", "last" or escapes via other 57 when the BLOCK calls "exit", "goto", "last" or escapes via other
40 means. 58 means.
41 59
42 See EXCEPTIONS, below, for an explanation of exception handling 60 If multiple BLOCKs are registered to the same scope, they will be
43 ("die") within guard blocks. 61 executed in reverse order. Other scope-related things such as
62 "local" are managed via the same mechanism, so variables "local"ised
63 *after* calling "scope_guard" will be restored when the guard runs.
44 64
45 Example: Temporarily change the directory to /etc and make sure it's 65 Example: temporarily change the timezone for the current process,
46 set back to / when the function returns: 66 ensuring it will be reset when the "if" scope is exited:
47 67
48 sub dosomething { 68 use Guard;
49 scope_guard { chdir "/" }; 69 use POSIX ();
50 chdir "/etc";
51 70
52 ... 71 if ($need_to_switch_tz) {
72 # make sure we call tzset after $ENV{TZ} has been restored
73 scope_guard { POSIX::tzset };
74
75 # localise after the scope_guard, so it gets undone in time
76 local $ENV{TZ} = "Europe/London";
77 POSIX::tzset;
78
79 # do something with the new timezone
53 } 80 }
54 81
55 my $guard = guard BLOCK 82 my $guard = guard BLOCK
83 my $guard = guard ($coderef)
56 Behaves the same as "scope_guard", except that instead of executing 84 Behaves the same as "scope_guard", except that instead of executing
57 the block on scope exit, it returns an object whose lifetime 85 the block on scope exit, it returns an object whose lifetime
58 determines when the BLOCK gets executed: when the last reference to 86 determines when the BLOCK gets executed: when the last reference to
59 the object gets destroyed, the BLOCK gets executed as with 87 the object gets destroyed, the BLOCK gets executed as with
60 "scope_guard". 88 "scope_guard".
61 89
62 The returned object can be copied as many times as you want.
63
64 See EXCEPTIONS, below, for an explanation of exception handling 90 See the EXCEPTIONS section for an explanation of how exceptions
65 ("die") within guard blocks. 91 (i.e. "die") are handled inside guard blocks.
66 92
67 Example: acquire a Coro::Semaphore for a second by registering a 93 Example: acquire a Coro::Semaphore for a second by registering a
68 timer. The timer callback references the guard used to unlock it 94 timer. The timer callback references the guard used to unlock it
69 again. 95 again. (Please ignore the fact that "Coro::Semaphore" has a "guard"
96 method that does this already):
70 97
98 use Guard;
71 use AnyEvent; 99 use Coro::AnyEvent;
72 use Coro::Semaphore; 100 use Coro::Semaphore;
73 101
74 my $sem = new Coro::Semaphore; 102 my $sem = new Coro::Semaphore;
75 103
76 sub lock_1s { 104 sub lock_for_a_second {
77 $sem->down; 105 $sem->down;
78 my $guard = guard { $sem->up }; 106 my $guard = guard { $sem->up };
79 107
80 my $timer; 108 Coro::AnyEvent::sleep 1;
81 $timer = AnyEvent->timer (after => 1, sub { 109
82 # do something 110 # $sem->up gets executed when returning
83 undef $sem;
84 undef $timer;
85 });
86 } 111 }
87 112
88 The advantage of doing this with a guard instead of simply calling 113 The advantage of doing this with a guard instead of simply calling
89 "$sem->down" in the callback is that you can opt not to create the 114 "$sem->down" in the callback is that you can opt not to create the
90 timer, or your code can throw an exception before it can create the 115 timer, or your code can throw an exception before it can create the
91 timer, or you can create multiple timers or other event watchers and 116 timer (or the thread gets canceled), or you can create multiple
92 only when the last one gets executed will the lock be unlocked. 117 timers or other event watchers and only when the last one gets
118 executed will the lock be unlocked. Using the "guard", you do not
119 have to worry about catching all the places where you have to unlock
120 the semaphore.
93 121
94 Guard::cancel $guard 122 $guard->cancel
95 Calling this function will "disable" the guard object returned by 123 Calling this function will "disable" the guard object returned by
96 the "guard" function, i.e. it will free the BLOCK originally passed 124 the "guard" function, i.e. it will free the BLOCK originally passed
97 to "guard "and will arrange for the BLOCK not to be executed. 125 to "guard "and will arrange for the BLOCK not to be executed.
98 126
99 This can be useful when you use "guard" to create a fatal cleanup 127 This can be useful when you use "guard" to create a cleanup handler
100 handler and later decide it is no longer needed. 128 to be called under fatal conditions and later decide it is no longer
129 needed.
101 130
102EXCEPTIONS 131EXCEPTIONS
103 Guard blocks should not normally throw exceptions (e.g. "die"), after 132 Guard blocks should not normally throw exceptions (that is, "die").
104 all, they are usually used to clean up after such exceptions. However, 133 After all, they are usually used to clean up after such exceptions.
105 if something truly exceptional is happening, a guard block should be 134 However, if something truly exceptional is happening, a guard block
106 allowed to die. Also, programming errors are a large source of 135 should of course be allowed to die. Also, programming errors are a large
107 exceptions, and the programmer certainly wants to know about those. 136 source of exceptions, and the programmer certainly wants to know about
137 those.
108 138
109 Since in most cases, the block executing when the guard gets executes 139 Since in most cases, the block executing when the guard gets executed
110 does not know or does not care about the guard blocks, it makes little 140 does not know or does not care about the guard blocks, it makes little
111 sense to let containing code handle the exception. 141 sense to let containing code handle the exception.
112 142
113 Therefore, whenever a guard block throws an exception, it will be 143 Therefore, whenever a guard block throws an exception, it will be caught
114 caught, and this module will call the code reference stored in 144 by Guard, followed by calling the code reference stored in $Guard::DIED
115 $Guard::DIED (with $@ set to the actual exception), which is similar to 145 (with $@ set to the actual exception), which is similar to how most
116 how most event loops handle this case. 146 event loops handle this case.
147
148 The default for $Guard::DIED is to call "warn "$@"", i.e. the error is
149 printed as a warning and the program continues.
150
151 The $@ variable will be restored to its value before the guard call in
152 all cases, so guards will not disturb $@ in any way.
117 153
118 The code reference stored in $Guard::DIED should not die (behaviour is 154 The code reference stored in $Guard::DIED should not die (behaviour is
119 not guaranteed, but right now, the exception will simply be ignored). 155 not guaranteed, but right now, the exception will simply be ignored).
120
121 The default for $Guard::DIED is to call "warn "$@"".
122 156
123AUTHOR 157AUTHOR
124 Marc Lehmann <schmorp@schmorp.de> 158 Marc Lehmann <schmorp@schmorp.de>
125 http://home.schmorp.de/ 159 http://home.schmorp.de/
126 160
127THANKS 161THANKS
128 To Marco Maisenhelder, who reminded me of the $Guard::DIED solution to 162 Thanks to Marco Maisenhelder, who reminded me of the $Guard::DIED
129 the problem of exceptions. 163 solution to the problem of exceptions.
130 164
165SEE ALSO
166 Scope::Guard and Sub::ScopeFinalizer, which actually implement
167 dynamically scoped guards only, not the lexically scoped guards that
168 their documentation promises, and have a lot higher CPU, memory and
169 typing overhead.
170
171 Hook::Scope, which has apparently never been finished and can corrupt
172 memory when used.
173
174 Scope::Guard seems to have a big SEE ALSO section for even more modules
175 like it.
176

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines