Scope-Guard-0.20 > Scope::Guard
Scope-Guard-0.20

名前

Scope::Guard - lexically-scoped resource management

Scope::Guard - レキシカルスコープにおけるリソース管理

概要

    my $guard = guard { ... };

      # or

    my $guard = scope_guard \&handler;

      # or

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

    $guard->dismiss(); # disable the handler
    my $guard = guard { ... };

      # or

    my $guard = scope_guard \&handler;

      # or

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

    $guard->dismiss(); # ハンドラの無効化

説明

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: http://www.drdobbs.com/cpp/184403758

このモジュールは, スコープ終了地点でのクリーンアップまたはリソース管理などを行う 使い勝手のよい手段を提供します. これは例外を扱う場合に特に便利です: Scope::Guardのコンストラクタはサブルーチンリファレンスを受け取り, スレッドの実行が早い段階でアボートしたときでもそれを呼び出すことを保証します. この機能は, perlのガーベッジコレクタが, レキシカルスコープの処理を自動的に引き受けることを保証するので, 実現できます.

より多くの情報については http://www.drdobbs.com/cpp/184403758を参照してください.

メソッド

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);

new

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

      # or

    my $guard = Scope::Guard->new(\&handler);

newメソッドは, DESTROYメソッドが呼び出されるときに与えられたハンドラを呼び出す Scope::Guardオブジェクトを生成します. DESTROYメソッドは通常スコープの終了地点で呼ばれます.

dismiss

    $guard->dismiss();

      # or

    $guard->dismiss(1);

dismissScope::Guardオブジェクトからハンドラを切り離します. オブジェクトが破棄されるときに, ハンドラを呼び出す"保証"を無効にします.

以下の呼び出しでハンドラを再び有効化できます:

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

guard

guardはブロックを受け取り, Scope::Guardオブジェクトを返します. 次の省略表現として使えます.

    Scope::Guard->new(...)

このように.

    my $guard = guard { ... };

注記: guardを voidコンテキストで呼び出すと例外が発生します. これは(スコープの終了地点ではなく)即座にカードが破棄されてしまい, 望む振る舞いにはならないためです.

scope_guard

scope_guardguardと同様ですが, ブロックではなくコードリファレンスを受け取ります.

例を挙げると

    my $guard = scope_guard \&handler;

もしくは

    my $guard = scope_guard sub { ... };

もしくは

    my $guard = scope_guard $handler;

guardと同様に, voidコンテキストでの scope_guardの呼び出しは例外が発生します.

VERSION

0.20

SEE ALSO

作者

chocolateboy <[email protected]>

コピーライト

Copyright (c) 2005-2010, chocolateboy.

This module is free software. It may be used, redistributed and/or modified under the same terms as Perl itself.