1B::BUtils(3) User Contributed Perl Documentation B::BUtils(3)
2
3
4
6 B::Utils - Helper functions for op tree manipulation
7
9 use B::Utils;
10
12 These functions make it easier to manipulate the op tree.
13
15 "all_starts"
16 "all_roots"
17 Returns a hash of all of the starting ops or root ops of optrees,
18 keyed to subroutine name; the optree for main program is simply
19 keyed to "__MAIN__".
20
21 Note: Certain "dangerous" stashes are not scanned for subroutines:
22 the list of such stashes can be found in @B::Utils::bad_stashes.
23 Feel free to examine and/or modify this to suit your needs. The
24 intention is that a simple program which uses no modules other than
25 "B" and "B::Utils" would show no addition symbols.
26
27 This does not return the details of ops in anonymous subroutines
28 compiled at compile time. For instance, given
29
30 $a = sub { ... };
31
32 the subroutine will not appear in the hash. This is just as well,
33 since they're anonymous... If you want to get at them, use...
34
35 "anon_subs()"
36 This returns an array of hash references. Each element has the keys
37 "start" and "root". These are the starting and root ops of all of
38 the anonymous subroutines in the program.
39
40 "$op->oldname"
41 Returns the name of the op, even if it is currently optimized to
42 null. This helps you understand the stucture of the op tree.
43
44 "$op->kids"
45 Returns an array of all this op's non-null children, in order.
46
47 "$op->first"
48 "$op->last"
49 "$op->other"
50 Normally if you call first, last or other on anything which is not
51 an UNOP, BINOP or LOGOP respectivly it will die. This leads to lots
52 of code like:
53
54 $op->first if $op->can('first');
55
56 B::Utils provides every op with first, last and other methods which
57 will simply return nothing if it isn't relevent.
58
59 "$op->parent"
60 Returns the parent node in the op tree, if possible. Currently
61 "possible" means "if the tree has already been optimized"; that is,
62 if we're during a "CHECK" block. (and hence, if we have valid "next"
63 pointers.)
64
65 In the future, it may be possible to search for the parent before we
66 have the "next" pointers in place, but it'll take me a while to
67 figure out how to do that.
68
69 "$op->previous"
70 Like "$op->next", but not quite.
71
72 walkoptree_simple($op, \&callback, [$data])
73 The "B" module provides various functions to walk the op tree, but
74 they're all rather difficult to use, requiring you to inject methods
75 into the "B::OP" class. This is a very simple op tree walker with
76 more expected semantics.
77
78 The &callback is called at each op with the op itself passed in as
79 the first argument and any additional $data as the second.
80
81 All the "walk" functions set $B::Utils::file and $B::Utils::line to
82 the appropriate values of file and line number in the program being
83 examined. Since only COPs contain this information it may be
84 unavailable in the first few callback calls.
85
86 walkoptree_filtered($op, \&filter, \&callback, [$data])
87 This is much the same as "walkoptree_simple", but will only call the
88 callback if the "filter" returns true. The "filter" is passed the op
89 in question as a parameter; the "opgrep" function is fantastic for
90 building your own filters.
91
92 walkallops_simple(\&callback, [$data])
93 This combines "walkoptree_simple" with "all_roots" and "anon_subs"
94 to examine every op in the program. $B::Utils::sub is set to the
95 subroutine name if you're in a subroutine, "__MAIN__" if you're in
96 the main program and "__ANON__" if you're in an anonymous
97 subroutine.
98
99 walkallops_filtered(\&filter, \&callback, [$data])
100 Same as above, but filtered.
101
102 carp(@args)
103 croak(@args)
104 Warn and die, respectively, from the perspective of the position of
105 the op in the program. Sounds complicated, but it's exactly the kind
106 of error reporting you expect when you're grovelling through an op
107 tree.
108
109 opgrep(\%conditions, @ops)
110 Returns the ops which meet the given conditions. The conditions
111 should be specified like this:
112
113 @barewords = opgrep(
114 { name => "const", private => OPpCONST_BARE },
115 @ops
116 );
117
118 You can specify alternation by giving an arrayref of values:
119
120 @svs = opgrep ( { name => ["padsv", "gvsv"] }, @ops)
121
122 And you can specify inversion by making the first element of the
123 arrayref a "!". (Hint: if you want to say "anything", say "not
124 nothing": "["!"]")
125
126 You may also specify the conditions to be matched in nearby ops.
127
128 walkallops_filtered(
129 sub { opgrep( {name => "exec",
130 next => {
131 name => "nextstate",
132 sibling => { name => [qw(! exit warn die)] }
133 }
134 }, @_)},
135 sub {
136 carp("Statement unlikely to be reached");
137 carp("\t(Maybe you meant system() when you said exec()?)\n");
138 }
139 )
140
141 Get that?
142
143 Here are the things that can be tested:
144
145 name targ type seq flags private pmflags pmpermflags
146 first other last sibling next pmreplroot pmreplstart pmnext
147
148 EXPORT
149 None by default.
150
152 Simon Cozens, "simon@cpan.org"
153
155 I need to add more Fun Things, and possibly clean up some parts where
156 the (previous/parent) algorithm has catastrophic cases, but it's more
157 important to get this out right now than get it right.
158
160 B, B::Generate.
161
162
163
164perl v5.10.1 2010-11-12 B::BUtils(3)