blib/lib/B/Utils.pm

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