# perldoc > B::Deparse

---
type: CommandReference
command: B::Deparse
mode: perldoc
section: 3pm
source: perldoc
---

## Quick Reference

- `perl -MO=Deparse prog.pl` — deparse main code and subs in the same file
- `perl -MO=Deparse,-p prog.pl` — add extra parentheses to show parse structure
- `perl -MO=Deparse,-l prog.pl` — add `#line` directives
- `perl -MO=Deparse,-d prog.pl` — use `Data::Dumper` for constant values
- `perl -MO=Deparse,-q prog.pl` — expand double-quoted strings into concatenation/uc/join etc.
- `perl -MO=Deparse,-sC prog.pl` — cuddle `else`, `elsif`, `continue` blocks
- `perl -MO=Deparse,-x7 prog.pl` — expand `if` statements into `&&`, `?:`, `do {}`
- `$deparse = B::Deparse->new("-p", "-sC"); $body = $deparse->coderef2text(\&func)` — deparse a subroutine reference into source code

## Name

Perl compiler backend to produce perl code

## Synopsis

    perl -MO=Deparse[,-d][,-f*FILE*][,-p][,-q][,-l][,-s*LETTERS*][,-x*LEVEL*] *prog.pl*

    use B::Deparse;
    $deparse = B::Deparse->new(OPTIONS);
    $body = $deparse->coderef2text(\&func);

## Options

All options follow directly after `-MO=Deparse`, separated by a comma, no whitespace.

- `-d` — Output data values (constants) using `Data::Dumper` instead of built-in routines.
- `-f*FILE*` — Include subs defined in the given file. Can be specified multiple times.
- `-l` — Add `#line` declarations based on original line/file locations.
- `-p` — Print extra parentheses (almost always when legal). Useful for seeing how perl parses input.
- `-P` — Disable prototype checking; deparse all function calls as if no prototype was defined.
- `-q` — Expand double-quoted strings into concatenation, `uc`, `ucfirst`, `lc`, `lcfirst`, `quotemeta`, and `join`. Turns off the reverse translation B::Deparse usually does.
- `-s*LETTERS*` — Tweak output style. Letters follow directly after `s` with no space.
  - `C` — Cuddle `elsif`, `else`, `continue` blocks.
  - `i*NUMBER*` — Indent by multiples of NUMBER columns (default 4).
  - `T` — Use tabs for each 8 columns of indent.
  - `v*STRING*.` — Print STRING for constants optimized away in void context. End with a period. Default is `'???'`.
- `-x*LEVEL*` — Expand conventional syntax into equivalent internal forms. Higher LEVEL = more expansion.
  - Level >= 3: `for` loops → `while` loops with `continue` blocks.
  - Level >= 5: `use` declarations → `BEGIN` blocks with `require` and `import`.
  - Level >= 7: `if` statements → `&&`, `?:`, `do {}` expressions.

## Module Usage

### `new`

    $deparse = B::Deparse->new(OPTIONS)

Create an object to store deparsing state and options. Options are the same as command-line options, given as separate strings.

### `ambient_pragmas`

    $deparse->ambient_pragmas(strict => 'all', '$[' => $[);

Describe the pragmas assumed to be in scope when compiling the output. Parameters:

- `strict` — String (e.g., `'subs refs'`) or `'all'`/`'none'`.
- `$[` — Number (array base; obsolete, cannot be non-zero).
- `bytes` / `utf8` / `integer` — Boolean; true means the pragma is assumed ambient.
- `re` — String or array reference of values (e.g., `'eval'`).
- `warnings` — String or array reference (e.g., `[qw[void io]]`). Can be passed multiple times; use `FATAL` to mark warnings as fatal.
- `hint_bits` / `warning_bits` — Raw format of `$^H` and `${^WARNING_BITS}`.
- `%^H` — Hash to specify ambient pragmas stored in `%^H`.

### `coderef2text`

    $body = $deparse->coderef2text(\&func)
    $body = $deparse->coderef2text(sub ($$) { ... })

Return source code for the body of a subroutine (block, possibly with prototype). Does not include a `sub` declaration; prepend `sub subname ` or `sub ` for anonymous. Includes a `package` declaration if not in `main::`.

## Examples

**Deparse with extra parentheses:**

    $ perl -MO=Deparse,-p -e 'if ($var & 0x7f == 65) {print "hi"}'
    if (($var & 0)) {
        print('hi');
    };

**Deparse with cuddled else:**

    $ perl -MO=Deparse,-sC -e 'if ($x) {1} else {2}'
    if ($x) {
        1;
    } else {
        2;
    }

**Deparse expanding `for` loop:**

    $ perl -MO=Deparse,-x3 -e 'for ($i=0; $i<10; $i++) {print $i}'
    $i = 0;
    while ($i < 10) {
        print $i;
    } continue {
        ++$i;
    }

**Deparse expanding `use`:**

    $ perl -MO=Deparse,-x5 -e 'use strict "refs"'
    sub BEGIN {
        require strict;
        do {
            'strict'->import('refs');
        };
    }

**Programmatic deparsing of a sub:**

    use B::Deparse;
    $deparse = B::Deparse->new("-p", "-sC");
    $body = $deparse->coderef2text(\&main::some_sub);
    eval "sub some_sub $body";

## See Also

- [B::Deparse on perldoc](https://perldoc.perl.org/B::Deparse)
- [Data::Dumper](https://perldoc.perl.org/Data::Dumper)
- [Data::Dump::Streamer](https://perldoc.perl.org/Data::Dump::Streamer)
- [PadWalker](https://perldoc.perl.org/PadWalker)

## Bugs

- Only `use warnings`, `use strict`, `use bytes`, `use integer`, and `use feature` are fully supported. Other pragmas may appear at the wrong point.
- BEGIN blocks and `use` declarations may not be placed correctly.
- Some constants (e.g., dual-valued scalars) do not print correctly.
- Input files using source filtering will not produce runnable code.
- Optimized-away statements are rendered as `'???'` (e.g., `my $x if 0`, `foreach my $i (@_) { 0 }`).
- Lexical (`my`) variables from external scopes appear as package variables in `coderef2text` output.
- Bugs likely on non-ASCII platforms (EBCDIC).