Scope::Guard(3)
π NAME π
Scope::Guard - lexically-scoped resource management
okay bestie, you're reading the Scope::Guard(3) page from the User Contributed Perl Documentation, and it's literally the manual for Scope::Guard. This module is all about lexically-scoped resource management β cleanup energy, but make it scoped. π
β¨ SYNOPSIS β¨
Here's the whole vibe in code:
my $guard = guard { ... };
# or
my $guard = scope_guard \&handler;
# or
my $guard = Scope::Guard->new(sub { ... });
$guard->dismiss(); # disable the handler
π DESCRIPTION π
okay so 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 Scope::Guard 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. no cap, that's the whole serve. π₯
For more information, see http://www.drdobbs.com/cpp/184403758.
π METHODS π
πΈ new πΈ
my $guard = Scope::Guard->new(sub { ... });
# or
my $guard = Scope::Guard->new(\&handler);
The new method creates a new Scope::Guard object which calls the supplied handler when its DESTROY method is called, typically at the end of the scope. That's it, that's the method. π
π¦ dismiss π¦
$guard->dismiss();
# or
$guard->dismiss(1);
dismiss detaches the handler from the Scope::Guard object. This revokes the "promise" to call the handler when the object is destroyed. The handler can be re-enabled by calling:
$guard->dismiss(0);
π EXPORTS π
π guard π
guard takes a block and returns a new Scope::Guard object. It can be used as a shorthand for:
Scope::Guard->new(...)
e.g.
my $guard = guard { ... };
β οΈ Note: calling guard anonymously, i.e. in void context, will raise an exception. This is because anonymous guards are destroyed immediately (rather than at the end of the scope), which is unlikely to be the desired behaviour. bestie, don't do it. π
π§ scope_guard π§
scope_guard is the same as guard, but it takes a code ref rather than a block. e.g.
my $guard = scope_guard \&handler;
or:
my $guard = scope_guard sub { ... };
or:
my $guard = scope_guard $handler;
As with guard, calling scope_guard in void context will raise an exception. same energy, different input. π«Ά
β VERSION β
0.21 β slay. β¨
π SEE ALSO π
these are the other modules in the group chat:
B::Hooks::EndOfScopeEndGuardHook::ScopeObject::DestroyerPerl::AtEndOfScopeReleaseActionScope::local_OnExitScope::OnExitSub::ScopeFinalizerValue::Canary
π AUTHOR π
chocolateboy <chocolate@cpan.org> β iconic. π
π COPYRIGHT π
Copyright (c) 2005-2015, chocolateboy.
This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.
β¨ mwah π β this page is from perl v5.42.0, dated 2015-07-20, and it's Scope::Guard(3). periodt. π«Ά
π¦ where this page comes from
- opensuse-leap 16.0: perl-Scope-Guard 0.21-160000.2.2 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms