info > B::Deparse

๐Ÿท๏ธ NAME

B::Deparse - Perl compiler backend to produce perl code

๐Ÿš€ Quick Reference

Use CaseCommandDescription
Deparse a Perl scriptperl -MO=Deparse prog.plGenerate readable Perl source from compiled code
Deparse with extra parenthesesperl -MO=Deparse,-p prog.plShow how Perl parses expressions (LISP-like)
Deparse with data dumperperl -MO=Deparse,-d prog.plUse Data::Dumper for constant values
Deparse a subroutine referenceuse B::Deparse; $deparse = B::Deparse->new; $code = $deparse->coderef2text(\&func);Get source code for a specific subroutine
Deparse with style optionsperl -MO=Deparse,-sC prog.plCuddle else blocks, adjust indentation

๐Ÿ“– SYNOPSIS

perl -MO=Deparse[,-d][,-fFILE][,-p][,-q][,-l]
        [,-sLETTERS][,-xLEVEL] prog.pl

๐Ÿ“ 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 won't be exactly the same as the original source, since perl doesn't keep track of comments or whitespace, but it will often be close. When you use the -p 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.

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.

โš™๏ธ OPTIONS

As with all compiler backend options, these must follow directly after the -MO=Deparse, separated by a comma but not any white space.

๐Ÿ”น -d

Output data values (when they appear as constants) using Data::Dumper. Without this option, B::Deparse will use some simple routines of its own for the same purpose. Currently, Data::Dumper is better for some kinds of data (such as complex structures with sharing and self-reference) while the built-in routines are better for others (such as odd floating-point values).

๐Ÿ”น -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 -f option with the filename. You can pass the -f 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 #line directive with two parameters.

๐Ÿ”น -l

Add #line declarations to the output based on the line and file locations of the original code.

๐Ÿ”น -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 -p, it uses parentheses (almost) whenever they would be legal. This can be useful if you are used to LISP, or if you want to see how perl parses your input. If you say

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

๐Ÿ”น -P

Disable prototype checking. With this option, all function calls are deparsed as if no prototype was defined for them. In other words,

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.

๐Ÿ”น -q

Expand double-quoted strings into the corresponding combinations of concatenation, uc, ucfirst, lc, lcfirst, quotemeta, and join. For instance, print

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.

๐Ÿ”น -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:

๐Ÿ”น -xLEVEL

Expand conventional syntax constructions into equivalent ones that expose their internal operation. LEVEL should be a digit, with higher values meaning more expansion. As with -q, this actually involves turning off special cases in B::Deparse's normal operations.

๐Ÿ“ฆ USING B::Deparse AS A MODULE

๐Ÿ“Œ Synopsis

use B::Deparse;
$deparse = B::Deparse->new("-p", "-sC");
$body = $deparse->coderef2text(\&func);
eval "sub func $body"; # the inverse operation

๐Ÿ“Œ Description

B::Deparse can also be used on a sub-by-sub basis from other perl programs.

๐Ÿ“Œ new

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

๐Ÿ“Œ ambient_pragmas

$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:

๐Ÿ“Œ coderef2text

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

๐Ÿ› BUGS

๐Ÿ‘ค AUTHOR

Stephen McCamant <smcc AT CSUA.EDU>, based on an earlier version by Malcolm Beattie <mbeattie AT sable.uk>, with contributions from Gisle Aas, James Duncan, Albert Dvornik, Robin Houston, Dave Mitchell, Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and Rafael Garcia-Suarez.

B::Deparse
๐Ÿท๏ธ NAME ๐Ÿš€ Quick Reference ๐Ÿ“– SYNOPSIS ๐Ÿ“ DESCRIPTION โš™๏ธ OPTIONS
๐Ÿ”น -d ๐Ÿ”น -fFILE ๐Ÿ”น -l ๐Ÿ”น -p ๐Ÿ”น -P ๐Ÿ”น -q ๐Ÿ”น -sLETTERS ๐Ÿ”น -xLEVEL
๐Ÿ“ฆ USING B::Deparse AS A MODULE
๐Ÿ“Œ Synopsis ๐Ÿ“Œ Description ๐Ÿ“Œ new ๐Ÿ“Œ ambient_pragmas ๐Ÿ“Œ coderef2text
๐Ÿ› BUGS ๐Ÿ‘ค AUTHOR

Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-24 04:52 @216.73.216.102
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!