12 my $handler = shift() || die "Scope::Guard::new: no handler supplied";
13 my $ref = ref $handler || '';
15 die "Scope::Guard::new: invalid handler - expected CODE ref, got: '$ref'"
16 unless (UNIVERSAL::isa($handler, 'CODE'));
18 bless [ 0, $handler ], ref $class || $class;
23 my $dismiss = @_ ? shift : 1;
25 $self->[0] = $dismiss;
30 my ($dismiss, $handler) = @$self;
32 $handler->() unless ($dismiss);
43 Scope::Guard - lexically scoped resource management
47 my $sg = Scope::Guard->new(sub { ... });
51 my $sg = Scope::Guard->new(\&handler);
53 $sg->dismiss(); # disable the handler
57 This module provides a convenient way to perform cleanup or other forms of resource
58 management at the end of a scope. It is particularly useful when dealing with exceptions:
59 the Scope::Guard constructor takes a reference to a subroutine that is guaranteed to
60 be called even if the thread of execution is aborted prematurely. This effectively allows
61 lexically-scoped "promises" to be made that are automatically honoured by perl's garbage
64 For more information, see: L<http://www.cuj.com/documents/s=8000/cujcexp1812alexandr/>
70 my $sg = Scope::Guard->new(sub { ... });
74 my $sg = Scope::Guard->new(\&handler);
78 Create a new Scope::Guard object which calls the supplied handler when its C<DESTROY> method is
79 called, typically when it goes out of scope.
93 Detach the handler from the Scope::Guard object. This revokes the "promise" to call the
94 handler when the object is destroyed.
96 The handler can be re-enabled by calling:
108 =item * L<Hook::LexWrap|Hook::LexWrap>
110 =item * L<Hook::Scope|Hook::Scope>
112 =item * L<Sub::ScopeFinalizer|Sub::ScopeFinalizer>
114 =item * L<Object::Destroyer|Object::Destroyer>
120 chocolateboy: <chocolate.boy@email.com>
124 Copyright (c) 2005-2007, chocolateboy.
126 This module is free software. It may be used, redistributed and/or modified under the same terms