File Coverage

blib/lib/Sub/ScopeFinalizer.pm
Criterion Covered Total %
statement 30 30 100.0
branch 4 4 100.0
condition 6 6 100.0
subroutine 8 8 100.0
pod 4 4 100.0
total 52 52 100.0


line stmt bran cond sub pod time code
1             ## ----------------------------------------------------------------------------
2             # Sub::ScopeFinalizer
3             # -----------------------------------------------------------------------------
4             # Mastering programmed by YAMASHINA Hio
5             #
6             # Copyright 2006 YAMASHINA Hio
7             # -----------------------------------------------------------------------------
8             # $Id: /perl/Sub-ScopeFinalizer/lib/Sub/ScopeFinalizer.pm 202 2006-11-03T10:24:44.000948Z hio $
9             # -----------------------------------------------------------------------------
10             package Sub::ScopeFinalizer;
11 4     4   186512 use strict;
  4         10  
  4         153  
12 4     4   23 use warnings;
  4         7  
  4         129  
13 4     4   21 use base qw(Exporter);
  4         22  
  4         2206  
14              
15             our @EXPORT_OK = qw(scope_finalizer);
16              
17             our $VERSION = '0.02';
18              
19             1;
20              
21             # -----------------------------------------------------------------------------
22             # scope_finalizer {CODE;...};
23             # scope_finalizer {CODE;...} { args=>[...] };
24             # shortcut of Sub::ScopeFinalizer->new(...);
25             #
26             sub scope_finalizer(&;@)
27             {
28 8     8 1 7211 Sub::ScopeFinalizer->new(@_);
29             }
30              
31             # -----------------------------------------------------------------------------
32             # Sub::ScopeFinalizer->new(sub{ ... });
33             # Sub::ScopeFinalizer->new(sub{ ... }, { args=>[...] });
34             # create colosing object. it is similar to destructor or finally clause.
35             #
36             sub new
37             {
38 8     8 1 16 my $pkg = shift;
39 8         15 my $code = shift;
40 8         14 my $opts = shift;
41            
42 8         24 my $this = bless {}, $pkg;
43 8         43 $this->{code} = $code;
44 8   100     50 $this->{args} = $opts->{args} || undef;
45 8         18 $this->{disabled} = $opts->{disabled};
46 8         28 $this;
47             }
48              
49             # -----------------------------------------------------------------------------
50             # $obj->raise();
51             # $obj->raise({ args => [...] });
52             # invoke scope_finalizer code before it run automatically.
53             #
54             sub raise
55             {
56 10     10 1 1306 my $this = shift;
57 10   100     53 my $opts = shift || {};
58 10 100       32 if( !$this->{disabled} )
59             {
60 7   100     64 my $args = $opts->{args} || $this->{args} || [];
61 7         26 $this->{code}->(@$args);
62 7         52 $this->{disabled} = 1;
63             }else
64             {
65 3         22 return;
66             }
67             }
68              
69             # -----------------------------------------------------------------------------
70             # $obj->disable();
71             # disable auto raise.
72             #
73             sub disable
74             {
75 3     3 1 12 my $this = shift;
76 3 100       11 $this->{disabled} = @_ ? shift : 1;
77 3         7 $this;
78             }
79              
80             # -----------------------------------------------------------------------------
81             # DESTRUCTOR.
82             # invoke scope_finalizer code.
83             #
84             sub DESTROY
85             {
86 8     8   3548 my $this = shift;
87 8         26 $this->raise();
88             }
89              
90             # -----------------------------------------------------------------------------
91             # End of Module.
92             # -----------------------------------------------------------------------------
93             __END__