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