B::Utils(3pm) | User Contributed Perl Documentation | B::Utils(3pm) |
B::Utils - Helper functions for op tree manipulation
version 0.27
To install this module, run the following commands:
perl Makefile.PL make make test make install
use B::Utils;
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.
Warning: Since 5.21.2 B comes with its own version of B::OP::parent which returns either B::NULL or the real parent when ccflags contains -DPERL_OP_PARENT. In this case rather use $op->_parent.
You can control which properties of the op to include in the pattern by passing named arguments. The default behaviour is as if you passed in the following options:
my $pattern = $op->as_opgrep_pattern( attributes => [qw(name flags)], max_recursion_depth => undef, );
So obviously, you can set "max_recursion_depth" to a number to limit the maximum depth of recursion into the op tree. Setting it to 0 will limit the dump to the current op.
"attributes" is a list of attributes to include in the produced pattern. The attributes that can be checked against in this way are:
name targ type seq flags private pmflags pmpermflags.
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...
All the "walk" functions set $B::Utils::file, $B::Utils::line, and $B::Utils::sub to the appropriate values of file, line number, and sub name in the program being examined.
@barewords = opgrep( { name => "const", private => OPpCONST_BARE }, @ops );
where the first argument to "opgrep()" is the condition to be matched against the op structure. We'll henceforth refer to it as an op-pattern.
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 as nested patterns.
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 in this way:
name targ type seq flags private pmflags pmpermflags first other last sibling next pmreplroot pmreplstart pmnext
Additionally, you can use the "kids" keyword with an array reference to match the result of a call to "$op->kids()". An example use is given in the documentation for "op_or" below.
For debugging, you can have many properties of an op that is currently being matched against a given condition dumped to STDERR by specifying "dump =" 1> in the condition's hash reference.
If you match a complex condition against an op tree, you may want to extract a specific piece of information from the tree if the condition matches. This normally entails manually walking the tree a second time down to the op you wish to extract, investigate or modify. Since this is tedious duplication of code and information, you can specify a special property in the pattern of the op you wish to extract to capture the sub-op of interest. Example:
my ($result) = opgrep( { name => "exec", next => { name => "nextstate", sibling => { name => [qw(! exit warn die)] capture => "notreached", }, } }, $root_op ); if ($result) { my $name = $result->{notreached}->name; # result is *not* the root op carp("Statement unlikely to be reached (op name: $name)"); carp("\t(Maybe you meant system() when you said exec()?)\n"); }
While the above is a terribly contrived example, consider the win for a deeply nested pattern or worse yet, a pattern with many disjunctions. If a "capture" property is found anywhere in the op pattern, "opgrep()" returns an unblessed hash reference on success instead of the tested op. You can tell them apart using Scalar::Util's "blessed()". That hash reference contains all captured ops plus the tested root up as the hash entry "$result->{op}". Note that you cannot use this feature with "walkoptree_filtered" since that function was specifically documented to pass the tested op itself to the callback.
You cannot capture disjunctions, but that doesn't really make sense anyway.
Example:
my $sub_structure = { name => 'helem', first => { name => 'rv2hv', }, 'last' => { name => 'const', }, }; my @ops = opgrep( { name => 'leavesub', first => { name => 'lineseq', kids => [, { name => 'nextstate', }, op_or( { name => 'return', first => { name => 'pushmark' }, last => $sub_structure, }, $sub_structure, ), ], }, }, $op_obj );
This example matches the code in a typical simplest-possible accessor method (albeit not down to the last bit):
sub get_foo { $_[0]->{foo} }
But by adding an alternation we can also match optional op layers. In this case, we optionally match a return statement, so the following implementation is also recognized:
sub get_foo { return $_[0]->{foo} }
Essentially, this is syntactic sugar for the following structure recognized by "opgrep()":
{ disjunction => [@conditions] }
None by default.
This modules uses ExtUtils::Depends to export some useful functions for XS modules to use. To use those, include in your Makefile.PL:
my $pkg = ExtUtils::Depends->new("Your::XSModule", "B::Utils"); WriteMakefile( ... # your normal makefile flags $pkg->get_makefile_vars, );
Your XS module can now include BUtils.h and BUtils_op.h. To see document for the functions provided, use:
perldoc -m B::Utils::Install::BUtils.h perldoc -m B::Utils::Install::BUtils_op.h
Originally written by Simon Cozens, "simon@cpan.org" Maintained by Joshua ben Jore, "jjore@cpan.org"
Contributions from Mattia Barbon, Jim Cromie, Steffen Mueller, and Chia-liang Kao, Alexandr Ciornii, Reini Urban.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
B, B::Generate.
2022-10-19 | perl v5.36.0 |