File Coverage

blib/lib/B/Utils.pm
Criterion Covered Total %
statement 147 256 57.4
branch 61 130 46.9
condition 31 87 35.6
subroutine 33 48 68.7
pod 4 4 100.0
total 276 525 52.5


line stmt bran cond sub pod time code
1             package B::Utils;
2              
3 8     8   168789 use 5.006;
  8         27  
4 8     8   37 use strict;
  8         10  
  8         205  
5 8     8   35 use warnings;
  8         15  
  8         308  
6 8         954 use vars qw( @EXPORT_OK %EXPORT_TAGS
7 8     8   37 @bad_stashes $TRACE_FH $file $line $sub );
  8         12  
8              
9             use subs (
10 8         38 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     8   5602 );
  8         159  
15             sub croak (@);
16             sub carp (@);
17              
18 8     8   875 use Scalar::Util qw( weaken blessed );
  8         11  
  8         1308  
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             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     8   48 use base 'DynaLoader';
  8         12  
  8         1416  
52             bootstrap B::Utils $VERSION;
53             #bootstrap B::Utils::OP $VERSION;
54             #B::Utils::OP::boot_B__Utils__OP();
55 8     8 1 1775 sub dl_load_flags {0x01}
56              
57             =head1 SYNOPSIS
58              
59             use B::Utils;
60              
61             =cut
62              
63 8     8   48 use B qw( OPf_KIDS main_start main_root walksymtable class main_cv ppname );
  8         30  
  8         710  
64              
65 8     8   40 use Exporter ();
  8         11  
  8         546  
66             @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             %EXPORT_TAGS = ( all => \@EXPORT_OK );
72             *import = \&Exporter::import;
73              
74             @bad_stashes
75             = qw(B Carp Exporter warnings Cwd Config CORE blib strict DynaLoader vars XSLoader AutoLoader base);
76              
77 8     8   106 use List::Util qw( shuffle );
  8         13  
  8         906  
