blib/lib/Devel/Caller.pm

filter: Statementstmt 85.1% 92 / 108 Branchbran 57.9% 51 / 88 Conditioncond 64.2% 9 / 14 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 12 / 12 Podpod 80.0% 4 / 5 totaltotal 72.8% 172 / 236 CC 50 SCAR 41.0
CC
50
Cov
72%
called_with
101.1
called_as_method
8.7
scan_forward
8.5
CRAP 60.5
↑ Summary Caller.xs »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Devel::Caller;
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use B qw( peekop );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use PadWalker ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XSLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = '2.07';
11 1 XSLoader::load __PACKAGE__, $VERSION;
12  
13 1 our @EXPORT_OK = qw( caller_cv caller_args caller_vars called_with called_as_method );
14  
15 52 ▶sub caller_cv {
Subroutine caller_cv: called CC=2 SCAR=6.9
Pod documented
16 52 my $level = shift;
17 52 my $cx = PadWalker::_upcontext($level + 1);
18 52 ▶ return unless $cx;
Branch: 1 branch
BranchTrueFalseTotal
unless $cx 44 8 52
19 8 return _context_cv($cx);
20 }
21  
22 1 our $DEBUG = 0;
23  
24 # scan forward through the ops noting the pushmark or a padrange ops.
25 # These indicate the start of a subroutine call. We're looking for the most
26 # recent one before the subroutine invocation (the entersub).
27 47 ▶sub scan_forward {
Subroutine scan_forward: called CC=8 SCAR=21.4
Pod undocumented
28 47 my $op = shift;
29 47 ▶ die "was expecting a pushmark or a padrange, not a " . $op->name
Branch: 1 branch
BranchTrueFalseTotal
unless $op->name =~ /^(?:pushmark|padrange)$/ 0 47 47
30 if ($op->name !~ /^(?:pushmark|padrange)$/);
31  
32 47 my @stack;
33 47 ▶ for (; $op && $op->name ne 'entersub'; $op = $op->next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op and $op->name ne "entersub") 313 47 360
Condition66%2/3
$op and $op->name ne "entersub"
!ll&&!rl&&r
047313
MC/DC50%1/2
$op and $op->name ne "entersub"
$op $op->name ne "entersub"
Truth table66%2/3
A and B
A= $opB= $op->name ne "entersub"
ABresult
0X0
100
111
34 313 ▶ print "SCAN ", peekop($op), "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 313 313
35 313 ▶ if ($op->name eq "pushmark" or $op->name eq "padrange") {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->name eq 'pushmark' or $op->name eq 'padrange') { } 97 216 313
Condition66%2/3
$op->name eq "pushmark" or $op->name eq "padrange"
l!l&&r!l&&!r
970216
MC/DC50%1/2
$op->name eq "pushmark" or $op->name eq "padrange"
$op->name eq "pushmark" $op->name eq "padrange"
Truth table66%2/3
A or B
A= $op->name eq "pushmark"B= $op->name eq "padrange"
ABresult
1X1
011
000
36 97 ▶ print " PUSH\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 97 97
37 97 push @stack, $op;
38 }
39 elsif (0) { # op consumes a mark
40 print " POP\n" if $DEBUG;
41 pop @stack;
42 }
43 }
44 47 return pop @stack;
45 }
46  
47 1 *caller_vars = \&called_with;
48 47 ▶sub called_with {
Subroutine called_with: called CC=35 SCAR=46.2
Pod documented
49 47 my $level = shift;
50 47 my $want_names = shift;
51  
52 47 my $op = _context_op( PadWalker::_upcontext( $level + 1 ));
53 47 my $cv = caller_cv( $level + 2 );
54 47 ▶ my $pad = $cv ? B::svref_2object( $cv )->PADLIST : B::comppadlist;
Branch: 1 branch
BranchTrueFalseTotal
$cv ? : 3 44 47
55 47 my $padn = $pad->ARRAYelt( 0 );
56 47 my $padv = $pad->ARRAYelt( 1 );
57  
58 47 ▶ print "Context OP: ", peekop($op), "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 47 47
59 47 $op = scan_forward( $op );
60 47 ▶ print "Scanned forward to ", peekop($op), "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 47 47
61  
62 47 my @return;
63 my $prev;
64  
65 # We're scanning through looking for ops which are pushing
66 # variables onto the stack (/pad(sv|av|hv)/ push from the pad,
67 # /gvsv|rv2([ahg]v/ are from globs.
68 47 ▶ for (; $op && $op->name ne 'entersub'; ($prev = $op) && ($op = $op->next)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op and $op->name ne "entersub") 224 47 271
Condition66%2/3
$op and $op->name ne "entersub"
!ll&&!rl&&r
047224
MC/DC50%1/2
$op and $op->name ne "entersub"
$op $op->name ne "entersub"
Truth table66%2/3
A and B
A= $opB= $op->name ne "entersub"
ABresult
0X0
100
111
69 224 ▶ printf "Loop: %s %s targ: %d\n", peekop($op), $op->name, $op->targ if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 224 224
70  
71 224 ▶ if ($op->name eq "padrange") {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->name eq "padrange") 0 224 224
72 # A padrange is a 5.17 optimisation that uses a single op to
73 # load multiple pad variables onto the stack. The old ops
74 # are preserved and are reachable as the padrange's sibling
75 # so that B::Deparse can pessimise it back to that state.
76 #
77 # http://perl5.git.perl.org/perl.git/commitdiff/0fe870f5
78 # http://perl5.git.perl.org/perl.git/commitdiff/a7fd8ef6
79 #
80 # We could use the B::Deparse method, but it's probably simpler if
81 # we just reassign $op.
82 0 ▶ print "padrange, diverting down ", $prev->sibling, "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
83 0 $op = $op->sibling;
84 }
85  
86 224 ▶ if ($op->name =~ /padsv_store/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($op->name =~ /padsv_store/) { } 0 224 224
elsif ($op->name =~ /pad(sv|av|hv)/) { } 50 174 224
elsif ($op->name =~ /gvsv|rv2(av|hv|gv)/) { } 23 151 174
elsif ($op->name eq 'const') { } 6 145 151
87 # A padsv_store is a 5.37 optimization that combines a padsv and
88 # an sassign into a single op. The new op steals the targ slot
89 # of the original padsv.
90 #
91 # https://github.com/Perl/perl5/commit/9fdd7fc
92 0 ▶ print "Copying from pad\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
93 0 ▶ if ($want_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_names) { } 0 0 0
94 0 push @return, $padn->ARRAYelt( $op->targ )->PVX;
95 }
96 else {
97 0 push @return, $padv->ARRAYelt( $op->targ )->object_2svref;
98 }
99 0 next;
100 }
101 elsif ($op->name =~ "pad(sv|av|hv)") {
102 50 ▶ if ($op->next->next->name eq "sassign") {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->next->next->name eq 'sassign') { } 6 44 50
elsif ($op->next->name eq 'padsv_store') { } 0 44 44
103 6 ▶ print "sassign in two ops, this is the target skipping\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 6 6
104 6 next;
105 } elsif ($op->next->name eq "padsv_store") {
106 0 ▶ print "padsv_store in one op, this is the target, skipping\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
107 0 next;
108 }
109  
110 44 ▶ print "Copying from pad\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 44 44
111 44 ▶ if ($want_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_names) { } 41 3 44
112 41 push @return, $padn->ARRAYelt( $op->targ )->PVX;
113 }
114 else {
115 3 push @return, $padv->ARRAYelt( $op->targ )->object_2svref;
116 }
117 44 next;
118 }
119 elsif ($op->name =~ /gvsv|rv2(av|hv|gv)/) {
120 23 ▶ if ($op->next->next->name eq "sassign") {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->next->next->name eq "sassign") 0 23 23
121 0 ▶ print "sassign in two ops, this is the target, skipping\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
122 0 next;
123 }
124  
125 23 ▶ my $consider = ($op->name eq "gvsv") ? $op : $prev;
Branch: 1 branch
BranchTrueFalseTotal
$op->name eq 'gvsv' ? : 6 17 23
126 23 my $gv;
127  
128 23 ▶ if (ref $consider eq 'B::PADOP') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $consider eq 'B::PADOP') { } 0 23 23
129 0 ▶ print "GV is really a padgv\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
130 0 $gv = $padv->ARRAYelt( $consider->padix );
131 0 ▶ print "NEW GV $gv\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
132 }
133 else {
134 23 $gv = $consider->gv;
135 }
136  
137 23 ▶ print "consider: $consider ", $consider->name, " gv $gv\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 23 23
138 if $DEBUG;
139  
140 23 ▶ if ($want_names) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_names) { } 20 3 23
141 20 my %sigils = (
142 "gvsv" => '$',
143 "rv2av" => '@',
144 "rv2hv" => '%',
145 "rv2gv" => '*',
146 );
147  
148 20 push @return, $sigils{ $op->name } . $gv->STASH->NAME . "::" . $gv->SAFENAME;
149 }
150 else {
151 3 my %slots = (
152 "gvsv" => 'SCALAR',
153 "rv2av" => 'ARRAY',
154 "rv2hv" => 'HASH',
155 "rv2gv" => 'GLOB',
156 );
157 3 push @return, *{ $gv->object_2svref }{ $slots{ $op->name} };
158 }
159  
160 23 next;
161 }
162 elsif ($op->name eq "const") {
163 6 ▶ if ($op->next->next->name eq "sassign") {
Branch: 2 branches
BranchTrueFalseTotal
if ($op->next->next->name eq 'sassign') { } 3 3 6
elsif ($op->next->name eq 'padsv_store') { } 0 3 3
164 3 ▶ print "sassign in two ops, this is the target, skipping\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 3 3
165 3 next;
166 } elsif ($op->next->name eq "padsv_store") {
167 0 ▶ print "padsv_store in one op, this is the target, skipping\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 0 0
168 0 next;
169 }
170  
171 3 ▶ push @return, $want_names ? undef : $op->sv;
Branch: 1 branch
BranchTrueFalseTotal
$want_names ? : 3 0 3
172 3 ▶ next;
Branch: 1 branch
BranchTrueFalseTotal
$prev = $op and $op = $op->next 224 0 224
173 }
174 }
175 47 return @return;
176 }
177  
178  
179 3 ▶sub called_as_method {
Subroutine called_as_method: called CC=8 SCAR=21.6
Pod documented
180 3 ▶ my $level = shift || 0;
Condition50%1/2
shift() || 0
l!l
03
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
181 3 my $op = _context_op( PadWalker::_upcontext( $level + 1 ));
182  
183 3 ▶ print "called_as_method: $op\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 3 3
184 3 ▶ die "was expecting a pushmark or pad, not a ". $op->name
Branch: 1 branch
BranchTrueFalseTotal
unless $op->name eq "pushmark" 0 3 3
185 unless $op->name eq "pushmark";
186 3 ▶ while (($op = $op->next) && ($op->name ne "entersub")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op = $op->next and $op->name ne "entersub") 6 1 7
Condition66%2/3
$op = $op->next and $op->name ne "entersub"
!ll&&!rl&&r
016
MC/DC50%1/2
$op = $op->next and $op->name ne "entersub"
$op = $op->next $op->name ne "entersub"
Truth table66%2/3
A and B
A= $op = $op->nextB= $op->name ne "entersub"
ABresult
0X0
100
111
187 6 ▶ print "method: ", $op->name, "\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Devel::Caller::DEBUG 0 6 6
188 6 ▶ return 1 if $op->name =~ /^method(?:_named)?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $op->name =~ /^method(?:_named)?$/ 2 4 6
189 }
190 1 return;
191 }
192  
193  
194 1 ▶sub caller_args {
Subroutine caller_args: called CC=1 SCAR=0.0
Pod documented
195 1 my $level = shift;
196 package DB;
197 1 () = caller( $level + 1 );
198 return @DB::args
199 1 }
200  
201 1 1;
202 __END__
↑ Summary Caller.xs »