… | |
… | |
13 | expected. |
13 | expected. |
14 | |
14 | |
15 | Specifically, this module supports two different types of guards: guard |
15 | Specifically, this module supports two different types of guards: guard |
16 | objects, which execute a given code block when destroyed, and scoped |
16 | objects, which execute a given code block when destroyed, and scoped |
17 | guards, which are tied to the scope exit. |
17 | guards, which are tied to the scope exit. |
|
|
18 | |
|
|
19 | =head1 FUNCTIONS |
|
|
20 | |
|
|
21 | This module currently exports the C<scope_guard> and C<guard> functions by |
|
|
22 | default. |
18 | |
23 | |
19 | =over 4 |
24 | =over 4 |
20 | |
25 | |
21 | =cut |
26 | =cut |
22 | |
27 | |
… | |
… | |
38 | =item scope_guard BLOCK |
43 | =item scope_guard BLOCK |
39 | |
44 | |
40 | Registers a block that is executed when the current scope (block, |
45 | Registers a block that is executed when the current scope (block, |
41 | function, method, eval etc.) is exited. |
46 | function, method, eval etc.) is exited. |
42 | |
47 | |
|
|
48 | The description below sounds a bit complicated, but that's just because |
|
|
49 | C<scope_guard> tries to get even corner cases "right": the goal is to |
|
|
50 | provide you with a rock solid clean up tool. |
|
|
51 | |
43 | This is similar to this code fragment: |
52 | This is similar to this code fragment: |
44 | |
53 | |
45 | eval ... code following scope_guard ... |
54 | eval ... code following scope_guard ... |
46 | { |
55 | { |
47 | local $@; |
56 | local $@; |
48 | eval BLOCK; |
57 | eval BLOCK; |
49 | eval { $Guard::DIED->() } if $@; |
58 | eval { $Guard::DIED->() } if $@; |
50 | } |
59 | } |
|
|
60 | die if $@; |
51 | |
61 | |
52 | Except it is much faster, and the whole thing gets executed even when the |
62 | Except it is much faster, and the whole thing gets executed even when the |
53 | BLOCK calls C<exit>, C<goto>, C<last> or escapes via other means. |
63 | BLOCK calls C<exit>, C<goto>, C<last> or escapes via other means. |
54 | |
64 | |
55 | See B<EXCEPTIONS>, below, for an explanation of exception handling |
65 | See B<EXCEPTIONS>, below, for an explanation of exception handling |