blib/lib/B/Utils1.pm

filter: Statementstmt 60.2% 168 / 279 Branchbran 48.7% 77 / 158 Conditioncond 47.6% 40 / 84 MC/DCmcdc 34.0% 16 / 47 Subroutinesub 71.4% 35 / 49 Podpod 100% 4 / 4 totaltotal 54.7% 340 / 621 CC 129 SCAR 55.2
CC
129
Cov
55%
opgrep
873.5
as_opgrep_pattern
182.0
siblings
30.0
CRAP 250.1
↑ Summary blib/lib/B/Utils1/Install/IFiles.pm »
1 package B::Utils1;
2  
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use vars qw( @EXPORT_OK %EXPORT_TAGS
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 @bad_stashes $TRACE_FH $file $line $sub $trace_removed );
8  
9 8 ▶use subs (
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 qw( all_starts all_roots anon_sub recalc_sub_cache ),
11 qw( walkoptree_simple walkoptree_filtered ),
12 qw( walkallops_simple walkallops_filtered ),
13 qw( opgrep op_or ),
14 8 );
15 sub croak (@);
16 sub carp (@);
17  
18 8 ▶use Scalar::Util qw( weaken blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 =head1 NAME
21  
22 B::Utils1 - Helper functions for op tree manipulation
23  
24 =cut
25  
26 =head1 VERSION
27  
28 1.05
29  
30 =cut
31  
32 8 our $VERSION = '1.05';
33  
34 8 ▶use base 'DynaLoader';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 bootstrap B::Utils1 $VERSION;
36 8 ▶sub dl_load_flags {0x01}
Subroutine dl_load_flags: called CC=1 SCAR=0.0
Pod documented
37  
38 =head1 SYNOPSIS
39  
40 use B::Utils1;
41  
42 =cut
43  
44 8 ▶use B qw( OPf_KIDS main_start main_root walksymtable class main_cv ppname );
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 8 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
47 8 @EXPORT_OK = qw(all_starts all_roots anon_subs
48 walkoptree_simple walkoptree_filtered
49 walkallops_simple walkallops_filtered
50 recalc_sub_cache
51 opgrep op_or );
52 8 %EXPORT_TAGS = ( all => \@EXPORT_OK );
53 8 *import = \&Exporter::import;
54  
55 @bad_stashes
56 8 = qw(B Carp Exporter warnings Cwd Config CORE blib strict DynaLoader vars XSLoader AutoLoader base);
57  
58 8 ▶use List::Util qw( shuffle );
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 8 ▶BEGIN {
Subroutine BEGIN: called CC=3 SCAR=11.2
61 # Fake up a TRACE constant and set $TRACE_FH
62 8 ▶ BEGIN { $^W = 0 }
Subroutine BEGIN: called CC=1 SCAR=0.0
63 8 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 8 eval 'sub _TRACE () {' . ( 0 + $ENV{B_UTILS_TRACE} ) . '}';
65 8 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 8 8
66 8 ▶ $TRACE_FH ||= \*STDOUT;
Condition50%1/2
$B::Utils1::TRACE_FH ||= \*STDOUT
l!l
08
MC/DC0%0/1
$B::Utils1::TRACE_FH ||= \*STDOUT
$B::Utils1::TRACE_FH
Truth table50%1/2
A
A= $B::Utils1::TRACE_FH
Aresult
11
00
67 }
68 sub _TRUE () { !!1 }
69 sub _FALSE () { !!0 }
70  
71 =head1 OP METHODS
72  
73 =over 4
74  
75 =cut
76  
77 # The following functions have been removed because it turns out that
78 # this breaks stuff like B::Concise which depends on ops lacking
79 # methods they wouldn't normally have.
80 #
81 # =pod
82 #
83 # =item C<$op-E<gt>first>
84 #
85 # =item C<$oo-E<gt>last>
86 #
87 # =item C<$op-E<gt>other>
88 #
89 # Normally if you call first, last or other on anything which is not an
90 # UNOP, BINOP or LOGOP respectively it will die. This leads to lots of
91 # code like:
92 #
93 # $op->first if $op->can('first');
94 #
95 # B::Utils1 provided every op with first, last and other methods which
96 # will simply returned nothing if it isn't relevent. But this broke B::Concise
97 #
98 # =cut
99 #
100 # sub B::OP::first { $_[0]->can("SUPER::first") ? $_[0]->SUPER::first() : () }
101 # sub B::OP::last { $_[0]->can("SUPER::last") ? $_[0]->SUPER::last() : () }
102 # sub B::OP::other { $_[0]->can("SUPER::other") ? $_[0]->SUPER::other() : () }
103  
104 =item C<$op-E<gt>oldname>
105  
106 Returns the name of the op, even if it is currently optimized to null.
107 This helps you understand the structure of the op tree.
108  
109 =cut
110  
111 96479 ▶sub B::OP::oldname {
Subroutine oldname: called CC=3 SCAR=11.0
112 96479 my $op = shift;
113 96479 my $name = $op->name;
114 96479 my $targ = $op->targ;
115  
116 # This is a an operation which *used* to be a real op but was
117 # optimized away. Fetch the old value and ignore the leading pp_.
118  
119 # I forget why the original pp # is located in the targ field.
120 96479 ▶ return $name eq 'null' && $targ
Branch: 1 branch
BranchTrueFalseTotal
$name eq 'null' && $targ ? : 19941 76538 96479
Condition100%3/3
$name eq "null" and $targ
!ll&&!rl&&r
70991554719941
MC/DC100%2/2
$name eq "null" and $targ
$name eq "null" $targ
Truth table100%3/3
A and B
A= $name eq "null"B= $targ
ABresult
0X0
100
111
121 ? substr( ppname($targ), 3 )
122 : $name;
123  
124 }
125  
126 =item C<$op-E<gt>kids>
127  
128 Returns an array of all this op's non-null children, in order.
129  
130 =cut
131  
132 61024 ▶sub B::OP::kids {
Subroutine kids: called CC=9 SCAR=22.6
133 61024 my $op = shift;
134 61024 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 61024 61024
135  
136 61024 my @kids;
137 61024 ▶ if ( ref $op and $$op and $op->flags & OPf_KIDS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $op and $$op and $op->flags & 4) { } 56612 4412 61024
Condition50%3/6
ref $op and $$op and $op->flags & 4
!ll&&!rl&&r
0441256612
ref $op and $$op
!ll&&!rl&&r
0061024
MC/DC33%1/3
ref $op and $$op and $op->flags & 4
ref $op $$op $op->flags & 4
Truth table50%2/4
A and B and C
A= ref $opB= $$opC= $op->flags & 4
ABCresult
0XX0
10X0
1100
1111
138 56612 ▶ for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$kid) 114955 56612 171567
139 114955 push @kids, $kid;
140 }
141 ### Assert: $op->children == @kids
142 }
143 else {
144 4412 ▶ @kids = (
Branch: 3 branches
BranchTrueFalseTotal
$op->can('first') ? : 148 4264 4412
$op->can('last') ? : 148 4264 4412
$op->can('other') ? : 0 4412 4412
145 ( $op->can('first') ? $op->first : () ),
146 ( $op->can('last') ? $op->last : () ),
147 ( $op->can('other') ? $op->other : () )
148 );
149 }
150 61024 return @kids;
151 }
152  
153 =item C<$op-E<gt>parent>
154  
155 Returns the parent node in the op tree, if possible. Currently
156 "possible" means "if the tree has already been optimized"; that is, if
157 we're during a C<CHECK> block. (and hence, if we have valid C<next>
158 pointers.)
159  
160 In the future, it may be possible to search for the parent before we
161 have the C<next> pointers in place, but it'll take me a while to
162 figure out how to do that.
163  
164 Warning: Since 5.21.2 B comes with it's own version of B::OP::parent
165 which returns either B::NULL or the real parent when ccflags contains
166 -DPERL_OP_PARENT.
167 We patch away this broken B::OP::parent and return again undef if no parent
168 exists. Note that L<B::Utils> returns B::NULL instead.
169  
170 =cut
171  
172 8 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=14.5
173 8 ▶ if ($] >= 5.021002 and exists &B::OP::parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.021002 and exists &B::OP::parent) { } 8 0 8
Condition33%1/3
$] >= "5.021002" and exists &B::OP::parent
!ll&&!rl&&r
008
MC/DC0%0/2
$] >= "5.021002" and exists &B::OP::parent
$] >= "5.021002" exists &B::OP::parent
Truth table33%1/3
A and B
A= $] >= "5.021002"B= exists &B::OP::parent
ABresult
0X0
100
111
174 8 ▶ eval q[
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine parent: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$parent && ref $parent ne 'B::NULL' ? : 91 1 92
Condition66%2/3
$parent and ref $parent ne "B::NULL"
!ll&&!rl&&r
1091
MC/DC50%1/2
$parent and ref $parent ne "B::NULL"
$parent ref $parent ne "B::NULL"
Truth table66%2/3
A and B
A= $parentB= ref $parent ne "B::NULL"
ABresult
0X0
100
111
175 no warnings 'redefine';
176 sub B::OP::parent {
177 my $op = shift;
178 my $parent = $op->_parent_impl( $op, "" );
179 return $parent && ref $parent ne 'B::NULL' ? $parent : undef;
180 }];
181 } else {
182 0 eval q[
183 sub B::OP::parent {
184 my $op = shift;
185 return $op->_parent_impl( $op, "" );
186 }];
187 }
188 8 ▶ if ($] >= 5.021002) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.021002") 8 0 8
189 8 ▶ eval q[
Subroutine kids: not called CC=1 SCAR=0.0
190 sub B::NULL::kids { }
191 ];
192 }
193 }
194  
195 4570 ▶sub B::NULL::_parent_impl { }
Subroutine _parent_impl: called CC=1 SCAR=0.0
196  
197 8835 ▶sub B::OP::_parent_impl {
Subroutine _parent_impl: called CC=8 SCAR=20.8
198 8835 my ( $op, $target, $cx ) = @_;
199  
200 8835 ▶ return if $cx =~ /\b$$op\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $cx =~ /\b$$op\b/ 220 8615 8835
201  
202 8615 for ( $op->kids ) {
203 8055 ▶ if ( $$_ == $$target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$_ == $$target) 91 7964 8055
204 91 return $op;
205 }
206 }
207  
208 return (
209 8524 ▶ $op->sibling->_parent_impl( $target, "$cx$$op S " )
Branch: 2 branches
BranchTrueFalseTotal
$cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ ? : 696 7677 8373
$op->can('first') ? : 4093 4061 8154
Condition100%6/6
$op->sibling->_parent_impl($target, "$cx$$op S ") || ($cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ ? $op->next->_parent_impl($target, "$cx$$op N ") : ()) || ($op->can('first') ? $op->first->_parent_impl($target, "$cx$$op F ") : ())
l!l&&r!l&&!r
3707716438
$op->sibling->_parent_impl($target, "$cx$$op S ") or $cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ ? $op->next->_parent_impl($target, "$cx$$op N ") : ()
l!l&&r!l&&!r
151568317
MC/DC100%3/3
$op->sibling->_parent_impl($target, "$cx$$op S ") || ($cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ ? $op->next->_parent_impl($target, "$cx$$op N ") : ()) || ($op->can('first') ? $op->first->_parent_impl($target, "$cx$$op F ") : ())
$op->sibling->_parent_impl($target, "$cx$$op S ") $cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ ? $op->next->_parent_impl($target, "$cx$$op N ") : () ($op->can('first') ? $op->first->_parent_impl($target, "$cx$$op F ") : ())
Truth table100%4/4
A or B || C
A= $op->sibling->_parent_impl($target, "$cx$$op S ")B= $cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/ ? $op->next->_parent_impl($target, "$cx$$op N ") : ()C= ($op->can('first') ? $op->first->_parent_impl($target, "$cx$$op F ") : ())
ABCresult
1XX1
01X1
0011
0000
210 || (
211 $cx =~ /^(?:-?\d+ S )*(?:-?\d+ N )*$/
212 ? $op->next->_parent_impl( $target, "$cx$$op N " )
213 : ()
214 )
215 || (
216 $op->can('first')
217 ? $op->first->_parent_impl( $target, "$cx$$op F " )
218 : ()
219 )
220 );
221 }
222  
223 =item C<$op-E<gt>ancestors>
224  
225 Returns all parents of this node, recursively. The list is ordered
226 from younger/closer parents to older/farther parents.
227  
228 =cut
229  
230 0 ▶sub B::OP::ancestors {
Subroutine ancestors: not called CC=2 SCAR=17.9
231 0 my @nodes = shift;
232  
233 0 my $parent;
234 0 ▶ push @nodes, $parent while $parent = $nodes[-1]->parent;
Branch: 1 branch
BranchTrueFalseTotal
$parent = $nodes[-1]->parent and do { push @nodes, $parent } 0 0 0
235 0 shift @nodes;
236  
237 0 return @nodes;
238 }
239  
240 =item C<$op-E<gt>descendants>
241  
242 Returns all children of this node, recursively. The list is unordered.
243  
244 =cut
245  
246 0 ▶sub B::OP::descendants {
Subroutine descendants: not called CC=1 SCAR=6.9
247 0 my $node = shift;
248 0 my @nodes;
249 walkoptree_simple( $node,
250 0 ▶ sub { push @nodes, $_ if ${ $_[0] } != $$node } );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if ${$_[0];} != $$node 0 0 0
251 0 return shuffle @nodes;
252 }
253  
254 =item C<$op-E<gt>siblings>
255  
256 Returns all younger siblings of this node. The list is ordered from
257 younger/closer siblings to older/farther siblings.
258  
259 =cut
260  
261 0 ▶sub B::OP::siblings {
Subroutine siblings: not called CC=5 SCAR=34.0
262 0 my @siblings = $_[0];
263  
264 0 my $sibling;
265 0 ▶ push @siblings, $siblings[-1]->sibling while $siblings[-1]->can('sibling');
Branch: 1 branch
BranchTrueFalseTotal
$siblings[-1]->can("sibling") and do { push @siblings, $siblings[-1]->sibling } 0 0 0
266 0 shift @siblings;
267  
268 # Remove any undefined or B::NULL objects
269 pop @siblings while
270 @siblings
271 && !( defined $siblings[-1]
272 0 ▶ && ${$siblings[-1]} );
Branch: 1 branch
BranchTrueFalseTotal
@siblings and not defined $siblings[-1] && ${$siblings[-1];} and do { pop @siblings } 0 0 0
Condition0%0/6
@siblings and not defined $siblings[-1] && ${$siblings[-1];}
!ll&&!rl&&r
000
defined $siblings[-1] && ${$siblings[-1];}
!ll&&!rl&&r
000
MC/DC0%0/3
@siblings and not defined $siblings[-1] && ${$siblings[-1];}
@siblings defined $siblings[-1] ${$siblings[-1];}
Truth table0%0/4
A and not(B && C)
A= @siblingsB= defined $siblings[-1]C= ${$siblings[-1];}
ABCresult
0XX0
1110
10X1
1101
273  
274 0 return @siblings;
275 }
276  
277 =item C<$op-E<gt>previous>
278  
279 Like C< $op-E<gt>next >, but not quite.
280  
281 =cut
282  
283 ## sub B::OP::previous {
284 ## return unless defined wantarray;
285 ##
286 ## my $target = shift;
287 ##
288 ## my $start = $target;
289 ## my (%deadend, $search);
290 ## $search = sub {
291 ## my $node = $_[0];
292 ##
293 ## unless ( defined $node ) {
294 ## # If I've been asked to search nothing, just return. The
295 ## # ->parent call might do this to me.
296 ## return _FALSE;
297 ## }
298 ## elsif ( exists $deadend{$node} ) {
299 ## # If this node has been seen already, try again as its
300 ## # parent.
301 ## return $search->( $node->parent );
302 ## }
303 ## elsif ( eval { ${$node->next} == $$target } ) {
304 ## return $node;
305 ## }
306 ##
307 ## # When searching the children, do it in reverse order because
308 ## # pointers back up are more likely to be farther down the
309 ## # stack. This works without reversing but I can avoid some
310 ## # work by ordering the work this way.
311 ## my @kids = reverse $node->kids;
312 ##
313 ## # Search this node's direct children for the ->next pointer
314 ## # that points to this node.
315 ## eval { ${$_->can('next')} == $$target } and return $_->next
316 ## for @kids;
317 ##
318 ## # For each child, check it for a match.
319 ## my $found;
320 ## $found = $search->($_) and return $found
321 ## for @kids;
322 ##
323 ## # Not in this subtree.
324 ## $deadend{$node} = _TRUE;
325 ## return _FALSE;
326 ## };
327 ##
328 ## my $next = $target;
329 ## while ( eval { $next = $next->next } ) {
330 ## my $result;
331 ## $result = $search->( $next )
332 ## and return $result;
333 ## }
334 ##
335 ## return _FALSE;
336 ## }
337  
338 =item C<$op-E<gt>stringify>
339  
340 Returns a nice stringification of an opcode.
341  
342 =cut
343  
344 183 ▶sub B::OP::stringify {
Subroutine stringify: called CC=1 SCAR=0.0
345 183 my $op = shift;
346  
347 183 return sprintf "%s-%s=(0x%07x)", $op->name, class($op), $$op;
348 }
349  
350 =item C<$op-E<gt>as_opgrep_pattern(%options)>
351  
352 From the op tree it is called on, C<as_opgrep_pattern()>
353 generates a data structure suitable for use as a condition pattern
354 for the C<opgrep()> function described below in detail.
355 I<Beware>: When using such generated patterns, there may be
356 false positives: The pattern will most likely not match I<only>
357 the op tree it was generated from since by default, not all properties
358 of the op are reproduced.
359  
360 You can control which properties of the op to include in the pattern
361 by passing named arguments. The default behaviour is as if you
362 passed in the following options:
363  
364 my $pattern = $op->as_opgrep_pattern(
365 attributes => [qw(name flags)],
366 max_recursion_depth => undef,
367 );
368  
369 So obviously, you can set C<max_recursion_depth> to a number to
370 limit the maximum depth of recursion into the op tree. Setting
371 it to C<0> will limit the dump to the current op.
372  
373 C<attributes> is a list of attributes to include in the produced
374 pattern. The attributes that can be checked against in this way
375 are:
376  
377 name targ type seq flags private pmflags pmpermflags.
378  
379 =cut
380  
381 0 ▶sub B::OP::as_opgrep_pattern {
Subroutine as_opgrep_pattern: not called CC=13 SCAR=52.0
382 0 my $op = shift;
383 0 ▶ my $opt = (@_ == 1 and ref($_[0]) eq 'HASH') ? shift() : {@_};
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
384  
385 0 my $attribs = $opt->{attributes};
386 0 ▶ $attribs ||= [qw(name flags)];
Condition0%0/2
$attribs ||= ["name", "flags"]
l!l
00
MC/DC0%0/1
$attribs ||= ["name", "flags"]
$attribs
Truth table0%0/2
A
A= $attribs
Aresult
11
00
387  
388 0 my $pattern = {};
389 0 foreach my $attr (@$attribs) {
390 0 ▶ $pattern->{$attr} = $op->$attr() if $op->can($attr);
Branch: 1 branch
BranchTrueFalseTotal
if $op->can($attr) 0 0 0
391 }
392  
393 0 my $recursion_limit = $opt->{max_recursion_depth};
394 0 ▶ if ( (not defined $recursion_limit or $recursion_limit > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (!defined($recursion_limit) || $recursion_limit > 0 and ref $op and $$op and $op->flags & 4) 0 0 0
Condition0%0/12
!defined($recursion_limit) || $recursion_limit > 0 and ref $op and $$op and $op->flags & 4
!ll&&!rl&&r
000
!defined($recursion_limit) || $recursion_limit > 0 and ref $op and $$op
!ll&&!rl&&r
000
!defined($recursion_limit) || $recursion_limit > 0 and ref $op
!ll&&!rl&&r
000
not defined $recursion_limit or $recursion_limit > 0
l!l&&r!l&&!r
000
MC/DC0%0/5
!defined($recursion_limit) || $recursion_limit > 0 and ref $op and $$op and $op->flags & 4
not defined $recursion_limit $recursion_limit > 0 ref $op $$op $op->flags & 4
Truth table0%0/9
not(A) or B and C and D and E
A= not defined $recursion_limitB= $recursion_limit > 0C= ref $opD= $$opE= $op->flags & 4
ABCDEresult
00XXX0
1X0XX0
010XX0
1X10X0
0110X0
1X1100
011100
1X1111
011111
395 and ref($op)
396 and $$op
397 and $op->flags & OPf_KIDS
398 ) {
399 0 ▶ $opt->{max_recursion_depth}-- if defined $recursion_limit;
Branch: 1 branch
BranchTrueFalseTotal
if defined $recursion_limit 0 0 0
400  
401 $pattern->{kids} = [
402 0 map { $_->as_opgrep_pattern($opt) } $op->kids()
403 ];
404 }
405  
406 # reset the option structure in case we got a hash ref passed in.
407 $opt->{max_recursion_depth} = $recursion_limit
408 0 ▶ if exists $opt->{max_recursion_depth};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"max_recursion_depth"} 0 0 0
409  
410 0 return $pattern;
411 }
412  
413 =back
414  
415 =head1 EXPORTABLE FUNCTIONS
416  
417 =over 4
418  
419 =item C<all_starts>
420  
421 =item C<all_roots>
422  
423 Returns a hash of all of the starting ops or root ops of optrees, keyed
424 to subroutine name; the optree for main program is simply keyed to C<__MAIN__>.
425  
426 B<Note>: Certain "dangerous" stashes are not scanned for subroutines:
427 the list of such stashes can be found in
428 C<@B::Utils1::bad_stashes>. Feel free to examine and/or modify this to
429 suit your needs. The intention is that a simple program which uses no
430 modules other than C<B> and C<B::Utils1> would show no addition
431 symbols.
432  
433 This does B<not> return the details of ops in anonymous subroutines
434 compiled at compile time. For instance, given
435  
436 $a = sub { ... };
437  
438 the subroutine will not appear in the hash. This is just as well,
439 since they're anonymous... If you want to get at them, use...
440  
441 =cut
442  
443 8 my ( %starts, %roots );
444 2 ▶sub all_starts { _init_sub_cache(); wantarray ? %starts : \%starts }
Subroutine all_starts: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
445 3 ▶sub all_roots { _init_sub_cache(); wantarray ? %roots : \%roots }
Subroutine all_roots: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 2 3
446  
447 =item C<anon_subs>
448  
449 This returns an array of hash references. Each element has the keys
450 "start" and "root". These are the starting and root ops of all of the
451 anonymous subroutines in the program.
452  
453 =cut
454  
455 8 my @anon_subs;
456 2 ▶sub anon_subs { _init_sub_cache(); wantarray ? @anon_subs : \@anon_subs }
Subroutine anon_subs: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 1 2
457  
458 =item C<recalc_sub_cache>
459  
460 If PL_sub_generation has changed or you have some other reason to want
461 to force the re-examination of the optrees, everywhere, call this
462 function.
463  
464 =cut
465  
466 8 my $subs_cached = _FALSE;
467  
468 0 ▶sub recalc_sub_cache {
Subroutine recalc_sub_cache: not called CC=1 SCAR=6.9
469 0 $subs_cached = _FALSE;
470  
471 0 %starts = %roots = @anon_subs = ();
472  
473 0 _init_sub_cache();
474 0 return;
475 }
476  
477 8 ▶sub _init_sub_cache {
Subroutine _init_sub_cache: called CC=3 SCAR=11.0
478  
479 # Allow this function to be run only once.
480 8 ▶ return if $subs_cached;
Branch: 1 branch
BranchTrueFalseTotal
if $subs_cached 3 5 8
481  
482 5 %starts = ( __MAIN__ => main_start() );
483 5 %roots = ( __MAIN__ => main_root() );
484  
485 # Through the magic of B::'s ugly callback system, %starts and
486 # %roots will be populated.
487 walksymtable(
488 \%main::,
489 1205 ▶ _B_Utils_init_sub_cache => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
490  
491 # Do not eat our own children!
492 1205 ▶ $_[0] eq "$_\::" && return _FALSE for @bad_stashes;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq "${_}::" and 65 16355 16420
493  
494 1140 return _TRUE;
495 },
496 5 ''
497 );
498  
499 # Some sort of file-scoped anonymous code refs are found here. In
500 # general, when a function has anonymous functions, they can be
501 # found in the scratchpad.
502 5 ▶ push @anon_subs,
Branch: 1 branch
BranchTrueFalseTotal
'CV' eq B::Utils1::class($_) ? : 4 56 60
503 map( (
504 'CV' eq class($_)
505 ? { root => $_->ROOT,
506 start => $_->START
507 }
508 : ()
509 ),
510 main_cv()->PADLIST->ARRAY->ARRAY );
511  
512 5 $subs_cached = _TRUE;
513 5 return;
514 }
515  
516 20244 ▶sub B::GV::_B_Utils_init_sub_cache {
Subroutine _B_Utils_init_sub_cache: called CC=8 SCAR=21.1
517  
518 # This is a callback function called from B::Utils1::_init via
519 # B::walksymtable.
520  
521 20244 my $gv = shift;
522 20244 my $cv = $gv->CV;
523  
524 # If the B::CV object is a pointer to nothing, ignore it.
525 20244 ▶ return unless $$cv;
Branch: 1 branch
BranchTrueFalseTotal
unless $$cv 4801 15443 20244
526  
527 # Simon was originally using $gv->SAFENAME but I don't think
528 # that's a "correct" decision because then oddly named functions
529 # can't be disambiguated. If a function were actually named ^G, I
530 # couldn't tell it apart from one named after the control
531 # character ^G.
532 15443 my $name = $gv->STASH->NAME . "::" . $gv->NAME;
533  
534 # When does a CV not fulfill ->ARRAY->ARRAY? Some time during
535 # initialization?
536 15443 ▶ if ( $cv->can('PADLIST')
Branch: 1 branch
BranchTrueFalseTotal
if ($cv->can("PADLIST") and $cv->PADLIST->can("ARRAY") and $cv->PADLIST->ARRAY->can("ARRAY")) 9510 5933 15443
Condition83%5/6
$cv->can("PADLIST") and $cv->PADLIST->can("ARRAY") and $cv->PADLIST->ARRAY->can("ARRAY")
!ll&&!rl&&r
5918159510
$cv->can("PADLIST") and $cv->PADLIST->can("ARRAY")
!ll&&!rl&&r
059189525
MC/DC66%2/3
$cv->can("PADLIST") and $cv->PADLIST->can("ARRAY") and $cv->PADLIST->ARRAY->can("ARRAY")
$cv->can("PADLIST") $cv->PADLIST->can("ARRAY") $cv->PADLIST->ARRAY->can("ARRAY")
Truth table75%3/4
A and B and C
A= $cv->can("PADLIST")B= $cv->PADLIST->can("ARRAY")C= $cv->PADLIST->ARRAY->can("ARRAY")
ABCresult
0XX0
10X0
1100
1111
537 and $cv->PADLIST->can('ARRAY')
538 and $cv->PADLIST->ARRAY->can('ARRAY') )
539 {
540 9510 ▶ push @anon_subs,
Branch: 1 branch
BranchTrueFalseTotal
'CV' eq B::Utils1::class($_) ? : 500 109437 109937
541 map( (
542 'CV' eq class($_)
543 ? { root => $_->ROOT,
544 start => $_->START
545 }
546 : ()
547 ),
548 $cv->PADLIST->ARRAY->ARRAY );
549 }
550  
551 15443 ▶ return unless ( ( my $start = $cv->START )
Branch: 1 branch
BranchTrueFalseTotal
unless my $start = $cv->START and my $root = $cv->ROOT 0 15443 15443
Condition33%1/3
my $start = $cv->START and my $root = $cv->ROOT
!ll&&!rl&&r
0015443
MC/DC0%0/2
my $start = $cv->START and my $root = $cv->ROOT
my $start = $cv->START my $root = $cv->ROOT
Truth table33%1/3
A and B
A= my $start = $cv->STARTB= my $root = $cv->ROOT
ABresult
0X0
100
111
552 and ( my $root = $cv->ROOT ) );
553  
554 15443 $starts{$name} = $start;
555 15443 $roots{$name} = $root;
556  
557 # return _TRUE;
558 15443 return;
559 }
560  
561 # sub B::SPECIAL::_B_Utils_init_sub_cache {
562 #
563 # # This is a callback function called from B::Utils1::_init via
564 # # B::walksymtable.
565 #
566 # # JJ: I'm not sure why this callback function exists.
567 #
568 # return _TRUE;
569 # }
570  
571 =item C<walkoptree_simple($op, \&callback, [$data])>
572  
573 The C<B> module provides various functions to walk the op tree, but
574 they're all rather difficult to use, requiring you to inject methods
575 into the C<B::OP> class. This is a very simple op tree walker with
576 more expected semantics.
577  
578 All the C<walk> functions set C<$B::Utils1::file>, C<$B::Utils::line>,
579 and C<$B::Utils1::sub> to the appropriate values of file, line number,
580 and sub name in the program being examined.
581 Sets C<$B::Utils::trace_removed> when the nextstate COPs that contained
582 that line was optimized away. Such lines won't normally be
583 step-able or breakpoint-able in a debugger without special work.
584  
585 =cut
586  
587 8 $B::Utils1::file = '__none__';
588 8 $B::Utils1::line = 0;
589 8 $B::Utils1::sub = undef;
590  
591 3092 ▶sub walkoptree_simple {
Subroutine walkoptree_simple: called CC=1 SCAR=0.0
592 3092 $B::Utils1::file = '__none__';
593 3092 $B::Utils1::line = 0;
594  
595 3092 _walkoptree_simple( {}, @_ );
596  
597 3092 return _TRUE;
598 }
599  
600 109839 ▶sub _walkoptree_simple {
Subroutine _walkoptree_simple: called CC=12 SCAR=25.9
601 109839 my ( $visited, $op, $callback, $data ) = @_;
602  
603 109839 ▶ return if $visited->{$$op}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$visited{$$op}++ 0 109839 109839
604  
605 109839 ▶ if ( ref $op and $op->isa("B::COP") ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $op and $op->isa('B::COP')) { } 12177 97662 109839
elsif (not $op->isa('B::NULL') and $op->oldname =~ /^(next|db)state$/) { } 0 98845 98845
Condition66%2/3
ref $op and $op->isa("B::COP")
!ll&&!rl&&r
09766212177
MC/DC50%1/2
ref $op and $op->isa("B::COP")
ref $op $op->isa("B::COP")
Truth table66%2/3
A and B
A= ref $opB= $op->isa("B::COP")
ABresult
0X0
100
111
606 12177 $B::Utils1::file = $op->file;
607 12177 $B::Utils1::line = $op->line;
608 12177 $B::Utils1::trace_removed = _FALSE;
609 ▶ } elsif ( !$op->isa('B::NULL') and $op->oldname =~ /^(next|db)state$/) {
Condition66%2/3
not $op->isa("B::NULL") and $op->oldname =~ /^(next|db)state$/
!ll&&!rl&&r
1183964790
MC/DC0%0/2
not $op->isa("B::NULL") and $op->oldname =~ /^(next|db)state$/
not $op->isa("B::NULL") $op->oldname =~ /^(next|db)state$/
Truth table66%2/3
not(A) and B
A= not $op->isa("B::NULL")B= $op->oldname =~ /^(next|db)state$/
ABresult
0X0
100
111
610 # COP nextstate has been optimized away. However by turning
611 # this locally back into a COP we can retrieve the file and line
612 # values.
613 0 my $cop = $op;
614 0 bless $cop, 'B::COP';
615 0 $B::Utils1::file = $cop->file;
616 0 $B::Utils1::line = $cop->line;
617 0 $B::Utils1::trace_removed = _TRUE;
618 }
619  
620 109839 $callback->( $op, $data );
621 109839 ▶ return if $op->isa('B::NULL');
Branch: 1 branch
BranchTrueFalseTotal
if $op->isa("B::NULL") 1183 108656 109839
622 108656 ▶ if ( $op->flags & OPf_KIDS ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->flags & 4) 52318 56338 108656
623 # for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
624 # _walkoptree_simple( $visited, $kid, $callback, $data );
625 # }
626 52318 _walkoptree_simple( $visited, $_, $callback, $data ) for $op->kids;
627 }
628 108656 ▶ if ( $op->isa('B::PMOP') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->isa("B::PMOP")) 574 108082 108656
629 574 my $maybe_root = $op->pmreplroot;
630 574 ▶ if (ref($maybe_root) and $maybe_root->isa("B::OP")) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $maybe_root and $maybe_root->isa("B::OP")) 17 557 574
Condition100%3/3
ref $maybe_root and $maybe_root->isa("B::OP")
!ll&&!rl&&r
954817
MC/DC100%2/2
ref $maybe_root and $maybe_root->isa("B::OP")
ref $maybe_root $maybe_root->isa("B::OP")
Truth table100%3/3
A and B
A= ref $maybe_rootB= $maybe_root->isa("B::OP")
ABresult
0X0
100
111
631 # It really is the root of the replacement, not something
632 # else stored here for lack of space elsewhere
633 17 _walkoptree_simple( $visited, $maybe_root, $callback, $data );
634 }
635 }
636  
637 108656 return;
638  
639 }
640  
641 =item C<walkoptree_filtered($op, \&filter, \&callback, [$data])>
642  
643 This is much the same as C<walkoptree_simple>, but will only call the
644 callback if the C<filter> returns true. The C<filter> is passed the
645 op in question as a parameter; the C<opgrep> function is fantastic
646 for building your own filters.
647  
648 =cut
649  
650 3191 ▶sub walkoptree_filtered {
Subroutine walkoptree_filtered: called CC=1 SCAR=0.0
651 3191 $B::Utils1::file = '__none__';
652 3191 $B::Utils1::line = 0;
653  
654 3191 _walkoptree_filtered( {}, @_ );;
655  
656 3191 return _TRUE;
657 }
658  
659 112477 ▶sub _walkoptree_filtered {
Subroutine _walkoptree_filtered: called CC=8 SCAR=20.9
660 112477 my ( $visited, $op, $filter, $callback, $data ) = @_;
661  
662 112477 ▶ if ( $op->isa("B::COP") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op->isa("B::COP")) 12502 99975 112477
663 12502 $B::Utils1::file = $op->file;
664 12502 $B::Utils1::line = $op->line;
665 }
666  
667 112477 ▶ $callback->( $op, $data ) if $filter->($op);
Branch: 1 branch
BranchTrueFalseTotal
if &$filter($op) 0 112477 112477
668  
669 112477 ▶ if ( ref $op
Branch: 1 branch
BranchTrueFalseTotal
if (ref $op and $$op and $op->flags & 4) 53612 58865 112477
Condition83%5/6
ref $op and $$op and $op->flags & 4
!ll&&!rl&&r
11915767453612
ref $op and $$op
!ll&&!rl&&r
01191111286
MC/DC66%2/3
ref $op and $$op and $op->flags & 4
ref $op $$op $op->flags & 4
Truth table75%3/4
A and B and C
A= ref $opB= $$opC= $op->flags & 4
ABCresult
0XX0
10X0
1100
1111
670 and $$op
671 and $op->flags & OPf_KIDS )
672 {
673  
674 53612 my $kid = $op->first;
675 53612 ▶ while ( ref $kid
Branch: 1 branch
BranchTrueFalseTotal
if (ref $kid and $$kid) 109286 53612 162898
Condition66%2/3
ref $kid and $$kid
!ll&&!rl&&r
053612109286
MC/DC50%1/2
ref $kid and $$kid
ref $kid $$kid
Truth table66%2/3
A and B
A= ref $kidB= $$kid
ABresult
0X0
100
111
676 and $$kid )
677 {
678 109286 _walkoptree_filtered( $visited, $kid, $filter, $callback, $data );
679  
680 109286 $kid = $kid->sibling;
681 }
682 }
683  
684 112477 return _TRUE;
685 }
686  
687 =item C<walkallops_simple(\&callback, [$data])>
688  
689 This combines C<walkoptree_simple> with C<all_roots> and C<anon_subs>
690 to examine every op in the program. C<$B::Utils1::sub> is set to the
691 subroutine name if you're in a subroutine, C<__MAIN__> if you're in
692 the main program and C<__ANON__> if you're in an anonymous subroutine.
693  
694 =cut
695  
696 0 ▶sub walkallops_simple {
Subroutine walkallops_simple: not called CC=1 SCAR=6.9
697 0 $B::Utils1::sub = undef;
698  
699 0 &_walkallops_simple;
700  
701 0 return _TRUE;
702 }
703  
704 0 ▶sub _walkallops_simple {
Subroutine _walkallops_simple: not called CC=3 SCAR=24.8
705 0 my ( $callback, $data ) = @_;
706  
707 0 _init_sub_cache();
708  
709 0 for my $sub_name (sort keys %roots) {
710 0 $B::Utils1::sub = $sub_name;
711 0 my $root = $roots{$sub_name};
712 0 walkoptree_simple( $root, $callback, $data );
713 }
714  
715 0 $B::Utils1::sub = "__ANON__";
716 0 walkoptree_simple( $_->{root}, $callback, $data ) for @anon_subs;
717  
718 0 return _TRUE;
719 }
720  
721 =item C<walkallops_filtered(\&filter, \&callback, [$data])>
722  
723 Same as above, but filtered.
724  
725 =cut
726  
727 1 ▶sub walkallops_filtered {
Subroutine walkallops_filtered: called CC=1 SCAR=0.0
728 1 $B::Utils1::sub = undef;
729  
730 1 &_walkallops_filtered;
731  
732 1 return _TRUE;
733 }
734  
735 1 ▶sub _walkallops_filtered {
Subroutine _walkallops_filtered: called CC=3 SCAR=11.0
736 1 my ( $filter, $callback, $data ) = @_;
737  
738 1 _init_sub_cache();
739  
740 1 walkoptree_filtered( $_, $filter, $callback, $data ) for values %roots;
741  
742 1 $B::Utils1::sub = "__ANON__";
743  
744 walkoptree_filtered( $_->{root}, $filter, $callback, $data )
745 1 for @anon_subs;
746  
747 1 return _TRUE;
748 }
749  
750 =item C<opgrep(\%conditions, @ops)>
751  
752 Returns the ops which meet the given conditions. The conditions should
753 be specified like this:
754  
755 @barewords = opgrep(
756 { name => "const", private => OPpCONST_BARE },
757 @ops
758 );
759  
760 where the first argument to C<opgrep()> is the condition to be matched against the
761 op structure. We'll henceforth refer to it as an op-pattern.
762  
763 You can specify alternation by giving an arrayref of values:
764  
765 @svs = opgrep ( { name => ["padsv", "gvsv"] }, @ops)
766  
767 And you can specify inversion by making the first element of the
768 arrayref a "!". (Hint: if you want to say "anything", say "not
769 nothing": C<["!"]>)
770  
771 You may also specify the conditions to be matched in nearby ops as nested patterns.
772  
773 walkallops_filtered(
774 sub { opgrep( {name => "exec",
775 next => {
776 name => "nextstate",
777 sibling => { name => [qw(! exit warn die)] }
778 }
779 }, @_)},
780 sub {
781 carp("Statement unlikely to be reached");
782 carp("\t(Maybe you meant system() when you said exec()?)\n");
783 }
784 )
785  
786 Get that?
787  
788 Here are the things that can be tested in this way:
789  
790 name targ type seq flags private pmflags pmpermflags
791 first other last sibling next pmreplroot pmreplstart pmnext
792  
793 Additionally, you can use the C<kids> keyword with an array reference
794 to match the result of a call to C<$op-E<gt>kids()>. An example use is
795 given in the documentation for C<op_or> below.
796  
797 For debugging, you can have many properties of an op that is currently being
798 matched against a given condition dumped to STDERR
799 by specifying C<dump => 1> in the condition's hash reference.
800  
801 If you match a complex condition against an op tree, you may want to extract
802 a specific piece of information from the tree if the condition matches.
803 This normally entails manually walking the tree a second time down to
804 the op you wish to extract, investigate or modify. Since this is tedious
805 duplication of code and information, you can specify a special property
806 in the pattern of the op you wish to extract to capture the sub-op
807 of interest. Example:
808  
809 my ($result) = opgrep(
810 { name => "exec",
811 next => { name => "nextstate",
812 sibling => { name => [qw(! exit warn die)]
813 capture => "notreached",
814 },
815 }
816 },
817 $root_op
818 );
819  
820 if ($result) {
821 my $name = $result->{notreached}->name; # result is *not* the root op
822 carp("Statement unlikely to be reached (op name: $name)");
823 carp("\t(Maybe you meant system() when you said exec()?)\n");
824 }
825  
826 While the above is a terribly contrived example, consider the win for a
827 deeply nested pattern or worse yet, a pattern with many disjunctions.
828 If a C<capture> property is found anywhere in
829 the op pattern, C<opgrep()> returns an unblessed hash reference on success
830 instead of the tested op. You can tell them apart using L<Scalar::Util>'s
831 C<blessed()>. That hash reference contains all captured ops plus the
832 tested root up as the hash entry C<$result-E<gt>{op}>. Note that you cannot
833 use this feature with C<walkoptree_filtered> since that function was
834 specifically documented to pass the tested op itself to the callback.
835  
836 You cannot capture disjunctions, but that doesn't really make sense anyway.
837  
838 =item C<opgrep( \@conditions, @ops )>
839  
840 Same as above, except that you don't have to chain the conditions
841 yourself. If you pass an array-ref, opgrep will chain the conditions
842 for you using C<next>.
843 The conditions can either be strings (taken as op-names), or
844 hash-refs, with the same testable conditions as given above.
845  
846 =cut
847  
848 112477 ▶sub opgrep {
Subroutine opgrep: called CC=47 SCAR=67.7
849 112477 ▶ return unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 112477 112477
850  
851 112477 my $conds_ref = shift;
852 112477 ▶ $conds_ref = _opgrep_helper($conds_ref)
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $conds_ref 0 112477 112477
853 if 'ARRAY' eq ref $conds_ref;
854  
855 112477 my @grep_ops;
856  
857 # Check whether we're dealing with a disjunction of patterns:
858 0 ▶ my @conditions = exists($conds_ref->{disjunction}) ? @{$conds_ref->{disjunction}} : ($conds_ref);
Branch: 1 branch
BranchTrueFalseTotal
exists $conds_ref->{'disjunction'} ? : 0 112477 112477
859  
860 OP:
861 112477 for my $op (@_) {
862 112477 ▶ next unless ref $op and $$op;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $op and $$op 1191 111286 112477
Condition66%2/3
ref $op and $$op
!ll&&!rl&&r
01191111286
MC/DC50%1/2
ref $op and $$op
ref $op $$op
Truth table66%2/3
A and B
A= ref $opB= $$op
ABresult
0X0
100
111
863  
864 # only one condition by default, but if we have a disjunction, there will
865 # be several
866 CONDITION:
867 111286 foreach my $condition (@conditions) {
868 # nested disjunctions? naughty user!
869 # $foo or ($bar or $baz) is $foo or $bar or $baz!
870 # ==> flatten
871 111286 ▶ if (exists($condition->{disjunction})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $condition->{"disjunction"}) 0 111286 111286
872 0 push @conditions, @{$condition->{disjunction}};
873 0 next CONDITION;
874 }
875  
876 # structure to hold captured information
877 111286 my $capture = {};
878  
879 # Debugging aid
880 111286 ▶ if (exists $condition->{'dump'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $condition->{"dump"}) 0 111286 111286
881 ($op->can($_)
882 or next)
883 and warn "$_: " . $op->$_ . "\n"
884 0 ▶ for
Branch: 1 branch
BranchTrueFalseTotal
$op->can($_) || (next) and warn "${_}: " . $op->$_ . "\n" 0 0 0
Condition0%0/2
$op->can($_) or next
l!l
00
MC/DC0%0/1
$op->can($_) or next
$op->can($_)
Truth table0%0/2
A
A= $op->can($_)
Aresult
11
00
885 qw( first other last pmreplroot pmreplstart pmnext pmflags pmpermflags name targ type seq flags private kids);
886 }
887  
888 # special disjunction case. undef in a disjunction => (child) does not exist
889 111286 ▶ if (not defined $condition) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $condition) 0 111286 111286
890 0 ▶ return _TRUE if not defined $op and not wantarray();
Branch: 1 branch
BranchTrueFalseTotal
unless defined $op or wantarray 0 0 0
Condition0%0/3
defined $op or wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $op or wantarray
defined $op wantarray
Truth table0%0/3
A or B
A= defined $opB= wantarray
ABresult
1X1
011
000
891 0 return();
892 }
893  
894 # save the op if the user wants flat access to it
895 111286 ▶ if ($condition->{capture}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($condition->{"capture"}) 0 111286 111286
896 0 $capture->{ $condition->{capture} } = $op;
897 }
898  
899 # First, let's skim off ops of the wrong type. If they require
900 # something that isn't implemented for this kind of object, it
901 # must be wrong. These tests are cheap
902 exists $condition->{$_}
903 and !$op->can($_)
904 and next
905 111286 ▶ for
Branch: 1 branch
BranchTrueFalseTotal
exists $condition->{$_} and not $op->can($_) and next 0 1669290 1669290
Condition66%2/3
exists $condition->{$_} and not $op->can($_)
!ll&&!rl&&r
15580041112860
MC/DC0%0/2
exists $condition->{$_} and not $op->can($_)
exists $condition->{$_} not $op->can($_)
Truth table66%2/3
A and not(B)
A= exists $condition->{$_}B= not $op->can($_)
ABresult
0X0
100
111
906 qw( first other last pmreplroot pmreplstart pmnext pmflags pmpermflags name targ type seq flags private kids);
907  
908 # # Check alternations
909 # ( ref( $condition->{$_} )
910 # ? ( "!" eq $condition->{$_}[0]
911 # ? ()
912 # : ()
913 # )
914 # : ( $op->can($_) && $op->$_ eq $condition->{$_} or next )
915 # )
916 # for qw( name targ type seq flags private pmflags pmpermflags );
917  
918 111286 for my $test (
919 qw(name targ type seq flags private pmflags pmpermflags))
920 {
921 111286 ▶ next unless exists $condition->{$test};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $condition->{$test} 0 111286 111286
922 111286 my $val = $op->$test;
923  
924 111286 ▶ if ( 'ARRAY' eq ref $condition->{$test} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('ARRAY' eq ref $condition->{$test}) { } 0 111286 111286
elsif ('CODE' eq ref $condition->{$test}) { } 0 111286 111286
925  
926 # Test a list of valid/invalid values.
927 0 ▶ if ( '!' eq $condition->{$test}[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('!' eq $condition->{$test}[0]) { } 0 0 0
928  
929 # Fail if any entries match.
930 $_ ne $val
931 or next CONDITION
932 0 ▶ for @{ $condition->{$test} }
Branch: 1 branch
BranchTrueFalseTotal
$_ ne $val or next CONDITION 0 0 0
933 0 [ 1 .. $#{ $condition->{$test} } ];
934 }
935 else {
936  
937 # Fail if no entries match.
938 0 my $okay = 0;
939  
940 $_ eq $val and $okay = 1, last
941 0 ▶ for @{ $condition->{$test} };
Branch: 1 branch
BranchTrueFalseTotal
$_ eq $val and $okay = 1, last 0 0 0
942  
943 0 ▶ next CONDITION if not $okay;
Branch: 1 branch
BranchTrueFalseTotal
unless $okay 0 0 0
944 }
945 }
946 elsif ( 'CODE' eq ref $condition->{$test} ) {
947 0 local $_ = $val;
948 0 ▶ $condition->{$test}($op)
Branch: 1 branch
BranchTrueFalseTotal
$condition->{$test}->($op) or next CONDITION 0 0 0
949 or next CONDITION;
950 }
951 else {
952  
953 # Test a single value.
954 111286 ▶ $condition->{$test} eq $op->$test
Branch: 1 branch
BranchTrueFalseTotal
$condition->{$test} eq $op->$test or next CONDITION 0 111286 111286
955 or next CONDITION;
956 }
957 } # end for test
958  
959 # We know it ->can because that was tested above. It is an
960 # error to have anything in this list of tests that isn't
961 # tested for ->can above.
962 0 foreach (
963 qw( first other last sibling next pmreplroot pmreplstart pmnext )
964 ) {
965 0 ▶ next unless exists $condition->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $condition->{$_} 0 0 0
966 0 my ($result) = opgrep( $condition->{$_}, $op->$_ );
967 0 ▶ next CONDITION if not $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
968  
969 0 ▶ if (not blessed($result)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($result)) 0 0 0
970 # copy over the captured data/ops from the recursion
971 0 $capture->{$_} = $result->{$_} foreach keys %$result;
972 }
973 }
974  
975 # Apply all kids conditions. We $op->can(kids) (see above).
976 0 ▶ if (exists $condition->{kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $condition->{"kids"}) 0 0 0
977 0 my $kidno = 0;
978 0 my $kidconditions = $condition->{kids};
979  
980 0 ▶ next CONDITION if not @{$kidconditions} == @{$condition->{kids}};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$kidconditions;} == @{$condition->{"kids"};} 0 0 0
981  
982 0 foreach my $kid ($op->kids()) {
983 # if you put undef in your kid conditions list, we skip one kid
984 0 ▶ next if not defined $kidconditions->[$kidno];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kidconditions->[$kidno] 0 0 0
985  
986 0 my ($result) = opgrep( $kidconditions->[$kidno++], $kid );
987 0 ▶ next CONDITION if not $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
988  
989 0 ▶ if (not blessed($result)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($result)) 0 0 0
990 # copy over the captured data/ops from the recursion
991 0 $capture->{$_} = $result->{$_} foreach keys %$result;
992 }
993 }
994 }
995  
996 # Attempt to quit early if possible.
997 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
998 0 ▶ if (keys %$capture) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$capture) { } 0 0 0
999 # save all captured information and the main op
1000 0 $capture->{op} = $op;
1001 0 push @grep_ops, $capture;
1002 }
1003 else {
1004 # save main op
1005 0 push @grep_ops, $op;
1006 }
1007 0 last;
1008 }
1009 elsif ( defined wantarray ) {
1010 0 return _TRUE;
1011 }
1012 } # end for @conditions
1013 # end of conditions loop should be end of op test
1014 }
1015  
1016 # Either this was called in list context and then I want to just
1017 # return everything possible or this is in scalar/void context and
1018 # @grep_ops will be empty and thus "false."
1019 112477 return @grep_ops;
1020 }
1021  
1022 0 ▶sub _opgrep_helper {
Subroutine _opgrep_helper: not called CC=3 SCAR=24.8
1023 my @conds =
1024 0 ▶ map ref() ? {%$_} : { name => $_ }, @{ $_[0] };
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
1025  
1026 # Wire this into a list of entries, all ->next
1027 0 for ( 1 .. $#conds ) {
1028 0 $conds[ $_ - 1 ]{next} = $conds[$_];
1029 }
1030  
1031 # This is a linked list now so I can return only the head.
1032 0 return $conds[0];
1033 }
1034  
1035 =item C<op_or( @conditions )>
1036  
1037 Unlike the chaining of conditions done by C<opgrep> itself if there are multiple
1038 conditions, this function creates a disjunction (C<$cond1 || $cond2 || ...>) of
1039 the conditions and returns a structure (hash reference) that can be passed to
1040 opgrep as a single condition.
1041  
1042 Example:
1043  
1044 my $sub_structure = {
1045 name => 'helem',
1046 first => { name => 'rv2hv', },
1047 'last' => { name => 'const', },
1048 };
1049  
1050 my @ops = opgrep( {
1051 name => 'leavesub',
1052 first => {
1053 name => 'lineseq',
1054 kids => [,
1055 { name => 'nextstate', },
1056 op_or(
1057 {
1058 name => 'return',
1059 first => { name => 'pushmark' },
1060 last => $sub_structure,
1061 },
1062 $sub_structure,
1063 ),
1064 ],
1065 },
1066 }, $op_obj );
1067  
1068 This example matches the code in a typical simplest-possible
1069 accessor method (albeit not down to the last bit):
1070  
1071 sub get_foo { $_[0]->{foo} }
1072  
1073 But by adding an alternation
1074 we can also match optional op layers. In this case, we optionally
1075 match a return statement, so the following implementation is also
1076 recognized:
1077  
1078 sub get_foo { return $_[0]->{foo} }
1079  
1080 Essentially, this is syntactic sugar for the following structure
1081 recognized by C<opgrep()>:
1082  
1083 { disjunction => [@conditions] }
1084  
1085 =cut
1086  
1087 0 ▶sub op_or {
Subroutine op_or: not called CC=1 SCAR=6.9
1088 0 my @conditions = @_;
1089 0 return({ disjunction => [@conditions] });
1090 }
1091  
1092 # TODO
1093 # sub op_pattern_match {
1094 # my $op = shift;
1095 # my $pattern = shift;
1096 #
1097 # my $ret = {};
1098 #
1099 #
1100 # return $ret;
1101 # }
1102  
1103 =item C<carp(@args)>
1104  
1105 =item C<croak(@args)>
1106  
1107 Warn and die, respectively, from the perspective of the position of
1108 the op in the program. Sounds complicated, but it's exactly the kind
1109 of error reporting you expect when you're grovelling through an op
1110 tree.
1111  
1112 =cut
1113  
1114 0 ▶sub carp (@) { CORE::warn( _preparewarn(@_) ) }
Subroutine carp: not called CC=1 SCAR=6.9
Pod documented
1115 0 ▶sub croak (@) { CORE::die( _preparewarn(@_) ) }
Subroutine croak: not called CC=1 SCAR=6.9
Pod documented
1116  
1117 =item C<dl_load_flags 1>
1118  
1119 Override L<DynaLoader> default to force global loading.
1120  
1121 =cut
1122  
1123 0 ▶sub _preparewarn {
Subroutine _preparewarn: not called CC=3 SCAR=24.8
1124 0 my $args = join '', @_;
1125 0 ▶ $args = "Something's wrong " unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 0 0 0
1126 0 ▶ if ( "\n" ne substr $args, -1, 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("\n" ne substr($args, -1, 1)) 0 0 0
1127 0 $args .= " at $B::Utils1::file line $B::Utils::line.\n";
1128 }
1129 0 return $args;
1130 }
1131  
1132 =back
1133  
1134 =head2 EXPORT
1135  
1136 None by default.
1137  
1138 =head2 XS EXPORT
1139  
1140 This modules uses L<ExtUtils::Depends> to export some useful functions
1141 for XS modules to use. To use those, include in your Makefile.PL:
1142  
1143 my $pkg = ExtUtils::Depends->new("Your::XSModule", "B::Utils1");
1144 WriteMakefile(
1145 ... # your normal makefile flags
1146 $pkg->get_makefile_vars,
1147 );
1148  
1149 Your XS module can now include F<BUtils1.h>. To see
1150 document for the functions provided, use:
1151  
1152 perldoc -m B::Utils1::Install::BUtils.h
1153  
1154 =head1 INSTALLATION
1155  
1156 To install this module, you may want to run the following commands:
1157  
1158 perl Makefile.PL
1159 make test
1160 sudo make install
1161  
1162 =head1 AUTHOR
1163  
1164 Maintained by Reini Urban C<rurban@cpan.org>.
1165  
1166 Originally written by Simon Cozens, C<simon@cpan.org> as B::Utils.
1167  
1168 Previously maintained by Joshua ben Jore, C<jjore@cpan.org> and Karen
1169 Etheridge as B::Utils.
1170  
1171 Contributions from Mattia Barbon, Jim Cromie, Steffen Mueller, and
1172 Chia-liang Kao, Alexandr Ciornii.
1173  
1174 =head1 LICENSE
1175  
1176 This module is free software; you can redistribute it and/or modify it
1177 under the same terms as Perl itself.
1178  
1179 =head1 SEE ALSO
1180  
1181 L<B::Utils>, L<B>, L<B::Generate>.
1182  
1183 =cut
1184  
1185 8 1;
↑ Summary blib/lib/B/Utils1/Install/IFiles.pm »