1Scope::Guard(3)       User Contributed Perl Documentation      Scope::Guard(3)
2
3
4

NAME

6       Scope::Guard - lexically-scoped resource management
7

SYNOPSIS

9           my $guard = guard { ... };
10
11             # or
12
13           my $guard = scope_guard \&handler;
14
15             # or
16
17           my $guard = Scope::Guard->new(\&handler);
18
19           $guard->dismiss(); # disable the handler
20

DESCRIPTION

22       This module provides a convenient way to perform cleanup or other forms
23       of resource management at the end of a scope. It is particularly useful
24       when dealing with exceptions: the "Scope::Guard" constructor takes a
25       reference to a subroutine that is guaranteed to be called even if the
26       thread of execution is aborted prematurely. This effectively allows
27       lexically-scoped "promises" to be made that are automatically honoured
28       by perl's garbage collector.
29
30       For more information, see: <http://www.drdobbs.com/cpp/184403758>
31

METHODS

33   new
34           my $guard = Scope::Guard->new(sub { ... });
35
36             # or
37
38           my $guard = Scope::Guard->new(\&handler);
39
40       The "new" method creates a new "Scope::Guard" object which calls the
41       supplied handler when its "DESTROY" method is called, typically at the
42       end of the scope.
43
44   dismiss
45           $guard->dismiss();
46
47             # or
48
49           $guard->dismiss(1);
50
51       "dismiss" detaches the handler from the "Scope::Guard" object. This
52       revokes the "promise" to call the handler when the object is destroyed.
53
54       The handler can be re-enabled by calling:
55
56           $guard->dismiss(0);
57

EXPORTS

59   guard
60       "guard" takes a block and returns a new "Scope::Guard" object. It can
61       be used as a shorthand for:
62
63           Scope::Guard->new(...)
64
65       e.g.
66
67           my $guard = guard { ... };
68
69       - or it can be called in void context to create a guard for the current
70       scope e.g.
71
72           guard { ... };
73
74       Because there is no way to dismiss the guard in the latter case, it is
75       assumed that the block knows how to deal with situations in which the
76       resource has already been managed e.g.
77
78           guard {
79               if ($resource->locked) {
80                   $resource->unlock;
81               }
82           };
83
84   scope_guard
85       "scope_guard" is the same as "guard", but it takes a code ref rather
86       than a block.  e.g.
87
88           my $guard = scope_guard \&handler;
89
90       or:
91
92           my $guard = scope_guard sub { ... };
93
94       or:
95
96           my $guard = scope_guard $handler;
97
98       Like "guard", it can be called in void context to install an anonymous
99       guard in the current scope.
100

VERSION

102       0.12
103

SEE ALSO

105       ·   B::Hooks::EndOfScope
106
107       ·   End
108
109       ·   Guard
110
111       ·   Hook::Scope
112
113       ·   Object::Destroyer
114
115       ·   Perl::AtEndOfScope
116
117       ·   ReleaseAction
118
119       ·   Scope::OnExit
120
121       ·   Sub::ScopeFinalizer
122
123       ·   Value::Canary
124

AUTHOR

126       chocolateboy <chocolate@cpan.org>
127
129       Copyright (c) 2005-2010, chocolateboy.
130
131       This module is free software. It may be used, redistributed and/or
132       modified under the same terms as Perl itself.
133
134
135
136perl v5.12.0                      2010-03-26                   Scope::Guard(3)
Impressum