NAME
B::Generate - Create your own op trees.
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
! WARNING ! WARNING ! WARNING ! WARNING ! WARNING ! WARNING ! WARNING ! WARNING
B::Generate
is alpha-quality software. Large parts of it don't work. Even the parts that do work should give you the willies.
Patches welcome.
DESCRIPTION
Malcolm Beattie's B
module allows you to examine the Perl op tree at runtime, in Perl space; it's the basis of the Perl compiler. But what it doesn't let you do is manipulate that op tree: it won't let you create new ops, or 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. Hence, 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::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, for instance) or the name of the op. ("add"
)
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);
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 purposes. The best one will be in conjuction 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_sv->sv
-
Returns a real SV instead of a
B::SV
. For instance:$b_sv = $svop->sv; if ($b_sv->sv == 3) { print "SVOP's SV has an IV of 3\n" }
You can't use this to set the SV. That would be scary.
- $op->dump
-
Runs
Perl_op_dump
on an op; this is roughly equivalent toB::Debug
, but not quite. - $b_sv->dump
-
Runs
Perl_sv_dump
on an SV; this is exactly equivalent toData::Dumper::dump($b_sv->sv)
EXPORT
None.
AUTHOR
Simon Cozens, simon@cpan.org
SEE ALSO
B, perlguts, op.c