1B::Generate(3) User Contributed Perl Documentation B::Generate(3)
2
3
4
6 B::Generate - Create your own op trees.
7
9 use B::Generate;
10 # Do nothing, slowly.
11 CHECK {
12 my $null = new B::OP("null",0);
13 my $enter = new B::OP("enter",0);
14 my $cop = new B::COP(0, "hiya", 0);
15 my $leave = new B::LISTOP("leave", 0, $enter, $null);
16 $leave->children(3);
17 $enter->sibling($cop);
18 $enter->next($cop);
19 $cop->sibling($null);
20 $null->next($leave);
21 $cop->next($leave);
22
23 # Tell Perl where to find our tree.
24 B::main_root($leave);
25 B::main_start($enter);
26 }
27
29 This module will create segmentation faults if you don't know how to
30 use it properly. Further warning: sometimes I don't know how to use it
31 properly.
32
33 There are lots of other methods and utility functions, but they are not
34 documented here. This is deliberate, rather than just through laziness.
35 You are expected to have read the Perl and XS sources to this module
36 before attempting to do anything with it.
37
39 The "B" module allows you to examine the Perl op tree at runtime, in
40 Perl space; it's the basis of the Perl compiler. But what it doesn't
41 let you do is manipulate that op tree: it won't let you create new ops,
42 or modify old ones. Now you can.
43
44 Well, if you're intimately familiar with Perl's internals, you can.
45
46 "B::Generate" turns "B"'s accessor methods into get-set methods.
47 Hence, instead of merely saying
48
49 $op2 = $op->next;
50
51 you can now say
52
53 $op->next($op2);
54
55 to set the next op in the chain. It also adds constructor methods to
56 create new ops. This is where it gets really hairy.
57
58 new B::OP ( type, flags )
59 new B::UNOP ( type, flags, first )
60 new B::BINOP ( type, flags, first, last )
61 new B::LOGOP ( type, flags, first, other )
62 new B::LISTOP ( type, flags, first, last )
63 new B::SVOP ( type, flags, sv )
64 new B::COP ( flags, name, first )
65
66 In all of the above constructors, "type" is either a numeric value
67 representing the op type (62 is the addition operator in certain perl
68 versions, for instance) or the name of the op. ("add")
69
70 Incidentally, if you know about custom ops and have registed them
71 properly with the interpreter, you can create custom ops by name: "new
72 B::OP("mycustomop",0)", or whatever.
73
74 "first", "last" and "other" are ops to be attached to the current op;
75 these should be "B::OP" objects. If you haven't created the ops yet,
76 don't worry; give a false value, and fill them in later:
77
78 $x = new B::UNOP("negate", 0, undef);
79 # ... create some more ops ...
80 $x->first($y);
81
82 In addition, one may create a new "nextstate" operator with
83
84 newstate B::op ( flags, label, op)
85
86 in the same manner as "B::COP::new" - this will also, however, add the
87 "lineseq" op.
88
89 Finally, you can set the main root and the starting op by passing ops
90 to the "B::main_root" and "B::main_start" functions.
91
92 This module can obviously be used for all sorts of fun and
93 optimizational purposes. One example will be in conjuction with source
94 filters; have your source filter parse an input file in a foreign
95 language, create an op tree for it and get Perl to execute it. Then
96 email me and tell me how you did it. And why.
97
98 OTHER METHODS
99 B::SVOP->sv()
100 Returns the SV value instead of the "B::SV" object. For instance:
101
102 $b_sv = $svop->sv;
103 if ($b_sv->sv == 3) {
104 print "SVOP's SV has an IV of 3\n"
105 }
106
107 But to set the SV you need a proper B::SV object.
108
109 $op->dump
110 Runs "Perl_op_dump" on an op; this is roughly equivalent to
111 "B::Debug", but not quite.
112
113 $b_sv->dump
114 Runs "Perl_sv_dump" on an SV; this is exactly equivalent to
115 "Devel::Peek::dump($b_sv->sv)"
116
117 $b_op->linklist
118 Sets the "op_next" pointers in the tree in correct execution order,
119 overwriting the old "next" pointers. You need to do this once you've
120 created an op tree for execution, unless you've carefully threaded
121 it together yourself.
122
123 $b_op->scope
124 Create a surrounding scope for the b_op, "parenthesize" it.
125
126 Creates on OPf_PARENS (already parenthesized by the parser) a full
127 lineseq, enter, b_op, leave sequence.
128
129 Otherwise just scope, b_op.
130
131 B::SVOP->new_svrv ( type, flags, sv )
132 Similar to B::SVOP->new ( type, flags, sv ), it just creates a new
133 SVOP with an attached sv as SvRV to the given sv.
134
135 $cv->NEW_with_start (root, start)
136 Clone the "cv" with new root and start ops. Note that contrary to
137 "cv_clone", the PADLIST and pad index is kept, but the index might
138 point to a different lexical, because the PADLIST indices will be
139 different. See t/new_cv.t.
140
141 Warning: "$cv-"NEW_with_start> is disabled on some strict platforms,
142 like MSWin32. See CPAN RT#28912.
143
144 $b_op->targ ( [ targ] )
145 Get or set the PADOFFSET.
146
147 EXPORT
148 None.
149
151 Simon Cozens, "simon@cpan.org" Reini Urban, "rurban@cpan.org"
152
154 Maintained by Reini Urban.
155
156 This is just a list of people who have submitted patches to the module:
157
158 Josh Jore, Michael Schwern, Jim Cromie, Scott Walters, Reini Urban,
159 Anton Berezin, Dmitry Karasik.
160
161 Maintainership permissions do have: Artur Bergman, Chia-liang Kao,
162 Anton Berezin, Jim Cromie, Joshua ben Jore, Michael G Schwern, Matt S
163 Trout, Reini Urban, Scott Walters.
164
166 This module is available under the same licences as perl, the Artistic
167 license and the GPL.
168
170 B, perlguts, op.c, perloptree with B::C
171
172
173
174perl v5.32.1 2021-01-26 B::Generate(3)