autodie/Scope/GuardStack.pm

filter: Statementstmt 100% 36 / 36 Branchbran 75.0% 3 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 94.5% 52 / 55 CC 4 SCAR 5.5
CC
4
Cov
95%
__ANON__
3.0
DESTROY
2.0
push_hook
1.0
CRAP 1.7
« autodie.pm ↑ Summary autodie/skip.pm »
1 package autodie::Scope::GuardStack;
2  
3 62 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 62 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 62 ▶use autodie::Scope::Guard;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # ABSTRACT: Hook stack for managing scopes via %^H
9 61 our $VERSION = '2.37'; # VERSION
10  
11 61 my $H_KEY_STEM = __PACKAGE__ . '/guard';
12 61 my $COUNTER = 0;
13  
14 # This code schedules the cleanup of subroutines at the end of
15 # scope. It's directly inspired by chocolateboy's excellent
16 # Scope::Guard module.
17  
18 112 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19 112 my ($class) = @_;
20  
21 112 return bless([], $class);
22 }
23  
24 142 ▶sub push_hook {
Subroutine push_hook: called CC=1 SCAR=0.0
Pod documented
25 142 my ($self, $hook) = @_;
26 142 my $h_key = $H_KEY_STEM . ($COUNTER++);
27 142 my $size = @{$self};
28 137 ▶ $^H{$h_key} = autodie::Scope::Guard->new(sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
29 # Pop the stack until we reach the right size
30 # - this may seem weird, but it is to avoid relying
31 # on "destruction order" of keys in %^H.
32 #
33 # Example:
34 # {
35 # use autodie; # hook 1
36 # no autodie; # hook 2
37 # use autodie; # hook 3
38 # }
39 #
40 # Here we want call hook 3, then hook 2 and finally hook 1.
41 # Any other order could have undesired consequences.
42 #
43 # Suppose hook 2 is destroyed first, it will pop hook 3 and
44 # then hook 2. hook 3 will then be destroyed, but do nothing
45 # since its "frame" was already popped and finally hook 1
46 # will be popped and take its own frame with it.
47 #
48 # We need to check that $self still exists since things can get weird
49 # during global destruction.
50 137 ▶ $self->_pop_hook while $self && @{$self} > $size;
Branch: 1 branch
BranchTrueFalseTotal
$self and @{$self;} > $size and do { $self->_pop_hook } 137 137 274
Condition66%2/3
$self and @{$self;} > $size
!ll&&!rl&&r
0137137
MC/DC50%1/2
$self and @{$self;} > $size
$self @{$self;} > $size
Truth table66%2/3
A and B
A= $selfB= @{$self;} > $size
ABresult
0X0
100
111
51 142 });
52 142 push(@{$self}, [$hook, $h_key]);
53 142 return;
54 }
55  
56 137 ▶sub _pop_hook {
Subroutine _pop_hook: called CC=1 SCAR=0.0
57 137 my ($self) = @_;
58 137 my ($hook, $key) = @{ pop(@{$self}) };
59 137 my $ref = delete($^H{$key});
60 137 $hook->();
61 137 return;
62 }
63  
64 107 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.0
65 107 my ($self) = @_;
66  
67 # To be honest, I suspect @{$self} will always be empty here due
68 # to the subs in %^H having references to the stack (which would
69 # keep the stack alive until those have been destroyed). Anyhow,
70 # it never hurt to be careful.
71 107 ▶ $self->_pop_hook while @{$self};
Branch: 1 branch
BranchTrueFalseTotal
@{$self;} and do { $self->_pop_hook } 0 107 107
72 107 return;
73 }
74  
75 61 1;
76  
77 __END__
« autodie.pm ↑ Summary autodie/skip.pm »