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   64100 use strict;
  3         5  
  3         97  
4 3     3   12 use warnings;
  3         5  
  3         94  
5              
6 3     3   13 use Carp qw(confess);
  3         6  
  3         199  
7 3     3   14 use Exporter ();
  3         3  
  3         809  
8              
9             our @ISA = qw(Exporter);
10             our @EXPORT_OK = qw(guard scope_guard);
11             our $VERSION = '0.21';
12              
13             sub new {
14 21 100   21 1 9308 confess "Can't create a Scope::Guard in void context" unless (defined wantarray);
15              
16 18         20 my $class = shift;
17 18   50     35 my $handler = shift() || die 'Scope::Guard::new: no handler supplied';
18 18   50     61 my $ref = ref $handler || '';
19              
20 18 50       39 die "Scope::Guard::new: invalid handler - expected CODE ref, got: '$ref'"
21             unless ref($handler) eq 'CODE';
22              
23 18   33     93 bless [ 0, $handler ], ref $class || $class;
24             }
25              
26             sub dismiss {
27 18     18 1 48 my $self = shift;
28 18 100       34 my $dismiss = @_ ? shift : 1;
29              
30 18         30 $self->[0] = $dismiss;
31             }
32              
33 7     7 1 4357 sub guard(&) { __PACKAGE__->new(shift) }
34 7     7 1 5998 sub scope_guard($) { __PACKAGE__->new(shift) }
35              
36             sub DESTROY {
37 18     18   81 my $self = shift;
38 18         40 my ($dismiss, $handler) = @$self;
39              
40 18 100       60 $handler->() unless ($dismiss);
41             }
42              
43             1;
44              
45             __END__