| 1 |
NAME |
| 2 |
Guard - safe cleanup blocks |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Guard; |
| 6 |
|
| 7 |
DESCRIPTION |
| 8 |
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 |
| 10 |
expected. |
| 11 |
|
| 12 |
Specifically, this module supports two different types of guards: guard |
| 13 |
objects, which execute a given code block when destroyed, and scoped |
| 14 |
guards, which are tied to the scope exit. |
| 15 |
|
| 16 |
FUNCTIONS |
| 17 |
This module currently exports the "scope_guard" and "guard" functions by |
| 18 |
default. |
| 19 |
|
| 20 |
scope_guard BLOCK |
| 21 |
Registers a block that is executed when the current scope (block, |
| 22 |
function, method, eval etc.) is exited. |
| 23 |
|
| 24 |
The description below sounds a bit complicated, but that's just |
| 25 |
because "scope_guard" tries to get even corner cases "right": the |
| 26 |
goal is to provide you with a rock solid clean up tool. |
| 27 |
|
| 28 |
This is similar to this code fragment: |
| 29 |
|
| 30 |
eval ... code following scope_guard ... |
| 31 |
{ |
| 32 |
local $@; |
| 33 |
eval BLOCK; |
| 34 |
eval { $Guard::DIED->() } if $@; |
| 35 |
} |
| 36 |
die if $@; |
| 37 |
|
| 38 |
Except it is much faster, and the whole thing gets executed even |
| 39 |
when the BLOCK calls "exit", "goto", "last" or escapes via other |
| 40 |
means. |
| 41 |
|
| 42 |
See EXCEPTIONS, below, for an explanation of exception handling |
| 43 |
("die") within guard blocks. |
| 44 |
|
| 45 |
Example: Temporarily change the directory to /etc and make sure it's |
| 46 |
set back to / when the function returns: |
| 47 |
|
| 48 |
sub dosomething { |
| 49 |
scope_guard { chdir "/" }; |
| 50 |
chdir "/etc"; |
| 51 |
|
| 52 |
... |
| 53 |
} |
| 54 |
|
| 55 |
my $guard = guard BLOCK |
| 56 |
Behaves the same as "scope_guard", except that instead of executing |
| 57 |
the block on scope exit, it returns an object whose lifetime |
| 58 |
determines when the BLOCK gets executed: when the last reference to |
| 59 |
the object gets destroyed, the BLOCK gets executed as with |
| 60 |
"scope_guard". |
| 61 |
|
| 62 |
The returned object can be copied as many times as you want. |
| 63 |
|
| 64 |
See EXCEPTIONS, below, for an explanation of exception handling |
| 65 |
("die") within guard blocks. |
| 66 |
|
| 67 |
Example: acquire a Coro::Semaphore for a second by registering a |
| 68 |
timer. The timer callback references the guard used to unlock it |
| 69 |
again. |
| 70 |
|
| 71 |
use AnyEvent; |
| 72 |
use Coro::Semaphore; |
| 73 |
|
| 74 |
my $sem = new Coro::Semaphore; |
| 75 |
|
| 76 |
sub lock_1s { |
| 77 |
$sem->down; |
| 78 |
my $guard = guard { $sem->up }; |
| 79 |
|
| 80 |
my $timer; |
| 81 |
$timer = AnyEvent->timer (after => 1, sub { |
| 82 |
# do something |
| 83 |
undef $sem; |
| 84 |
undef $timer; |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
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 |
| 90 |
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 |
| 92 |
only when the last one gets executed will the lock be unlocked. |
| 93 |
|
| 94 |
Guard::cancel $guard |
| 95 |
Calling this function will "disable" the guard object returned by |
| 96 |
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. |
| 98 |
|
| 99 |
This can be useful when you use "guard" to create a fatal cleanup |
| 100 |
handler and later decide it is no longer needed. |
| 101 |
|
| 102 |
EXCEPTIONS |
| 103 |
Guard blocks should not normally throw exceptions (e.g. "die"), after |
| 104 |
all, they are usually used to clean up after such exceptions. However, |
| 105 |
if something truly exceptional is happening, a guard block should be |
| 106 |
allowed to die. Also, programming errors are a large source of |
| 107 |
exceptions, and the programmer certainly wants to know about those. |
| 108 |
|
| 109 |
Since in most cases, the block executing when the guard gets executes |
| 110 |
does not know or does not care about the guard blocks, it makes little |
| 111 |
sense to let containing code handle the exception. |
| 112 |
|
| 113 |
Therefore, whenever a guard block throws an exception, it will be |
| 114 |
caught, and this module will call the code reference stored in |
| 115 |
$Guard::DIED (with $@ set to the actual exception), which is similar to |
| 116 |
how most event loops handle this case. |
| 117 |
|
| 118 |
The code reference stored in $Guard::DIED should not die (behaviour is |
| 119 |
not guaranteed, but right now, the exception will simply be ignored). |
| 120 |
|
| 121 |
The default for $Guard::DIED is to call "warn "$@"". |
| 122 |
|
| 123 |
AUTHOR |
| 124 |
Marc Lehmann <schmorp@schmorp.de> |
| 125 |
http://home.schmorp.de/ |
| 126 |
|
| 127 |
THANKS |
| 128 |
To Marco Maisenhelder, who reminded me of the $Guard::DIED solution to |
| 129 |
the problem of exceptions. |
| 130 |
|