78              
79             BEGIN {
80              
81             # Fake up a TRACE constant and set $TRACE_FH
82 8     8   179 BEGIN { $^W = 0 }
83 8     8   43 no warnings;
  8         11  
  8         613  
84 8     8   429 eval 'sub _TRACE () {' . ( 0 + $ENV{B_UTILS_TRACE} ) . '}';
85 8 50       52 die $@ if $@;
86 8   50     3220 $TRACE_FH ||= \*STDOUT;
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             sub B::OP::oldname {
132 0     0   0 my $op = shift;
133 0         0 my $name = $op->name;
134 0         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 0 0     0 return $name eq 'null' && $targ
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             sub B::OP::kids {
153 33418     33418   28086 my $op = shift;
154 33418 50       46463 return unless defined wantarray;
155              
156 33418         23689 my @kids;
157 33418 100 33     177995 if ( ref $op and $$op and $op->flags & OPf_KIDS ) {
      66        
158 28994         83144 for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
159 58678         163620 push @kids, $kid;
160             }
161             ### Assert: $op->children == @kids
162             }
163             else {
164 4424 100       18270 @kids = (
    100          
    50          
165             ( $op->can('first') ? $op->first : () ),
166             ( $op->can('last') ? $op->last : () ),
167             ( $op->can('other') ? $op->other : () )
168             );
169             }
170 33418         67057 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             BEGIN {
192 8 50 33 8   82 unless ($] >= 5.021002 and exists &B::OP::parent) {
193 0         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     92   513 eval q[
  92         2658  
  92         230  
  92         261  
202             sub B::OP::_parent {
203             my $op = shift;
204             my $parent = $op->_parent_impl( $op, "" );
205             $parent;
206             }];
207             }
208 8 50       50 if ($] >= 5.021002) {
209 8     0   25467 eval q[
210             sub B::NULL::kids { }
211             ];
212             }
213             }
214              
215       4582     sub B::NULL::_parent_impl { }
216              
217             sub B::OP::_parent_impl {
218 8847     8847   8883 my ( $op, $target, $cx ) = @_;
219              
220 8847 100       66614 return if $cx =~ /\b$$op\b/;
221              
222 8627         11587 for ( $op->kids ) {
223 8055 100       12182 if ( $$_ == $$target ) {
224 91         2655 return $op;
225             }
226             }
227              
228             return (
229 8536   66     26179 $op->sibling->_parent_impl( $target, "$cx$$op S " )
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             sub B::OP::ancestors {
251 0     0   0 my @nodes = shift;
252              
253 0         0 my $parent;
254 0         0 push @nodes, $parent while $parent = $nodes[-1]->parent;
255 0         0 shift @nodes;
256              
257 0         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             sub B::OP::descendants {
267 0     0   0 my $node = shift;
268 0         0 my @nodes;
269             walkoptree_simple( $node,
270 0 0   0   0 sub { push @nodes, $_ if ${ $_[0] } != $$node } );
  0         0  
  0         0  
271 0         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             sub B::OP::siblings {
282 0     0   0 my @siblings = $_[0];
283              
284 0         0 my $sibling;
285 0         0 push @siblings, $siblings[-1]->sibling while $siblings[-1]->can('sibling');
286 0         0 shift @siblings;
287              
288             # Remove any undefined or B::NULL objects
289             pop @siblings while
290             @siblings
291             && !( defined $siblings[-1]
292 0   0     0 && ${$siblings[-1]} );
      0        
293              
294 0         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             sub B::OP::stringify {
365 182     182   1108 my $op = shift;
366              
367 182         2521 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             sub B::OP::as_opgrep_pattern {
402 0     0   0 my $op = shift;
403 0 0 0     0 my $opt = (@_ == 1 and ref($_[0]) eq 'HASH') ? shift() : {@_};
404              
405 0         0 my $attribs = $opt->{attributes};
406 0   0     0 $attribs ||= [qw(name flags)];
407              
408 0         0 my $pattern = {};
409 0         0 foreach my $attr (@$attribs) {
410 0 0       0 $pattern->{$attr} = $op->$attr() if $op->can($attr);
411             }
412              
413 0         0 my $recursion_limit = $opt->{max_recursion_depth};
414 0 0 0     0 if ( (not defined $recursion_limit or $recursion_limit > 0)
      0        
      0        
      0        
415             and ref($op)
416             and $$op
417             and $op->flags & OPf_KIDS
418             ) {
419 0 0       0 $opt->{max_recursion_depth}-- if defined $recursion_limit;
420              
421             $pattern->{kids} = [
422 0         0 map { $_->as_opgrep_pattern($opt) } $op->kids()
  0         0  
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 0       0 if exists $opt->{max_recursion_depth};
429              
430 0         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             my ( %starts, %roots );
464 2 100   2   1733 sub all_starts { _init_sub_cache(); wantarray ? %starts : \%starts }
  2         1189  
465 3 100   3   1156 sub all_roots { _init_sub_cache(); wantarray ? %roots : \%roots }
  3         765  
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             my @anon_subs;
476 2 100   2 1 1152 sub anon_subs { _init_sub_cache(); wantarray ? @anon_subs : \@anon_subs }
  2         23  
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             my $subs_cached = _FALSE;
487              
488             sub recalc_sub_cache {
489 0     0   0 $subs_cached = _FALSE;
490              
491 0         0 %starts = %roots = @anon_subs = ();
492              
493 0         0 _init_sub_cache();
494 0         0 return;
495             }
496              
497             sub _init_sub_cache {
498              
499             # Allow this function to be run only once.
500 8 100   8   30 return if $subs_cached;
501              
502 5         66 %starts = ( __MAIN__ => main_start() );
503 5         33 %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             _B_Utils_init_sub_cache => sub {
510              
511             # Do not eat our own children!
512 735   100 735   10584 $_[0] eq "$_\::" && return _FALSE for @bad_stashes;
513              
514 670         11661 return _TRUE;
515             },
516 5         991 ''
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 100       485 push @anon_subs,
523             map( (
524             'CV' eq class($_)
525             ? { root => $_->ROOT,
526             start => $_->START
527             }
528             : ()
529             ),
530             main_cv()->PADLIST->ARRAY->ARRAY );
531              
532 5         19 $subs_cached = _TRUE;
533 5         13 return;
534             }
535              
536             sub B::GV::_B_Utils_init_sub_cache {
537              
538             # This is a callback function called from B::Utils::_init via
539             # B::walksymtable.
540              
541 8982     8982   8112 my $gv = shift;
542 8982         14619 my $cv = $gv->CV;
543              
544             # If the B::CV object is a pointer to nothing, ignore it.
545 8982 100       27958 return unless $$cv;
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 6178         20240 my $name = $gv->STASH->NAME . "::" . $gv->NAME;
553              
554             # When does a CV not fulfill ->ARRAY->ARRAY? Some time during
555             # initialization?
556 6178 100 66     53191 if ( $cv->can('PADLIST')
      100        
557             and $cv->PADLIST->can('ARRAY')
558             and $cv->PADLIST->ARRAY->can('ARRAY') )
559             {
560 3275 100       138118 push @anon_subs,
561             map( (
562             'CV' eq class($_)
563             ? { root => $_->ROOT,
564             start => $_->START
565             }
566             : ()
567             ),
568             $cv->PADLIST->ARRAY->ARRAY );
569             }
570              
571 6178 50 33     38317 return unless ( ( my $start = $cv->START )
572             and ( my $root = $cv->ROOT ) );
573              
574 6178         13814 $starts{$name} = $start;
575 6178         7746 $roots{$name} = $root;
576              
577             # return _TRUE;
578 6178         46986 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             $B::Utils::file = '__none__';
605             $B::Utils::line = 0;
606             $B::Utils::sub = undef;
607              
608             sub walkoptree_simple {
609 1239     1239   3594 $B::Utils::file = '__none__';
610 1239         973 $B::Utils::line = 0;
611              
612 1239         2064 _walkoptree_simple( {}, @_ );
613              
614 1239         10256 return _TRUE;
615             }
616              
617             sub _walkoptree_simple {
618 51705     51705   50936 my ( $visited, $op, $callback, $data ) = @_;
619              
620 51705 50       140157 return if $visited->{$$op}++;
621              
622 51705 100 66     222026 if ( ref $op and $op->isa("B::COP") ) {
623 5520         14368 $B::Utils::file = $op->file;
624 5520         9554 $B::Utils::line = $op->line;
625             }
626              
627 51705         71164 $callback->( $op, $data );
628 51705 100       431831 return if $op->isa('B::NULL');
629 51126 100       127721 if ( $op->flags & OPf_KIDS ) {
630             # for (my $kid = $op->first; $$kid; $kid = $kid->sibling) {
631             # _walkoptree_simple( $visited, $kid, $callback, $data );
632             # }
633 24700         29928 _walkoptree_simple( $visited, $_, $callback, $data ) for $op->kids;
634             }
635 51126 100       124400 if ( $op->isa('B::PMOP') ) {
636 321         905 my $maybe_root = $op->pmreplroot;
637 321 100 66     1984 if (ref($maybe_root) and $maybe_root->isa("B::OP")) {
638             # It really is the root of the replacement, not something
639             # else stored here for lack of space elsewhere
640 13         28 _walkoptree_simple( $visited, $maybe_root, $callback, $data );
641             }
642             }
643              
644 51126         69461 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             sub walkoptree_filtered {
658 1284     1284   1538 $B::Utils::file = '__none__';
659 1284         1416 $B::Utils::line = 0;
660              
661 1284         2276 _walkoptree_filtered( {}, @_ );;
662              
663 1284         3424 return _TRUE;
664             }
665              
666             sub _walkoptree_filtered {
667 52878     52878   55081 my ( $visited, $op, $filter, $callback, $data ) = @_;
668              
669 52878 100       156421 if ( $op->isa("B::COP") ) {
670 5667         15244 $B::Utils::file = $op->file;
671 5667         10488 $B::Utils::line = $op->line;
672             }
673              
674 52878 50       83521 $callback->( $op, $data ) if $filter->($op);
675              
676 52878 100 66     361084 if ( ref $op
      100        
677             and $$op
678             and $op->flags & OPf_KIDS )
679             {
680              
681 25273         58963 my $kid = $op->first;
682 25273   66     82817 while ( ref $kid
683             and $$kid )
684             {
685 51594         72604 _walkoptree_filtered( $visited, $kid, $filter, $callback, $data );
686              
687 51594         253778 $kid = $kid->sibling;
688             }
689             }
690              
691 52878         53016 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             sub walkallops_simple {
704 0     0   0 $B::Utils::sub = undef;
705              
706 0         0 &_walkallops_simple;
707              
708 0         0 return _TRUE;
709             }
710              
711             sub _walkallops_simple {
712 0     0   0 my ( $callback, $data ) = @_;
713              
714 0         0 _init_sub_cache();
715              
716 0         0 for my $sub_name (sort keys %roots) {
717 0         0 $B::Utils::sub = $sub_name;
718 0         0 my $root = $roots{$sub_name};
719 0         0 walkoptree_simple( $root, $callback, $data );
720             }
721              
722 0         0 $B::Utils::sub = "__ANON__";
723 0         0 walkoptree_simple( $_->{root}, $callback, $data ) for @anon_subs;
724              
725 0         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             sub walkallops_filtered {
735 1     1   964 $B::Utils::sub = undef;
736              
737 1         3 &_walkallops_filtered;
738              
739 1         3 return _TRUE;
740             }
741              
742             sub _walkallops_filtered {
743 1     1   3 my ( $filter, $callback, $data ) = @_;
744              
745 1         2 _init_sub_cache();
746              
747 1         101 walkoptree_filtered( $_, $filter, $callback, $data ) for values %roots;
748              
749 1         3 $B::Utils::sub = "__ANON__";
750              
751             walkoptree_filtered( $_->{root}, $filter, $callback, $data )
752 1         6 for @anon_subs;
753              
754 1         2 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             sub opgrep {
856 52878 50   52878   271880 return unless defined wantarray;
857              
858 52878         45561 my $conds_ref = shift;
859 52878 50       84479 $conds_ref = _opgrep_helper($conds_ref)
860             if 'ARRAY' eq ref $conds_ref;
861              
862 52878         36791 my @grep_ops;
863              
864             # Check whether we're dealing with a disjunction of patterns:
865 52878 50       99477 my @conditions = exists($conds_ref->{disjunction}) ? @{$conds_ref->{disjunction}} : ($conds_ref);
  0         0  
866              
867             OP:
868 52878         58476 for my $op (@_) {
869 52878 100 66     170441 next unless ref $op and $$op;
870              
871             # only one condition by default, but if we have a disjunction, there will
872             # be several
873             CONDITION:
874 52299         50386 foreach my $condition (@conditions) {
875             # nested disjunctions? naughty user!
876             # $foo or ($bar or $baz) is $foo or $bar or $baz!
877             # ==> flatten
878 52299 50       71694 if (exists($condition->{disjunction})) {
879 0         0 push @conditions, @{$condition->{disjunction}};
  0         0  
880 0         0 next CONDITION;
881             }
882              
883             # structure to hold captured information
884 52299         49983 my $capture = {};
885              
886             # Debugging aid
887 52299 50       81086 if (exists $condition->{'dump'}) {
888             ($op->can($_)
889             or next)
890             and warn "$_: " . $op->$_ . "\n"
891 0   0     0 for
      0        
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 52299 50       69988 if (not defined $condition) {
897 0 0 0     0 return _TRUE if not defined $op and not wantarray();
898 0         0 return();
899             }
900              
901             # save the op if the user wants flat access to it
902 52299 50       73566 if ($condition->{capture}) {
903 0         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 52299   66     1028553 for
      50        
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 52299         53937 for my $test (
926             qw(name targ type seq flags private pmflags pmpermflags))
927             {
928 52299 50       77760 next unless exists $condition->{$test};
929 52299         149293 my $val = $op->$test;
930              
931 52299 50       111339 if ( 'ARRAY' eq ref $condition->{$test} ) {
    50          
932              
933             # Test a list of valid/invalid values.
934 0 0       0 if ( '!' eq $condition->{$test}[0] ) {
935              
936             # Fail if any entries match.
937             $_ ne $val
938             or next CONDITION
939 0   0     0 for @{ $condition->{$test} }
  0         0  
940 0         0 [ 1 .. $#{ $condition->{$test} } ];
941             }
942             else {
943              
944             # Fail if no entries match.
945 0         0 my $okay = 0;
946              
947             $_ eq $val and $okay = 1, last
948 0   0     0 for @{ $condition->{$test} };
  0         0  
949              
950 0 0       0 next CONDITION if not $okay;
951             }
952             }
953             elsif ( 'CODE' eq ref $condition->{$test} ) {
954 0         0 local $_ = $val;
955 0 0       0 $condition->{$test}($op)
956             or next CONDITION;
957             }
958             else {
959              
960             # Test a single value.
961 52299 50       219507 $condition->{$test} eq $op->$test
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         0 foreach (
970             qw( first other last sibling next pmreplroot pmreplstart pmnext )
971             ) {
972 0 0       0 next unless exists $condition->{$_};
973 0         0 my ($result) = opgrep( $condition->{$_}, $op->$_ );
974 0 0       0 next CONDITION if not $result;
975              
976 0 0       0 if (not blessed($result)) {
977             # copy over the captured data/ops from the recursion
978 0         0 $capture->{$_} = $result->{$_} foreach keys %$result;
979             }
980             }
981              
982             # Apply all kids conditions. We $op->can(kids) (see above).
983 0 0       0 if (exists $condition->{kids}) {
984 0         0 my $kidno = 0;
985 0         0 my $kidconditions = $condition->{kids};
986              
987 0 0       0 next CONDITION if not @{$kidconditions} == @{$condition->{kids}};
  0         0  
  0         0  
988              
989 0         0 foreach my $kid ($op->kids()) {
990             # if you put undef in your kid conditions list, we skip one kid
991 0 0       0 next if not defined $kidconditions->[$kidno];
992              
993 0         0 my ($result) = opgrep( $kidconditions->[$kidno++], $kid );
994 0 0       0 next CONDITION if not $result;
995              
996 0 0       0 if (not blessed($result)) {
997             # copy over the captured data/ops from the recursion
998 0         0 $capture->{$_} = $result->{$_} foreach keys %$result;
999             }
1000             }
1001             }
1002              
1003             # Attempt to quit early if possible.
1004 0 0       0 if (wantarray) {
    0          
1005 0 0       0 if (keys %$capture) {
1006             # save all captured information and the main op
1007 0         0 $capture->{op} = $op;
1008 0         0 push @grep_ops, $capture;
1009             }
1010             else {
1011             # save main op
1012 0         0 push @grep_ops, $op;
1013             }
1014 0         0 last;
1015             }
1016             elsif ( defined wantarray ) {
1017 0         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 52878         119145 return @grep_ops;
1027             }
1028              
1029             sub _opgrep_helper {
1030             my @conds =
1031 0 0   0     map ref() ? {%$_} : { name => $_ }, @{ $_[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             sub op_or {
1095 0     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     0 1   sub carp (@) { CORE::warn( _preparewarn(@_) ) }
1122 0     0 1   sub croak (@) { CORE::die( _preparewarn(@_) ) }
1123              
1124             sub _preparewarn {
1125 0     0     my $args = join '', @_;
1126 0 0         $args = "Something's wrong " unless $args;
1127 0 0         if ( "\n" ne substr $args, -1, 1 ) {
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             "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.";