{
    "mode": "perldoc",
    "parameter": "B::Deparse",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/B%3A%3ADeparse/json",
    "generated": "2026-06-03T05:25:47Z",
    "synopsis": "perl -MO=Deparse[,-d][,-f*FILE*][,-p][,-q][,-l] [,-s*LETTERS*][,-x*LEVEL*] *prog.pl*",
    "sections": {
        "NAME": {
            "content": "B::Deparse - Perl compiler backend to produce perl code\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "perl -MO=Deparse[,-d][,-f*FILE*][,-p][,-q][,-l] [,-s*LETTERS*][,-x*LEVEL*] *prog.pl*\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "B::Deparse is a backend module for the Perl compiler that generates perl source code, based on\nthe internal compiled structure that perl itself creates after parsing a program. The output of\nB::Deparse won't be exactly the same as the original source, since perl doesn't keep track of\ncomments or whitespace, and there isn't a one-to-one correspondence between perl's syntactical\nconstructions and their compiled form, but it will often be close. When you use the -p option,\nthe output also includes parentheses even when they are not required by precedence, which can\nmake it easy to see if perl is parsing your expressions the way you intended.\n\nWhile B::Deparse goes to some lengths to try to figure out what your original program was doing,\nsome parts of the language can still trip it up; it still fails even on some parts of Perl's own\ntest suite. If you encounter a failure other than the most common ones described in the BUGS\nsection below, you can help contribute to B::Deparse's ongoing development by submitting a bug\nreport with a small example.\n",
            "subsections": []
        },
        "OPTIONS": {
            "content": "As with all compiler backend options, these must follow directly after the '-MO=Deparse',\nseparated by a comma but not any white space.\n\n-d  Output data values (when they appear as constants) using Data::Dumper. Without this option,\nB::Deparse will use some simple routines of its own for the same purpose. Currently,\nData::Dumper is better for some kinds of data (such as complex structures with sharing and\nself-reference) while the built-in routines are better for others (such as odd\nfloating-point values).\n\n-f*FILE*\nNormally, B::Deparse deparses the main code of a program, and all the subs defined in the\nsame file. To include subs defined in other files, pass the -f option with the filename. You\ncan pass the -f option several times, to include more than one secondary file. (Most of the\ntime you don't want to use it at all.) You can also use this option to include subs which\nare defined in the scope of a #line directive with two parameters.\n\n-l  Add '#line' declarations to the output based on the line and file locations of the original\ncode.\n\n-p  Print extra parentheses. Without this option, B::Deparse includes parentheses in its output\nonly when they are needed, based on the structure of your program. With -p, it uses\nparentheses (almost) whenever they would be legal. This can be useful if you are used to\nLISP, or if you want to see how perl parses your input. If you say\n\nif ($var & 0x7f == 65) {print \"Gimme an A!\"}\nprint ($which ? $a : $b), \"\\n\";\n$name = $ENV{USER} or \"Bob\";\n\n\"B::Deparse,-p\" will print\n\nif (($var & 0)) {\nprint('Gimme an A!')\n};\n(print(($which ? $a : $b)), '???');\n(($name = $ENV{'USER'}) or '???')\n\nwhich probably isn't what you intended (the '???' is a sign that perl optimized away a\nconstant value).\n\n-P  Disable prototype checking. With this option, all function calls are deparsed as if no\nprototype was defined for them. In other words,\n\nperl -MO=Deparse,-P -e 'sub foo (\\@) { 1 } foo @x'\n\nwill print\n\nsub foo (\\@) {\n1;\n}\n&foo(\\@x);\n\nmaking clear how the parameters are actually passed to \"foo\".\n\n-q  Expand double-quoted strings into the corresponding combinations of concatenation, uc,\nucfirst, lc, lcfirst, quotemeta, and join. For instance, print\n\nprint \"Hello, $world, @ladies, \\u$gentlemen\\E, \\u\\L$me!\";\n\nas\n\nprint 'Hello, ' . $world . ', ' . join($\", @ladies) . ', '\n. ucfirst($gentlemen) . ', ' . ucfirst(lc $me . '!');\n\nNote that the expanded form represents the way perl handles such constructions internally --\nthis option actually turns off the reverse translation that B::Deparse usually does. On the\nother hand, note that \"$x = \"$y\"\" is not the same as \"$x = $y\": the former makes the value\nof $y into a string before doing the assignment.\n\n-s*LETTERS*\nTweak the style of B::Deparse's output. The letters should follow directly after the 's',\nwith no space or punctuation. The following options are available:\n\nC   Cuddle \"elsif\", \"else\", and \"continue\" blocks. For example, print\n\nif (...) {\n...\n} else {\n...\n}\n\ninstead of\n\nif (...) {\n...\n}\nelse {\n...\n}\n\nThe default is not to cuddle.\n\ni*NUMBER*\nIndent lines by multiples of *NUMBER* columns. The default is 4 columns.\n\nT   Use tabs for each 8 columns of indent. The default is to use only spaces. For instance,\nif the style options are -si4T, a line that's indented 3 times will be preceded by one\ntab and four spaces; if the options were -si8T, the same line would be preceded by three\ntabs.\n\nv*STRING*.\nPrint *STRING* for the value of a constant that can't be determined because it was\noptimized away (mnemonic: this happens when a constant is used in void context). The end\nof the string is marked by a period. The string should be a valid perl expression,\ngenerally a constant. Note that unless it's a number, it probably needs to be quoted,\nand on a command line quotes need to be protected from the shell. Some conventional\nvalues include 0, 1, 42, '', 'foo', and 'Useless use of constant omitted' (which may\nneed to be -sv\"'Useless use of constant omitted'.\" or something similar depending on\nyour shell). The default is '???'. If you're using B::Deparse on a module or other file\nthat's require'd, you shouldn't use a value that evaluates to false, since the customary\ntrue constant at the end of a module will be in void context when the file is compiled\nas a main program.\n\n-x*LEVEL*\nExpand conventional syntax constructions into equivalent ones that expose their internal\noperation. *LEVEL* should be a digit, with higher values meaning more expansion. As with -q,\nthis actually involves turning off special cases in B::Deparse's normal operations.\n\nIf *LEVEL* is at least 3, \"for\" loops will be translated into equivalent while loops with\ncontinue blocks; for instance\n\nfor ($i = 0; $i < 10; ++$i) {\nprint $i;\n}\n\nturns into\n\n$i = 0;\nwhile ($i < 10) {\nprint $i;\n} continue {\n++$i\n}\n\nNote that in a few cases this translation can't be perfectly carried back into the source\ncode -- if the loop's initializer declares a my variable, for instance, it won't have the\ncorrect scope outside of the loop.\n\nIf *LEVEL* is at least 5, \"use\" declarations will be translated into \"BEGIN\" blocks\ncontaining calls to \"require\" and \"import\"; for instance,\n\nuse strict 'refs';\n\nturns into\n\nsub BEGIN {\nrequire strict;\ndo {\n'strict'->import('refs')\n};\n}\n\nIf *LEVEL* is at least 7, \"if\" statements will be translated into equivalent expressions\nusing \"&&\", \"?:\" and \"do {}\"; for instance\n\nprint 'hi' if $nice;\nif ($nice) {\nprint 'hi';\n}\nif ($nice) {\nprint 'hi';\n} else {\nprint 'bye';\n}\n\nturns into\n\n$nice and print 'hi';\n$nice and do { print 'hi' };\n$nice ? do { print 'hi' } : do { print 'bye' };\n\nLong sequences of elsifs will turn into nested ternary operators, which B::Deparse doesn't\nknow how to indent nicely.\n\nUSING B::Deparse AS A MODULE",
            "subsections": [
                {
                    "name": "Synopsis",
                    "content": "use B::Deparse;\n$deparse = B::Deparse->new(\"-p\", \"-sC\");\n$body = $deparse->coderef2text(\\&func);\neval \"sub func $body\"; # the inverse operation\n"
                },
                {
                    "name": "Description",
                    "content": "B::Deparse can also be used on a sub-by-sub basis from other perl programs.\n\nnew\n$deparse = B::Deparse->new(OPTIONS)\n\nCreate an object to store the state of a deparsing operation and any options. The options are\nthe same as those that can be given on the command line (see \"OPTIONS\"); options that are\nseparated by commas after -MO=Deparse should be given as separate strings.\n\nambientpragmas\n$deparse->ambientpragmas(strict => 'all', '$[' => $[);\n\nThe compilation of a subroutine can be affected by a few compiler directives, pragmas. These\nare:\n\n*   use strict;\n\n*   use warnings;\n\n*   Assigning to the special variable $[\n\n*   use integer;\n\n*   use bytes;\n\n*   use utf8;\n\n*   use re;\n\nOrdinarily, if you use B::Deparse on a subroutine which has been compiled in the presence of one\nor more of these pragmas, the output will include statements to turn on the appropriate\ndirectives. So if you then compile the code returned by coderef2text, it will behave the same\nway as the subroutine which you deparsed.\n\nHowever, you may know that you intend to use the results in a particular context, where some\npragmas are already in scope. In this case, you use the ambientpragmas method to describe the\nassumptions you wish to make.\n\nNot all of the options currently have any useful effect. See \"BUGS\" for more details.\n\nThe parameters it accepts are:\n\nstrict\nTakes a string, possibly containing several values separated by whitespace. The special\nvalues \"all\" and \"none\" mean what you'd expect.\n\n$deparse->ambientpragmas(strict => 'subs refs');\n\n$[  Takes a number, the value of the array base $[. Obsolete: cannot be non-zero.\n\nbytes\nutf8\ninteger\nIf the value is true, then the appropriate pragma is assumed to be in the ambient scope,\notherwise not.\n\nre  Takes a string, possibly containing a whitespace-separated list of values. The values \"all\"\nand \"none\" are special. It's also permissible to pass an array reference here.\n\n$deparser->ambientpragmas(re => 'eval');\n\nwarnings\nTakes a string, possibly containing a whitespace-separated list of values. The values \"all\"\nand \"none\" are special, again. It's also permissible to pass an array reference here.\n\n$deparser->ambientpragmas(warnings => [qw[void io]]);\n\nIf one of the values is the string \"FATAL\", then all the warnings in that list will be\nconsidered fatal, just as with the warnings pragma itself. Should you need to specify that\nsome warnings are fatal, and others are merely enabled, you can pass the warnings parameter\ntwice:\n\n$deparser->ambientpragmas(\nwarnings => 'all',\nwarnings => [FATAL => qw/void io/],\n);\n\nSee warnings for more information about lexical warnings.\n\nhintbits\nwarningbits\nThese two parameters are used to specify the ambient pragmas in the format used by the\nspecial variables $^H and ${^WARNINGBITS}.\n\nThey exist principally so that you can write code like:\n\n{ my ($hintbits, $warningbits);\nBEGIN {($hintbits, $warningbits) = ($^H, ${^WARNINGBITS})}\n$deparser->ambientpragmas (\nhintbits    => $hintbits,\nwarningbits => $warningbits,\n'$['         => 0 + $[\n); }\n\nwhich specifies that the ambient pragmas are exactly those which are in scope at the point\nof calling.\n\n%^H This parameter is used to specify the ambient pragmas which are stored in the special hash\n%^H.\n\ncoderef2text\n$body = $deparse->coderef2text(\\&func)\n$body = $deparse->coderef2text(sub ($$) { ... })\n\nReturn source code for the body of a subroutine (a block, optionally preceded by a prototype in\nparens), given a reference to the sub. Because a subroutine can have no names, or more than one\nname, this method doesn't return a complete subroutine definition -- if you want to eval the\nresult, you should prepend \"sub subname \", or \"sub \" for an anonymous function constructor.\nUnless the sub was defined in the main:: package, the code will include a package declaration.\n"
                }
            ]
        },
        "BUGS": {
            "content": "*   The only pragmas to be completely supported are: \"use warnings\", \"use strict\", \"use bytes\",\n\"use integer\" and \"use feature\".\n\nExcepting those listed above, we're currently unable to guarantee that B::Deparse will\nproduce a pragma at the correct point in the program. (Specifically, pragmas at the\nbeginning of a block often appear right before the start of the block instead.) Since the\neffects of pragmas are often lexically scoped, this can mean that the pragma holds sway over\na different portion of the program than in the input file.\n\n*   In fact, the above is a specific instance of a more general problem: we can't guarantee to\nproduce BEGIN blocks or \"use\" declarations in exactly the right place. So if you use a\nmodule which affects compilation (such as by over-riding keywords, overloading constants or\nwhatever) then the output code might not work as intended.\n\n*   Some constants don't print correctly either with or without -d. For instance, neither\nB::Deparse nor Data::Dumper know how to print dual-valued scalars correctly, as in:\n\nuse constant E2BIG => ($!=7); $y = E2BIG; print $y, 0+$y;\n\nuse constant H => { \"#\" => 1 }; H->{\"#\"};\n\n*   An input file that uses source filtering probably won't be deparsed into runnable code,\nbecause it will still include the use declaration for the source filtering module, even\nthough the code that is produced is already ordinary Perl which shouldn't be filtered again.\n\n*   Optimized-away statements are rendered as '???'. This includes statements that have a\ncompile-time side-effect, such as the obscure\n\nmy $x if 0;\n\nwhich is not, consequently, deparsed correctly.\n\nforeach my $i (@) { 0 }\n=>\nforeach my $i (@) { '???' }\n\n*   Lexical (my) variables declared in scopes external to a subroutine appear in coderef2text\noutput text as package variables. This is a tricky problem, as perl has no native facility\nfor referring to a lexical variable defined within a different scope, although PadWalker is\na good start.\n\nSee also Data::Dump::Streamer, which combines B::Deparse and PadWalker to serialize closures\nproperly.\n\n*   There are probably many more bugs on non-ASCII platforms (EBCDIC).\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Stephen McCamant <smcc@CSUA.Berkeley.EDU>, based on an earlier version by Malcolm Beattie\n<mbeattie@sable.ox.ac.uk>, with contributions from Gisle Aas, James Duncan, Albert Dvornik,\nRobin Houston, Dave Mitchell, Hugo van der Sanden, Gurusamy Sarathy, Nick Ing-Simmons, and\nRafael Garcia-Suarez.\n",
            "subsections": []
        }
    },
    "summary": "B::Deparse - Perl compiler backend to produce perl code",
    "flags": [],
    "examples": [],
    "see_also": []
}