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