File Coverage

blib/lib/Scope/Guard.pm
Criterion Covered Total %
statement 26 26 100.0
branch 7 8 87.5
condition 3 7 42.8
subroutine 9 9 100.0
pod 4 4 100.0
total 49 54 90.7


line stmt bran cond sub pod time code
1             package Scope::Guard;
2              
3 3     3   98628 use strict;
  3         8  
  3         126  
4 3     3   16 use warnings;
  3         5  
  3         100  
5              
6 3     3   16 use Carp qw(confess);
  3         11  
  3         276  
7 3     3   16 use Exporter ();
  3         6  
  3         11123  
8              
9             our @ISA = qw(Exporter);
10             our @EXPORT_OK = qw(guard scope_guard);
11             our $VERSION = '0.20';
12              
13             sub new {
14 21 100   21 1 8174 confess "Can't create a Scope::Guard in void context" unless (defined wantarray);
15              
16 18         26 my $class = shift;
17 18   50     42 my $handler = shift() || die 'Scope::Guard::new: no handler supplied';
18 18   50     58 my $ref = ref $handler || '';
19              
20 18 50       66 die "Scope::Guard::new: invalid handler - expected CODE ref, got: '$ref'"
21             unless (UNIVERSAL::isa($handler, 'CODE'));
22              
23 18   33     131 bless [ 0, $handler ], ref $class || $class;
24             }
25              
26             sub dismiss {
27 18     18 1 58 my $self = shift;
28 18 100       45 my $dismiss = @_ ? shift : 1;
29              
30 18         38 $self->[0] = $dismiss;
31             }
32              
33 7     7 1 6914 sub guard(&) { __PACKAGE__->new(shift) }
34 7     7 1 8873 sub scope_guard($) { __PACKAGE__->new(shift) }
35              
36             sub DESTROY {
37 18     18   101 my $self = shift;
38 18         45 my ($dismiss, $handler) = @$self;
39              
40 18 100       103 $handler->() unless ($dismiss);
41             }
42              
43             1;
44              
45             __END__