=encoding utf-8 =head1 NAME =begin original Scope::Guard - lexically-scoped resource management =end original Scope::Guard - レキシカルスコープにおけるリソース管理 =head1 SYNOPSIS =begin original my $guard = guard { ... }; # or my $guard = scope_guard \&handler; # or my $guard = Scope::Guard->new(sub { ... }); $guard->dismiss(); # disable the handler =end original my $guard = guard { ... }; # or my $guard = scope_guard \&handler; # or my $guard = Scope::Guard->new(sub { ... }); $guard->dismiss(); # ハンドラの無効化 =head1 DESCRIPTION =begin original 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 C 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: L =end original このモジュールは, スコープ終了地点でのクリーンアップまたはリソース管理などを行う 使い勝手のよい手段を提供します. これは例外を扱う場合に特に便利です: Cのコンストラクタはサブルーチンリファレンスを受け取り, スレッドの実行が早い段階でアボートしたときでもそれを呼び出すことを保証します. この機能は, perlのガーベッジコレクタが, レキシカルスコープの処理を自動的に引き受けることを保証するので, 実現できます. より多くの情報については Lを参照してください. =head1 METHODS =begin original =head2 new my $guard = Scope::Guard->new(sub { ... }); # or my $guard = Scope::Guard->new(\&handler); The C method creates a new C object which calls the supplied handler when its C method is called, typically at the end of the scope. =head2 dismiss $guard->dismiss(); # or $guard->dismiss(1); C detaches the handler from the C 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); =end original =head2 new my $guard = Scope::Guard->new(sub { ... }); # or my $guard = Scope::Guard->new(\&handler); Cメソッドは, Cメソッドが呼び出されるときに与えられたハンドラを呼び出す Cオブジェクトを生成します. Cメソッドは通常スコープの終了地点で呼ばれます. =head2 dismiss $guard->dismiss(); # or $guard->dismiss(1); Cは Cオブジェクトからハンドラを切り離します. オブジェクトが破棄されるときに, ハンドラを呼び出す"保証"を無効にします. 以下の呼び出しでハンドラを再び有効化できます: $guard->dismiss(0); =head1 EXPORTS =begin original =head2 guard C takes a block and returns a new C object. It can be used as a shorthand for: Scope::Guard->new(...) e.g. my $guard = guard { ... }; Note: calling C anonymously, i.e. in void context, will raise an exception. This is because anonymous guards are destroyed B (rather than at the end of the scope), which is unlikely to be the desired behaviour. =head2 scope_guard C is the same as C, 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 C, calling C in void context will raise an exception. =end original =head2 guard Cはブロックを受け取り, Cオブジェクトを返します. 次の省略表現として使えます. Scope::Guard->new(...) このように. my $guard = guard { ... }; 注記: Cを voidコンテキストで呼び出すと例外が発生します. これは(スコープの終了地点ではなく)B<即座>にカードが破棄されてしまい, 望む振る舞いにはならないためです. =head2 scope_guard Cは Cと同様ですが, ブロックではなくコードリファレンスを受け取ります. 例を挙げると my $guard = scope_guard \&handler; もしくは my $guard = scope_guard sub { ... }; もしくは my $guard = scope_guard $handler; Cと同様に, voidコンテキストでの Cの呼び出しは例外が発生します. =head1 VERSION 0.20 =head1 SEE ALSO =over =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =item * L =back =head1 AUTHOR chocolateboy =head1 COPYRIGHT 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. =cut