| B::Utils - Helper functions for op tree manipulation |
B::Utils - Helper functions for op tree manipulation
use B::Utils;
These functions make it easier to manipulate the op tree.
all_startsall_roots__MAIN__.
Note: Certain ``dangerous'' stashes are not scanned for subroutines:
the list of such stashes can be found in @B::Utils::bad_stashes. Feel
free to examine and/or modify this to suit your needs. The intention is
that a simple program which uses no modules other than B and
B::Utils would show no addition symbols.
This does not return the details of ops in anonymous subroutines compiled at compile time. For instance, given
$a = sub { ... };
the subroutine will not appear in the hash. This is just as well, since they're anonymous... If you want to get at them, use...
anon_subs()$op->oldname$op->kids$op->parentCHECK
block. (and hence, if we have valid next pointers.)
In the future, it may be possible to search for the parent before we have the
next pointers in place, but it'll take me a while to figure out how to do
that.
$op->previous$op->next, but not quite.
B module provides various functions to walk the op tree, but
they're all rather difficult to use, requiring you to inject methods
into the B::OP class. This is a very simple op tree walker with
more expected semantics.
All the walk functions set B::Utils::file and B::Utils::line
to the appropriate values of file and line number in the program
being examined.
walkoptree_simple, but will only call the
callback if the filter returns true. The filter is passed the
op in question as a parameter; the opgrep function is fantastic
for building your own filters.
walkoptree_simple with all_roots and anon_subs
to examine every op in the program. $B::Utils::sub is set to the
subroutine name if you're in a subroutine, __MAIN__ if you're in
the main program and __ANON__ if you're in an anonymous subroutine.
carp(@args)croak(@args)
@barewords = opgrep(
{ name => "const", private => OPpCONST_BARE },
@ops
);
You can specify alternation by giving an arrayref of values:
@svs = opgrep ( { name => ["padsv", "gvsv"] }, @ops)
And you can specify inversion by making the first element of the arrayref
a ``!''. (Hint: if you want to say ``anything'', say ``not nothing'': ["!"])
You may also specify the conditions to be matched in nearby ops.
walkallops_filtered(
sub { opgrep( {name => "exec",
next => {
name => "nextstate",
sibling => { name => [qw(! exit warn die)] }
}
}, @_)},
sub {
carp("Statement unlikely to be reached");
carp("\t(Maybe you meant system() when you said exec()?)\n");
}
)
Get that?
Here are the things that can be tested:
name targ type seq flags private pmflags pmpermflags
first other last sibling next pmreplroot pmreplstart pmnext
None by default.
Simon Cozens, simon@cpan.org
I need to add more Fun Things, and possibly clean up some parts where the (previous/parent) algorithm has catastrophic cases, but it's more important to get this out right now than get it right.
the B manpage, the B::Generate manpage.
| B::Utils - Helper functions for op tree manipulation |