The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

B::OP - Inspect and manipulate op trees.

DESCRIPTION

B::OP, B::UNOP, B::BINOP, B::LOGOP, B::LISTOP, B::PMOP, B::SVOP, B::PADOP, B::PVOP, B::LOOP, B::COP.

These classes correspond in the obvious way to the underlying C structures of similar names. The inheritance hierarchy mimics the underlying C "inheritance":

                                 B::OP
                                   |
                   +---------------+--------+--------+-------+
                   |               |        |        |       |
                B::UNOP          B::SVOP B::PADOP  B::COP  B::PVOP
                 ,'  `-.
                /       `--.
           B::BINOP     B::LOGOP
               |
               |
           B::LISTOP
             ,' `.
            /     \
        B::LOOP B::PMOP

Access methods correspond to the underlying C structre field names, with the leading "class indication" prefix ("op_") removed.

Most fields also have an set method, prefixed with "set_".

B::OP Methods

These methods get the values of similarly named fields within the OP data structure. See top of op.h for more info.

next
sibling
name

This returns the op name as a string (e.g. "add", "rv2av").

ppaddr

This returns the function name as a string (e.g. "PL_ppaddr[OP_ADD]", "PL_ppaddr[OP_RV2AV]").

desc

This returns the op description from the global C PL_op_desc array (e.g. "addition" "array deref").

targ
type
opt
flags
private
spare
free

Frees the opcode and all child opcodes. The object should not be used after this.

B::UNOP METHOD

first

B::BINOP METHOD

last

B::LOGOP METHOD

other

B::LISTOP METHOD

children

B::PMOP Methods

pmreplroot
pmreplstart
precomp
pmflags
reflags

Additional flags stored in regexp->extflags. Extension and partially overlapping with op->pmflags. Exactly the RXf_PMf_ flags, 0x800-0x1FFFF, are the same, the rest are new for the new matcher.

pmoffset

Only when perl was compiled with ithreads.

pmstashpv

Only when perl was compiled with ithreads.

pmstash

Only when perl was compiled without ithreads.

B::SVOP METHOD

sv
gv

B::PADOP METHOD

padix

B::PVOP METHOD

pv

B::LOOP Methods

redoop
nextop
lastop

B::COP Methods

label
stash
stashpv
file
cop_seq
line
warnings
io
hints

CREATING OPTREES

SYNOPSIS

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

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

WARNING

This module will create segmentation faults if you don't know how to use it properly. Further warning: 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. This is deliberate, rather than just through laziness. You are expected to have read the Perl and XS sources to this module before attempting to do anything with it.

Patches welcome.

DESCRIPTION

This module also allows you to create and manipulate the Perl optree in Perl space.

Well, if you're intimately familiar with Perl's internals, you can.

B::OP turns B's accessor methods into get-set methods. Hence, instead of merely saying

    $op2 = $op->next;

you can now say

    $op->set_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.

    B::OP->new     ( type, flags )
    B::UNOP->new   ( type, flags, first )
    B::BINOP->new  ( type, flags, first, last )
    B::LOGOP->new  ( type, flags, first, other )
    B::LISTOP->new ( type, flags, first, last )
    B::COP->new    ( 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")

(Incidentally, if you know about custom ops and have registed them properly with the interpreter, you can create custom ops by name: B::OP-new("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 = B::UNOP->new("negate", 0, undef);
    # ... create some more ops ...
    $x->first($y);

In addition, one may create a new nextstate operator with

    B::op->newstate ( 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::set_main_root and B::set_main_start functions.

This module can obviously be used for all sorts of fun purposes. The best one will be in conjuction compilation subs.

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 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)

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.

EXPORT

None.

AUTHOR

Malcolm Beattie, mbeattie@sable.ox.ac.uk Simon Cozens, simon@cpan.org (Who else?)

MAINTAINERS

This module is a merge of B-Generate and the B::OP part of B.

Josh ben Jore, Michael Schwern, Jim Cromie, Scott Walters, Gerard Goossen.

LICENSE

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

SEE ALSO

B, perlguts, op.c