Scope::Guard

Perform cleanup or other forms of resource management at the end of a scope

Latest version: 0.21 registry icon
Maintenance score
0
Safety score
0
Popularity score
4
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
0.21 0 0 0 0 0
0.20 0 0 0 0 0
0.12 0 0 0 0 0
0.11 0 0 0 0 0
0.10 0 0 0 0 0
0.03 0 0 0 0 0
0.02 0 0 0 0 0
0.01 0 0 0 0 0

Stability
Latest release:

0.21 - This version may not be safe as it has not been updated for a long time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Artistic-1.0   -   Artistic License 1.0

Not a wildcard

Not proprietary

OSI Compliant


GPL-1.0-or-later   -   GNU General Public License v1.0 or later

Not a wildcard

Not proprietary

OSI Compliant



Scope::Guard

Build Status CPAN Version

NAME

Scope::Guard - lexically-scoped resource management

SYNOPSIS

my $guard = guard { ... };

  # or

my $guard = scope_guard \&handler;

  # or

my $guard = Scope::Guard->new(sub { ... });

$guard->dismiss(); # disable the handler

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 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.

For more information, see: https://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.

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.

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.

VERSION

0.21

SEE ALSO

AUTHOR

chocolateboy

COPYRIGHT AND LICENSE

Copyright © 2005-2021 by chocolateboy.

This is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.