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