B::Generate(3)

πŸ“¦
version 1.56 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

B::Generate β€” bestie, you can literally create your own op trees. Build them from scratch. No cap. 🌳✨


πŸŽ€ SYNOPSIS πŸŽ€

use B::Generate;
# Do nothing, slowly.
CHECK {
    my $null = new B::OP("null",0);
    my $enter = new B::OP("enter",0);
    my $cop = new B::COP(0, "hiya", 0);
    my $leave = new B::LISTOP("leave", 0, $enter, $null);
    $leave->children(3);
    $enter->sibling($cop);
    $enter->next($cop);
    $cop->sibling($null);
    $null->next($leave);
    $cop->next($leave);

    # Tell Perl where to find our tree.
    B::main_root($leave);
    B::main_start($enter);
}

πŸ’€ WARNING πŸ’€

Okay so real talk, this module will hand you segmentation faults if you don't know how to use it properly. That's not a vibe, that's a fact. And further warning, because honesty is hot: sometimes I don't know how to use it properly. 😭

There are lots of other methods and utility functions, but they are not documented here. That's deliberate, not just laziness, okay? You're expected to have read the Perl and XS sources to this module before you attempt to do literally anything with it. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

The B module lets you examine the Perl op tree at runtime, in Perl space β€” it's the whole basis of the Perl compiler. But the one thing it will not let you do is manipulate that op tree. It won't let you create new ops, and it won't let you modify old ones. Now you can. ✨

Well. If you're intimately familiar with Perl's internals, you can. πŸ’…

B::Generate turns B's accessor methods into get-set methods. So instead of merely saying

$op2 = $op->next;

you can now say

$op->next($op2);

to set the next op in the chain. It also adds constructor methods to create new ops. This is where it gets really hairy. πŸ™ˆ

new B::OP     ( type, flags )
new B::UNOP   ( type, flags, first )
new B::BINOP  ( type, flags, first, last )
new B::LOGOP  ( type, flags, first, other )
new B::LISTOP ( type, flags, first, last )
new B::SVOP   ( type, flags, sv )
new B::COP    ( flags, name, first )

In all of the above constructors, type is either a numeric value representing the op type (62 is the addition operator in certain perl versions, for instance) or the name of the op. ("add")

Incidentally, if you know about custom ops and have registered them properly with the interpreter, you can create custom ops by name: new B::OP("mycustomop",0), or whatever. 🧚

first, last and other are ops to be attached to the current op; these should be B::OP objects. If you haven't created the ops yet, don't worry β€” give a false value, and fill them in later:

$x = new B::UNOP("negate", 0, undef);
# ... create some more ops ...
$x->first($y);

In addition, one may create a new nextstate operator with

newstate B::op ( flags, label, op)

in the same manner as B::COP::new β€” this will also, however, add the lineseq op.

Finally, you can set the main root and the starting op by passing ops to the B::main_root and B::main_start functions. 🌸

This module can obviously be used for all sorts of fun and optimizational purposes. One example will be in conjunction with source filters: have your source filter parse an input file in a foreign language, create an op tree for it and get Perl to execute it. Then email me and tell me how you did it. And why. πŸ’Œ

⭐ OTHER METHODS ⭐

  • B::SVOP->sv() β€” Returns the SV value instead of the B::SV object. For instance:
$b_sv = $svop->sv;
if ($b_sv->sv == 3) {
    print "SVOP's SV has an IV of 3\n"
}

But to set the SV you need a proper B::SV object. πŸ€·β€β™€οΈ

  • $op->dump β€” Runs Perl_op_dump on an op; this is roughly equivalent to B::Debug, but not quite.
  • $b_sv->dump β€” Runs Perl_sv_dump on an SV; this is exactly equivalent to Devel::Peek::dump($b_sv->sv)
  • $b_op->linklist β€” Sets the op_next pointers in the tree in correct execution order, overwriting the old next pointers. You need to do this once you've created an op tree for execution, unless you've carefully threaded it together yourself.
  • $b_op->scope β€” Create a surrounding scope for the b_op, "parenthesize" it.

Creates on OPf_PARENS (already parenthesized by the parser) a full lineseq, enter, b_op, leave sequence.

Otherwise just scope, b_op.

  • B::SVOP->new_svrv ( type, flags, sv ) β€” Similar to B::SVOP->new ( type, flags, sv ), it just creates a new SVOP with an attached sv as SvRV to the given sv.
  • $cv->NEW_with_start (root, start) β€” Clone the cv with new root and start ops. Note that contrary to cv_clone, the PADLIST and pad index is kept, but the index might point to a different lexical, because the PADLIST indices will be different. See t/new_cv.t.
Warning: $cv-NEW_with_start> is disabled on some strict platforms, like MSWin32. See CPAN RT#28912. 😀
  • $b_op->targ ( [ targ] ) β€” Get or set the PADOFFSET.

🌈 EXPORT 🌈

None. That's it. Nothing. Zero. πŸ™…β€β™€οΈ


πŸ‘‘ AUTHOR πŸ‘‘

Simon Cozens, simon@cpan.org β€” Reini Urban, rurban@cpan.org


🫢 MAINTAINERS 🫢

Maintained by Reini Urban. πŸ’œ

This is just a list of people who have submitted patches to the module:

Josh Jore, Michael Schwern, Jim Cromie, Scott Walters, Reini Urban, Anton Berezin, Dmitry Karasik.

Maintainership permissions do have: Artur Bergman, Chia-liang Kao, Anton Berezin, Jim Cromie, Joshua ben Jore, Michael G Schwern, Matt S Trout, Reini Urban, Scott Walters.


πŸ“ LICENSE πŸ“

This module is available under the same licences as perl, the Artistic license and the GPL.


πŸ”₯ SEE ALSO πŸ”₯

B, perlguts, op.c, perloptree with B::C


And that's the whole page, bestie β€” this one's from perl v5.42.0, dated 2025-07-25, filed under B::Generate(3) in the User Contributed Perl Documentation. Now go build a tree and don't segfault. πŸ’…βœ¨

πŸ“¦ where this page comes from