.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Scope::Guard 3" .TH Scope::Guard 3 2023-07-26 "perl v5.38.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Scope::Guard \- lexically\-scoped resource management .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& my $guard = guard { ... }; \& \& # or \& \& my $guard = scope_guard \e&handler; \& \& # or \& \& my $guard = Scope::Guard\->new(sub { ... }); \& \& $guard\->dismiss(); # disable the handler .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" This module provides a convenient way to perform cleanup or other forms of resource management at the end of a scope. It is particularly useful when dealing with exceptions: the \f(CW\*(C`Scope::Guard\*(C'\fR constructor takes a reference to a subroutine that is guaranteed to be called even if the thread of execution is aborted prematurely. This effectively allows lexically-scoped "promises" to be made that are automatically honoured by perl's garbage collector. .PP For more information, see: .SH METHODS .IX Header "METHODS" .SS new .IX Subsection "new" .Vb 1 \& my $guard = Scope::Guard\->new(sub { ... }); \& \& # or \& \& my $guard = Scope::Guard\->new(\e&handler); .Ve .PP The \f(CW\*(C`new\*(C'\fR method creates a new \f(CW\*(C`Scope::Guard\*(C'\fR object which calls the supplied handler when its \f(CW\*(C`DESTROY\*(C'\fR method is called, typically at the end of the scope. .SS dismiss .IX Subsection "dismiss" .Vb 1 \& $guard\->dismiss(); \& \& # or \& \& $guard\->dismiss(1); .Ve .PP \&\f(CW\*(C`dismiss\*(C'\fR detaches the handler from the \f(CW\*(C`Scope::Guard\*(C'\fR object. This revokes the "promise" to call the handler when the object is destroyed. .PP The handler can be re-enabled by calling: .PP .Vb 1 \& $guard\->dismiss(0); .Ve .SH EXPORTS .IX Header "EXPORTS" .SS guard .IX Subsection "guard" \&\f(CW\*(C`guard\*(C'\fR takes a block and returns a new \f(CW\*(C`Scope::Guard\*(C'\fR object. It can be used as a shorthand for: .PP .Vb 1 \& Scope::Guard\->new(...) .Ve .PP e.g. .PP .Vb 1 \& my $guard = guard { ... }; .Ve .PP Note: calling \f(CW\*(C`guard\*(C'\fR anonymously, i.e. in void context, will raise an exception. This is because anonymous guards are destroyed \fBimmediately\fR (rather than at the end of the scope), which is unlikely to be the desired behaviour. .SS scope_guard .IX Subsection "scope_guard" \&\f(CW\*(C`scope_guard\*(C'\fR is the same as \f(CW\*(C`guard\*(C'\fR, but it takes a code ref rather than a block. e.g. .PP .Vb 1 \& my $guard = scope_guard \e&handler; .Ve .PP or: .PP .Vb 1 \& my $guard = scope_guard sub { ... }; .Ve .PP or: .PP .Vb 1 \& my $guard = scope_guard $handler; .Ve .PP As with \f(CW\*(C`guard\*(C'\fR, calling \f(CW\*(C`scope_guard\*(C'\fR in void context will raise an exception. .SH VERSION .IX Header "VERSION" 0.21 .SH "SEE ALSO" .IX Header "SEE ALSO" .IP \(bu 4 B::Hooks::EndOfScope .IP \(bu 4 End .IP \(bu 4 Guard .IP \(bu 4 Hook::Scope .IP \(bu 4 Object::Destroyer .IP \(bu 4 Perl::AtEndOfScope .IP \(bu 4 ReleaseAction .IP \(bu 4 Scope::local_OnExit .IP \(bu 4 Scope::OnExit .IP \(bu 4 Sub::ScopeFinalizer .IP \(bu 4 Value::Canary .SH AUTHOR .IX Header "AUTHOR" chocolateboy .SH COPYRIGHT .IX Header "COPYRIGHT" Copyright (c) 2005\-2015, chocolateboy. .PP This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.