B::Deparse - Perl compiler backend to produce perl code
While B::Deparse goes to some lengths to try to figure out what your original program was doing, some parts of the language can still trip it up; it still fails even on some parts of Perl's own test suite. If you encounter a failure other than the most common ones described in the BUGS section below, you can help contribute to B::Deparse's ongoing development by submitting a bug report with a small example.
if ($var & 0x7f == 65) {print "Gimme an A!"}
print ($which ? $a : $b), "\n";
$name = $ENV{USER} or "Bob";
"B::Deparse,-p" will print
if (($var & 0)) {
print('Gimme an A!')
};
(print(($which ? $a : $b)), '???');
(($name = $ENV{'USER'}) or '???')
which probably isn't what you intended (the '???' is a sign that perl optimized away a constant value).
perl -MO=Deparse,-P -e 'sub foo (\@) { 1 } foo @x'
will print
sub foo (\@) {
1;
}
&foo(\@x);
making clear how the parameters are actually passed to "foo".
print "Hello, $world, @ladies, \u$gentlemen\E, \u\L$me!";
as
print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
. ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
Note that the expanded form represents the way perl handles such constructions internally --- this option actually turns off the reverse translation that B::Deparse usually does. On the other hand, note that "$x = "$y"" is not the same as "$x = $y": the former makes the value of $y into a string before doing the assignment.
if (...) {
...
} else {
...
}
instead of
if (...) {
...
}
else {
...
}
The default is not to cuddle.
If LEVEL is at least 3, "for" loops will be translated into equivalent while loops with continue blocks; for instance
for ($i = 0; $i < 10; ++$i) {
print $i;
}
turns into
$i = 0;
while ($i < 10) {
print $i;
} continue {
++$i
}
Note that in a few cases this translation can't be perfectly carried back into the source code --- if the loop's initializer declares a my variable, for instance, it won't have the correct scope outside of the loop.
If LEVEL is at least 5, "use" declarations will be translated into "BEGIN" blocks containing calls to "require" and "import"; for instance,
use strict 'refs';
turns into
sub BEGIN {
require strict;
do {
'strict'->import('refs')
};
}
If LEVEL is at least 7, "if" statements will be translated into equivalent expressions using "&&", "?:" and "do {}"; for instance
print 'hi' if $nice;
if ($nice) {
print 'hi';
}
if ($nice) {
print 'hi';
} else {
print 'bye';
}
turns into
$nice and print 'hi';
$nice and do { print 'hi' };
$nice ? do { print 'hi' } : do { print 'bye' };
Long sequences of elsifs will turn into nested ternary operators, which B::Deparse doesn't know how to indent nicely.
use B::Deparse;
$deparse = B::Deparse->new("-p", "-sC");
$body = $deparse->coderef2text(\&func);
eval "sub func $body"; # the inverse operation
$deparse = B::Deparse->new(OPTIONS)
Create an object to store the state of a deparsing operation and any options. The options are the same as those that can be given on the command line (see ``OPTIONS''); options that are separated by commas after -MO=Deparse should be given as separate strings. Some options, like -u, don't make sense for a single subroutine, so don't pass them.
$deparse->ambient_pragmas(strict => 'all', '$[' => $[);
The compilation of a subroutine can be affected by a few compiler directives, pragmas. These are:
Ordinarily, if you use B::Deparse on a subroutine which has been compiled in the presence of one or more of these pragmas, the output will include statements to turn on the appropriate directives. So if you then compile the code returned by coderef2text, it will behave the same way as the subroutine which you deparsed.
However, you may know that you intend to use the results in a particular context, where some pragmas are already in scope. In this case, you use the ambient_pragmas method to describe the assumptions you wish to make.
Not all of the options currently have any useful effect. See ``BUGS'' for more details.
The parameters it accepts are:
$deparse->ambient_pragmas(strict => 'subs refs');
$deparser->ambient_pragmas(re => 'eval');
$deparser->ambient_pragmas(warnings => [qw[void io]]);
If one of the values is the string ``FATAL'', then all the warnings in that list will be considered fatal, just as with the warnings pragma itself. Should you need to specify that some warnings are fatal, and others are merely enabled, you can pass the warnings parameter twice:
$deparser->ambient_pragmas(
warnings => 'all',
warnings => [FATAL => qw/void io/],
);
See perllexwarn for more information about lexical warnings.
They exist principally so that you can write code like:
{ my ($hint_bits, $warning_bits);
BEGIN {($hint_bits, $warning_bits) = ($^H, ${^WARNING_BITS})}
$deparser->ambient_pragmas (
hint_bits => $hint_bits,
warning_bits => $warning_bits,
'$[' => 0 + $[
); }
which specifies that the ambient pragmas are exactly those which are in scope at the point of calling.
$body = $deparse->coderef2text(\&func)
$body = $deparse->coderef2text(sub ($$) { ... })
Return source code for the body of a subroutine (a block, optionally preceded by a prototype in parens), given a reference to the sub. Because a subroutine can have no names, or more than one name, this method doesn't return a complete subroutine definition --- if you want to eval the result, you should prepend ``sub subname '', or ``sub '' for an anonymous function constructor. Unless the sub was defined in the main:: package, the code will include a package declaration.
Excepting those listed above, we're currently unable to guarantee that B::Deparse will produce a pragma at the correct point in the program. (Specifically, pragmas at the beginning of a block often appear right before the start of the block instead.) Since the effects of pragmas are often lexically scoped, this can mean that the pragma holds sway over a different portion of the program than in the input file.
This is the most serious outstanding problem, and will require some help from the Perl core to fix.
use constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y;
my $x if 0;
which is not, consequently, deparsed correctly.
|
Закладки на сайте Проследить за страницей |
Created 1996-2025 by Maxim Chirkov Добавить, Поддержать, Вебмастеру |