Sub/Child.pm

filter: Statementstmt 99.1% 123 / 124 Branchbran 93.3% 56 / 60 Conditioncond 89.4% 17 / 19 MC/DCmcdc 83.3% 10 / 12 Subroutinesub 96.3% 26 / 27 Podpod 100% 10 / 10 totaltotal 96.0% 242 / 252 CC 40 SCAR 17.8
CC
40
Cov
97%
__ANON__
12.1
__ANON__
9.0
post
9.0
CRAP 5.9
« Sub.pm ↑ Summary
1 package Wrap::Sub::Child;
2 18 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 18 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 18 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 our $VERSION = '0.06';
12  
13 18 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 144 ▶ *CORE::GLOBAL::caller = sub (;$) {
Subroutine __ANON__: called CC=9 SCAR=22.0
15 144 ▶ my ($height) = ($_[0] || 0);
Condition100%2/2
$_[0] || 0
l!l
12915
MC/DC100%1/1
$_[0] || 0
$_[0]
Truth table100%2/2
A
A= $_[0]
Aresult
11
00
16 144 my $i = 1;
17 144 my $name_cache;
18 144 while (1) {
19 619 ▶ my @caller = CORE::caller($i++) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@caller) = caller $i++ or return 606 13 619
20 606 ▶ $caller[3] = $name_cache if $name_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $name_cache 65 541 606
21 606 ▶ $name_cache = $caller[0] eq 'Wrap::Sub::Child' ? $caller[3] : '';
Branch: 1 branch
BranchTrueFalseTotal
$caller[0] eq 'Wrap::Sub::Child' ? : 65 541 606
22 606 ▶ next if $name_cache || $height-- != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $name_cache or $height-- != 0 475 131 606
Condition100%3/3
$name_cache or $height-- != 0
l!l&&r!l&&!r
65410131
MC/DC100%2/2
$name_cache or $height-- != 0
$name_cache $height-- != 0
Truth table100%3/3
A or B
A= $name_cacheB= $height-- != 0
ABresult
1X1
011
000
23 131 ▶ return wantarray ? @_ ? @caller : @caller[0..2] : $caller[0];
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 36 95 131
@_ ? : 36 0 36
24 }
25 18 };
26 }
27  
28 77 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
29 77 return bless {}, shift;
30 }
31 82 ▶sub _wrap {
Subroutine _wrap: called CC=6 SCAR=17.9
32  
33 82 my $self = shift;
34 82 ▶ my $sub = @_ ? shift : $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 76 6 82
35  
36 82 my %p = @_;
37 82 for (keys %p){
38 0 $self->{$_} = $p{$_};
39 }
40  
41 82 ▶ if (ref $self ne 'Wrap::Sub::Child'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self ne "Wrap::Sub::Child") 1 81 82
42 1 croak "\n_wrap() is not a public method\n";
43 }
44  
45 81 ▶ $sub = "main::$sub" if $sub !~ /::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /::/ 16 65 81
46  
47 81 ▶ if (! exists &$sub){
Branch: 1 branch
BranchTrueFalseTotal
unless (exists &$sub) 3 78 81
48 3 croak "can't wrap() a non-existent sub ($sub). The sub specified " .
49 "does not exist. If you're trying to wrap all subs in a " .
50 "module, you have to load it with 'use' or 'require' first\n";
51 }
52  
53 78 $self->{name} = $sub;
54 78 $self->{orig} = \&$sub;
55  
56 {
57 18 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
58 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 78 my $wrap = $self;
61 78 weaken $wrap;
62  
63 75 ▶ *$sub = sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
64  
65 75 local $Wrap::Sub::name = $wrap->name;
66  
67 75 @{ $wrap->{called_with} } = @_;
68 75 $wrap->{called} = 1;
69  
70 75 my ($pre_return, $post_return) = ([], []);
71  
72 75 ▶ if (defined $wrap->{pre}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrap->{"pre"}) 11 64 75
73 11 $pre_return = [ $wrap->{pre}->(@_) ];
74 11 push @{ $wrap->{wrapper}{pre_returns} }, $pre_return;
75 }
76  
77 75 ▶ my $sub_return = [ $wrap->{orig}->(@_) ] || [];
Condition50%1/2
[$wrap->{'orig'}->(@_)] || []
l!l
750
MC/DC0%0/1
[$wrap->{'orig'}->(@_)] || []
[$wrap->{'orig'}->(@_)]
Truth table50%1/2
A
A= [$wrap->{'orig'}->(@_)]
Aresult
11
00
78  
79 75 ▶ if (defined $wrap->{post}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wrap->{"post"}) 47 28 75
80 47 $post_return = [ $wrap->{post}->($pre_return, $sub_return) ];
81 47 push @{ $wrap->{wrapper}{post_returns} }, $post_return;
82 }
83  
84 75 ▶ $post_return = undef if ! $wrap->{post_return};
Branch: 1 branch
BranchTrueFalseTotal
unless $wrap->{"post_return"} 36 39 75
85  
86 75 ▶ if (! $wrap->{pre} && ! $wrap->{post}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $wrap->{'pre'} || $wrap->{'post'}) { } 26 49 75
Condition100%3/3
$wrap->{'pre'} || $wrap->{'post'}
l!l&&r!l&&!r
113826
MC/DC100%2/2
$wrap->{'pre'} || $wrap->{'post'}
$wrap->{'pre'} $wrap->{'post'}
Truth table100%3/3
A || B
A= $wrap->{'pre'}B= $wrap->{'post'}
ABresult
1X1
011
000
87 26 ▶ if (! wantarray){
Branch: 1 branch
BranchTrueFalseTotal
if (not wantarray) { } 18 8 26
88 18 return $sub_return->[0];
89 }
90 else {
91 8 return @$sub_return;
92 }
93 }
94 else {
95 49 ▶ if (defined $post_return->[0] && $wrap->{post_return}){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $post_return->[0] and $wrap->{'post_return'}) { } 37 12 49
Condition66%2/3
defined $post_return->[0] and $wrap->{"post_return"}
!ll&&!rl&&r
12037
MC/DC50%1/2
defined $post_return->[0] and $wrap->{"post_return"}
defined $post_return->[0] $wrap->{"post_return"}
Truth table66%2/3
A and B
A= defined $post_return->[0]B= $wrap->{"post_return"}
ABresult
0X0
100
111
96 37 ▶ return wantarray ? @$post_return : $post_return->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 37 37
97 }
98 else {
99 12 ▶ return wantarray ? @$sub_return : $sub_return->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 12 12
100 }
101 }
102 78 };
103 }
104  
105 78 $self->{state} = 1;
106  
107 78 return $self;
108 }
109 7 ▶sub rewrap {
Subroutine rewrap: called CC=2 SCAR=6.9
Pod documented
110 7 my $self = shift;
111  
112 7 ▶ if ($self->is_wrapped){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_wrapped) 1 6 7
113 1 croak "\ncan't call rewrap() on an already wrapped sub. Either call " .
114 "unwrap() first, or DESTROY() the object\n";
115 }
116 6 $self->_wrap;
117 }
118 83 ▶sub unwrap {
Subroutine unwrap: called CC=3 SCAR=11.0
Pod documented
119 83 my $self = shift;
120 83 my $sub = $self->{name};
121  
122 {
123 18 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
124 18 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
125  
126 83 ▶ if (defined $self->{orig}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'orig'}) { } 81 2 83
127 81 *$sub = \&{ $self->{orig} };
128 }
129 else {
130 2 ▶ undef *$sub if $self->{name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"name"} 0 2 2
131 }
132 }
133  
134 83 $self->{state} = 0;
135 83 $self->reset;
136 }
137 7 ▶sub called {
Subroutine called: called CC=1 SCAR=0.0
Pod documented
138 7 return $_[0]->{called};
139 }
140 6 ▶sub called_with {
Subroutine called_with: called CC=2 SCAR=6.9
Pod documented
141 6 my $self = shift;
142 6 ▶ if (! $self->called){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->called) 1 5 6
143 1 croak "\n\ncan't call called_with() before the wrapped sub has " .
144 "been called. ";
145 }
146 5 return @{ $self->{called_with} };
147 }
148 108 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
149 108 return shift->{name};
150 }
151 84 ▶sub reset {
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
152 84 for (qw(pre post post_return post_returns called called_with)){
153 504 delete $_[0]->{$_};
154 }
155 }
156 12 ▶sub pre {
Subroutine pre: called CC=1 SCAR=0.0
Pod documented
157 12 $_[0]->_check_wrap($_[1], 'pre');
158 11 $_[0]->{pre} = $_[1];
159 }
160 28 ▶sub post {
Subroutine post: called CC=9 SCAR=22.0
Pod documented
161 28 my $self = shift;
162  
163 28 ▶ if (! defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 2 26 28
164 2 $self->{post} = undef;
165 2 return;
166 }
167  
168 26 my @args = @_;
169 26 my ($cref, %p);
170  
171 26 ▶ if (ref $args[0] eq 'CODE'){
Branch: 3 branches
BranchTrueFalseTotal
if (ref $args[0] eq 'CODE') { } 23 3 26
elsif ($args[0] eq 'post_return' and ref $args[2] eq 'CODE') { } 1 2 3
elsif ($args[0] eq 'post_return') { } 1 1 2
172 23 $cref = shift;
173 23 ▶ %p = @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 14 9 23
174 }
175 ▶ elsif ($args[0] eq 'post_return' && ref $args[2] eq 'CODE'){
Condition100%3/3
$args[0] eq "post_return" and ref $args[2] eq "CODE"
!ll&&!rl&&r
111
MC/DC100%2/2
$args[0] eq "post_return" and ref $args[2] eq "CODE"
$args[0] eq "post_return" ref $args[2] eq "CODE"
Truth table100%3/3
A and B
A= $args[0] eq "post_return"B= ref $args[2] eq "CODE"
ABresult
0X0
100
111
176 1 $cref = pop @args;
177 1 %p = @args;
178 }
179 elsif ($args[0] eq 'post_return'){
180 1 %p = @args;
181 }
182 else {
183 1 croak "invalid parameters to post()";
184 }
185  
186 25 ▶ $self->{post} = $cref if $cref;
Branch: 1 branch
BranchTrueFalseTotal
if $cref 24 1 25
187  
188 25 ▶ $self->{post_return} = $p{post_return} if defined $p{post_return};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p{"post_return"} 16 9 25
189 }
190 12 ▶sub _check_wrap {
Subroutine _check_wrap: called CC=3 SCAR=11.0
191 12 ▶ if (defined $_[1] && ref $_[1] ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1] and ref $_[1] ne "CODE") 1 11 12
Condition100%3/3
defined $_[1] and ref $_[1] ne "CODE"
!ll&&!rl&&r
1101
MC/DC100%2/2
defined $_[1] and ref $_[1] ne "CODE"
defined $_[1] ref $_[1] ne "CODE"
Truth table100%3/3
A and B
A= defined $_[1]B= ref $_[1] ne "CODE"
ABresult
0X0
100
111
192 1 croak "\n\nwrap()'s '$_[2]' parameter must be code a reference.";
193 }
194 }
195 61 ▶sub is_wrapped {
Subroutine is_wrapped: called CC=1 SCAR=0.0
Pod documented
196 61 return shift->{state};
197 }
198 75 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
199 75 $_[0]->unwrap;
200 }
201 0 ▶sub _end {}; # vim fold placeholder
Subroutine _end: not called CC=1 SCAR=0.0
202  
203 __END__
« Sub.pm ↑ Summary