File Coverage

blib/lib/B/Utils.pm
Criterion Covered Total %
statement 145 253 57.3
branch 59 126 46.8
condition 30 84 35.7
subroutine 32 46 69.5
pod 4 4 100.0
total 270 513 52.6


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