Plan 9 from Bell Labs’s /usr/web/sources/contrib/gabidiaz/root/sys/man/2perl/B::Deparse

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


.\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sh \" Subsection heading
.br
.if t .Sp
.ne 5
.PP
\fB\\$1\fR
.PP
..
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings.  \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote.  | will give a
.\" real vertical bar.  \*(C+ will give a nicer C++.  Capital omega is used to
.\" do unbreakable dashes and therefore won't be available.  \*(C` and \*(C'
.\" expand to `' in nroff, nothing in troff, for use with C<>.
.tr \(*W-|\(bv\*(Tr
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
.    ds -- \(*W-
.    ds PI pi
.    if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
.    if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\"  diablo 12 pitch
.    ds L" ""
.    ds R" ""
.    ds C` ""
.    ds C' ""
'br\}
.el\{\
.    ds -- \|\(em\|
.    ds PI \(*p
.    ds L" ``
.    ds R" ''
'br\}
.\"
.\" If the F register is turned on, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
.\" entries marked with X<> in POD.  Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.if \nF \{\
.    de IX
.    tm Index:\\$1\t\\n%\t"\\$2"
..
.    nr % 0
.    rr F
.\}
.\"
.\" For nroff, turn off justification.  Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.hy 0
.if n .na
.\"
.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
.\" Fear.  Run.  Save yourself.  No user-serviceable parts.
.    \" fudge factors for nroff and troff
.if n \{\
.    ds #H 0
.    ds #V .8m
.    ds #F .3m
.    ds #[ \f1
.    ds #] \fP
.\}
.if t \{\
.    ds #H ((1u-(\\\\n(.fu%2u))*.13m)
.    ds #V .6m
.    ds #F 0
.    ds #[ \&
.    ds #] \&
.\}
.    \" simple accents for nroff and troff
.if n \{\
.    ds ' \&
.    ds ` \&
.    ds ^ \&
.    ds , \&
.    ds ~ ~
.    ds /
.\}
.if t \{\
.    ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
.    ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
.    ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
.    ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
.    ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
.    ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
.\}
.    \" troff and (daisy-wheel) nroff accents
.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
.ds ae a\h'-(\w'a'u*4/10)'e
.ds Ae A\h'-(\w'A'u*4/10)'E
.    \" corrections for vroff
.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
.    \" for low resolution devices (crt and lpr)
.if \n(.H>23 .if \n(.V>19 \
\{\
.    ds : e
.    ds 8 ss
.    ds o a
.    ds d- d\h'-1'\(ga
.    ds D- D\h'-1'\(hy
.    ds th \o'bp'
.    ds Th \o'LP'
.    ds ae ae
.    ds Ae AE
.\}
.rm #[ #] #H #V #F C
.\" ========================================================================
.\"
.IX Title "B::Deparse 3"
.TH B::Deparse 3 "2002-11-24" "perl v5.8.0" "Perl Programmers Reference Guide"
.SH "NAME"
B::Deparse \- Perl compiler backend to produce perl code
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
\&\fBperl\fR \fB\-MO=Deparse\fR[\fB,\-u\fR\fI\s-1PACKAGE\s0\fR][\fB,\-p\fR][\fB,\-q\fR][\fB,\-l\fR]
        [\fB,\-s\fR\fI\s-1LETTERS\s0\fR][\fB,\-x\fR\fI\s-1LEVEL\s0\fR] \fIprog.pl\fR
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
B::Deparse is a backend module for the Perl compiler that generates
perl source code, based on the internal compiled structure that perl
itself creates after parsing a program. The output of B::Deparse won't
be exactly the same as the original source, since perl doesn't keep
track of comments or whitespace, and there isn't a one-to-one
correspondence between perl's syntactical constructions and their
compiled form, but it will often be close. When you use the \fB\-p\fR
option, the output also includes parentheses even when they are not
required by precedence, which can make it easy to see if perl is
parsing your expressions the way you intended.
.PP
Please note that this module is mainly new and untested code and is
still under development, so it may change in the future.
.SH "OPTIONS"
.IX Header "OPTIONS"
As with all compiler backend options, these must follow directly after
the '\-MO=Deparse', separated by a comma but not any white space.
.IP "\fB\-l\fR" 4
.IX Item "-l"
Add '#line' declarations to the output based on the line and file
locations of the original code.
.IP "\fB\-p\fR" 4
.IX Item "-p"
Print extra parentheses. Without this option, B::Deparse includes
parentheses in its output only when they are needed, based on the
structure of your program. With \fB\-p\fR, it uses parentheses (almost)
whenever they would be legal. This can be useful if you are used to
\&\s-1LISP\s0, or if you want to see how perl parses your input. If you say
.Sp
.Vb 3
\&    if ($var & 0x7f == 65) {print "Gimme an A!"} 
\&    print ($which ? $a : $b), "\en";
\&    $name = $ENV{USER} or "Bob";
.Ve
.Sp
\&\f(CW\*(C`B::Deparse,\-p\*(C'\fR will print
.Sp
.Vb 5
\&    if (($var & 0)) {
\&        print('Gimme an A!')
\&    };
\&    (print(($which ? $a : $b)), '???');
\&    (($name = $ENV{'USER'}) or '???')
.Ve
.Sp
which probably isn't what you intended (the \f(CW'???'\fR is a sign that
perl optimized away a constant value).
.IP "\fB\-P\fR" 4
.IX Item "-P"
Disable prototype checking. With this option, all function calls are
deparsed as if no prototype was defined for them. In other words,
.Sp
.Vb 1
\&    perl -MO=Deparse,-P -e 'sub foo (\e@) { 1 } foo @x'
.Ve
.Sp
will print
.Sp
.Vb 4
\&    sub foo (\e@) {
\&        1;
\&    }
\&    &foo(\e@x);
.Ve
.Sp
making clear how the parameters are actually passed to \f(CW\*(C`foo\*(C'\fR.
.IP "\fB\-q\fR" 4
.IX Item "-q"
Expand double-quoted strings into the corresponding combinations of
concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For
instance, print
.Sp
.Vb 1
\&    print "Hello, $world, @ladies, \eu$gentlemen\eE, \eu\eL$me!";
.Ve
.Sp
as
.Sp
.Vb 2
\&    print 'Hello, ' . $world . ', ' . join($", @ladies) . ', '
\&          . ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');
.Ve
.Sp
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
\&\f(CW\*(C`$x = "$y"\*(C'\fR is not the same as \f(CW\*(C`$x = $y\*(C'\fR: the former makes the value
of \f(CW$y\fR into a string before doing the assignment.
.IP "\fB\-f\fR\fI\s-1FILE\s0\fR" 4
.IX Item "-fFILE"
Normally, B::Deparse deparses the main code of a program, and all the subs
defined in the same file. To include subs defined in other files, pass the
\&\fB\-f\fR option with the filename. You can pass the \fB\-f\fR option several times, to
include more than one secondary file.  (Most of the time you don't want to
use it at all.)  You can also use this option to include subs which are
defined in the scope of a \fB#line\fR directive with two parameters.
.IP "\fB\-s\fR\fI\s-1LETTERS\s0\fR" 4
.IX Item "-sLETTERS"
Tweak the style of B::Deparse's output. The letters should follow
directly after the 's', with no space or punctuation. The following
options are available:
.RS 4
.IP "\fBC\fR" 4
.IX Item "C"
Cuddle \f(CW\*(C`elsif\*(C'\fR, \f(CW\*(C`else\*(C'\fR, and \f(CW\*(C`continue\*(C'\fR blocks. For example, print
.Sp
.Vb 5
\&    if (...) {
\&         ...
\&    } else {
\&         ...
\&    }
.Ve
.Sp
instead of
.Sp
.Vb 6
\&    if (...) {
\&         ...
\&    }
\&    else {
\&         ...
\&    }
.Ve
.Sp
The default is not to cuddle.
.IP "\fBi\fR\fI\s-1NUMBER\s0\fR" 4
.IX Item "iNUMBER"
Indent lines by multiples of \fI\s-1NUMBER\s0\fR columns. The default is 4 columns.
.IP "\fBT\fR" 4
.IX Item "T"
Use tabs for each 8 columns of indent. The default is to use only spaces.
For instance, if the style options are \fB\-si4T\fR, a line that's indented
3 times will be preceded by one tab and four spaces; if the options were
\&\fB\-si8T\fR, the same line would be preceded by three tabs.
.IP "\fBv\fR\fI\s-1STRING\s0\fR\fB.\fR" 4
.IX Item "vSTRING."
Print \fI\s-1STRING\s0\fR for the value of a constant that can't be determined
because it was optimized away (mnemonic: this happens when a constant
is used in \fBv\fRoid context). The end of the string is marked by a period.
The string should be a valid perl expression, generally a constant.
Note that unless it's a number, it probably needs to be quoted, and on
a command line quotes need to be protected from the shell. Some
conventional values include 0, 1, 42, '', 'foo', and
\&'Useless use of constant omitted' (which may need to be
\&\fB\-sv\*(L"'Useless use of constant omitted'.\*(R"\fR
or something similar depending on your shell). The default is '???'.
If you're using B::Deparse on a module or other file that's require'd,
you shouldn't use a value that evaluates to false, since the customary
true constant at the end of a module will be in void context when the
file is compiled as a main program.
.RE
.RS 4
.RE
.IP "\fB\-x\fR\fI\s-1LEVEL\s0\fR" 4
.IX Item "-xLEVEL"
Expand conventional syntax constructions into equivalent ones that expose
their internal operation. \fI\s-1LEVEL\s0\fR should be a digit, with higher values
meaning more expansion. As with \fB\-q\fR, this actually involves turning off
special cases in B::Deparse's normal operations.
.Sp
If \fI\s-1LEVEL\s0\fR is at least 3, for loops will be translated into equivalent
while loops with continue blocks; for instance
.Sp
.Vb 3
\&    for ($i = 0; $i < 10; ++$i) {
\&        print $i;
\&    }
.Ve
.Sp
turns into
.Sp
.Vb 6
\&    $i = 0;
\&    while ($i < 10) {
\&        print $i;
\&    } continue {
\&        ++$i
\&    }
.Ve
.Sp
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.
.Sp
If \fI\s-1LEVEL\s0\fR is at least 7, if statements will be translated into equivalent
expressions using \f(CW\*(C`&&\*(C'\fR, \f(CW\*(C`?:\*(C'\fR and \f(CW\*(C`do {}\*(C'\fR; for instance
.Sp
.Vb 9
\&    print 'hi' if $nice;
\&    if ($nice) {
\&        print 'hi';
\&    }
\&    if ($nice) {
\&        print 'hi';
\&    } else {
\&        print 'bye';
\&    }
.Ve
.Sp
turns into
.Sp
.Vb 3
\&    $nice and print 'hi';
\&    $nice and do { print 'hi' };
\&    $nice ? do { print 'hi' } : do { print 'bye' };
.Ve
.Sp
Long sequences of elsifs will turn into nested ternary operators, which
B::Deparse doesn't know how to indent nicely.
.SH "USING B::Deparse AS A MODULE"
.IX Header "USING B::Deparse AS A MODULE"
.Sh "Synopsis"
.IX Subsection "Synopsis"
.Vb 4
\&    use B::Deparse;
\&    $deparse = B::Deparse->new("-p", "-sC");
\&    $body = $deparse->coderef2text(\e&func);
\&    eval "sub func $body"; # the inverse operation
.Ve
.Sh "Description"
.IX Subsection "Description"
B::Deparse can also be used on a sub-by-sub basis from other perl
programs.
.Sh "new"
.IX Subsection "new"
.Vb 1
\&    $deparse = B::Deparse->new(OPTIONS)
.Ve
.PP
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 \*(L"\s-1OPTIONS\s0\*(R"); options that are separated by commas
after \fB\-MO=Deparse\fR should be given as separate strings. Some
options, like \fB\-u\fR, don't make sense for a single subroutine, so
don't pass them.
.Sh "ambient_pragmas"
.IX Subsection "ambient_pragmas"
.Vb 1
\&    $deparse->ambient_pragmas(strict => 'all', '$[' => $[);
.Ve
.PP
The compilation of a subroutine can be affected by a few compiler
directives, \fBpragmas\fR. These are:
.IP "\(bu" 4
use strict;
.IP "\(bu" 4
use warnings;
.IP "\(bu" 4
Assigning to the special variable $[
.IP "\(bu" 4
use integer;
.IP "\(bu" 4
use bytes;
.IP "\(bu" 4
use utf8;
.IP "\(bu" 4
use re;
.PP
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.
.PP
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 \fBambient_pragmas\fR method to describe the
assumptions you wish to make.
.PP
Not all of the options currently have any useful effect. See
\&\*(L"\s-1BUGS\s0\*(R" for more details.
.PP
The parameters it accepts are:
.IP "strict" 4
.IX Item "strict"
Takes a string, possibly containing several values separated
by whitespace. The special values \*(L"all\*(R" and \*(L"none\*(R" mean what you'd
expect.
.Sp
.Vb 1
\&    $deparse->ambient_pragmas(strict => 'subs refs');
.Ve
.IP "$[" 4
Takes a number, the value of the array base $[.
.IP "bytes" 4
.IX Item "bytes"
.PD 0
.IP "utf8" 4
.IX Item "utf8"
.IP "integer" 4
.IX Item "integer"
.PD
If the value is true, then the appropriate pragma is assumed to
be in the ambient scope, otherwise not.
.IP "re" 4
.IX Item "re"
Takes a string, possibly containing a whitespace-separated list of
values. The values \*(L"all\*(R" and \*(L"none\*(R" are special. It's also permissible
to pass an array reference here.
.Sp
.Vb 1
\&    $deparser->ambient_pragmas(re => 'eval');
.Ve
.IP "warnings" 4
.IX Item "warnings"
Takes a string, possibly containing a whitespace-separated list of
values. The values \*(L"all\*(R" and \*(L"none\*(R" are special, again. It's also
permissible to pass an array reference here.
.Sp
.Vb 1
\&    $deparser->ambient_pragmas(warnings => [qw[void io]]);
.Ve
.Sp
If one of the values is the string \*(L"\s-1FATAL\s0\*(R", then all the warnings
in that list will be considered fatal, just as with the \fBwarnings\fR
pragma itself. Should you need to specify that some warnings are
fatal, and others are merely enabled, you can pass the \fBwarnings\fR
parameter twice:
.Sp
.Vb 4
\&    $deparser->ambient_pragmas(
\&        warnings => 'all',
\&        warnings => [FATAL => qw/void io/],
\&    );
.Ve
.Sp
See perllexwarn for more information about lexical warnings. 
.IP "hint_bits" 4
.IX Item "hint_bits"
.PD 0
.IP "warning_bits" 4
.IX Item "warning_bits"
.PD
These two parameters are used to specify the ambient pragmas in
the format used by the special variables $^H and ${^WARNING_BITS}.
.Sp
They exist principally so that you can write code like:
.Sp
.Vb 7
\&    { 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 + $[
\&    ); }
.Ve
.Sp
which specifies that the ambient pragmas are exactly those which
are in scope at the point of calling.
.Sh "coderef2text"
.IX Subsection "coderef2text"
.Vb 2
\&    $body = $deparse->coderef2text(\e&func)
\&    $body = $deparse->coderef2text(sub ($$) { ... })
.Ve
.PP
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 \*(L"sub subname \*(R", or \*(L"sub \*(R"
for an anonymous function constructor. Unless the sub was defined in
the main:: package, the code will include a package declaration.
.SH "BUGS"
.IX Header "BUGS"
.IP "\(bu" 4
The only pragmas to be completely supported are: \f(CW\*(C`use warnings\*(C'\fR,
\&\f(CW\*(C`use strict 'refs'\*(C'\fR, \f(CW\*(C`use bytes\*(C'\fR, and \f(CW\*(C`use integer\*(C'\fR. (\f(CW$[\fR, which
behaves like a pragma, is also supported.)
.Sp
Excepting those listed above, we're currently unable to guarantee that
B::Deparse will produce a pragma at the correct point in the program.
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.
.IP "\(bu" 4
In fact, the above is a specific instance of a more general problem:
we can't guarantee to produce \s-1BEGIN\s0 blocks or \f(CW\*(C`use\*(C'\fR declarations in
exactly the right place. So if you use a module which affects compilation
(such as by over-riding keywords, overloading constants or whatever)
then the output code might not work as intended.
.Sp
This is the most serious outstanding problem, and will be very hard
to fix.
.IP "\(bu" 4
If a keyword is over\-ridden, and your program explicitly calls
the built-in version by using CORE::keyword, the output of B::Deparse
will not reflect this. If you run the resulting code, it will call
the over-ridden version rather than the built-in one. (Maybe there
should be an option to \fBalways\fR print keyword calls as \f(CW\*(C`CORE::name\*(C'\fR.)
.IP "\(bu" 4
\&\f(CW\*(C`sort foo (1, 2, 3)\*(C'\fR comes out as \f(CW\*(C`sort (foo 1, 2, 3)\*(C'\fR, which
causes perl to issue a warning.
.Sp
The obvious fix doesn't work, because these are different:
.Sp
.Vb 2
\&    print (FOO 1, 2, 3), 4, 5, 6;
\&    print FOO (1, 2, 3), 4, 5, 6;
.Ve
.IP "\(bu" 4
Constants (other than simple strings or numbers) don't work properly.
Pathological examples that fail (and probably always will) include:
.Sp
.Vb 2
\&    use constant E2BIG => ($!=7);
\&    use constant x=>\e$x; print x
.Ve
.Sp
The following could (and should) be made to work:
.Sp
.Vb 2
\&    use constant regex => qr/blah/;
\&    print regex;
.Ve
.IP "\(bu" 4
An input file that uses source filtering probably won't be deparsed into
runnable code, because it will still include the \fBuse\fR declaration
for the source filtering module, even though the code that is
produced is already ordinary Perl which shouldn't be filtered again.
.IP "\(bu" 4
There are probably many more bugs on non-ASCII platforms (\s-1EBCDIC\s0).
.SH "AUTHOR"
.IX Header "AUTHOR"
Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier
version by Malcolm Beattie <mbeattie@sable.ox.ac.uk>, with
contributions from Gisle Aas, James Duncan, Albert Dvornik, Robin
Houston, Hugo van der Sanden, Gurusamy Sarathy, Nick Ing\-Simmons,
and Rafael Garcia\-Suarez.

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.