# man > B::Deparse

---
type: CommandReference
command: B::Deparse
mode: man
section: 3perl
source: man-pages
---

## Quick Reference

- `perl -MO=Deparse prog.pl` — Deparse a Perl program to source code
- `perl -MO=Deparse,-p prog.pl` — Add parentheses to show parse structure
- `perl -MO=Deparse,-d prog.pl` — Use Data::Dumper (or built-in) for constants
- `perl -MO=Deparse,-f file.pl prog.pl` — Include subs from another file
- `perl -MO=Deparse,-l prog.pl` — Add `#line` directives to output
- `perl -MO=Deparse,-P prog.pl` — Show prototype and calling style for `&foo()` calls
- `perl -MO=Deparse,-q prog.pl` — Expand string operations (e.g., `join`, `ucfirst`)
- `perl -MO=Deparse,-sC -x5 prog.pl` — Cuddle `else`, expand `use` declarations

## Name

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

## Synopsis

**Command line:**
shell
perl -MO=Deparse[,-d][,-f FILE][,-p][,-q][,-l][,-s LETTERS][,-x LEVEL] prog.pl
**Module interface:**
perl
use B::Deparse;
$deparse = B::Deparse->new(OPTIONS);
$body = $deparse->coderef2text(\&func);
eval "sub func $body"; # inverse operation
## Options

Options follow directly after `-MO=Deparse`, separated by commas, no whitespace.

- `-d` — Use Data::Dumper for complex constants; otherwise built-in routines.
- `-f FILE` — Include subs from additional files. May be repeated.
- `-l` — Add `#line` directives to the output.
- `-p` — Add parentheses (almost) everywhere they are legal; shows parse structure.
- `-P` — Show prototype and calling style for `&foo()` calls.
- `-q` — Expand string operations: `print`, `join`, `ucfirst`, `lc`, `lcfirst`, `quotemeta`, `join`.
- `-s LETTERS` — Tweak output style. Options follow directly after `s`:
  - `C` — Cuddle `elsif`, `else`, `continue` blocks.
  - `iNUMBER` — Indent by NUMBER columns (default 4).
  - `T` — Use tabs every 8 columns (combined with `i`).
  - `vSTRING.` — Replace optimized-away constants with STRING (end with a period). Default `'???'`.
- `-x LEVEL` — Expand syntax constructions:
  - Level 3: `for` loops → `while` with `continue`.
  - Level 5: `use` → `BEGIN { require ...; import ... }`.
  - Level 7: `if` → `&&`, `?:`, `do {}`.

## Module Interface

### `new`
perl
$deparse = B::Deparse->new(OPTIONS)
Create a deparser object. OPTIONS are the same as command-line options, given as separate strings.

### `ambient_pragmas`
perl
$deparse->ambient_pragmas(strict => 'all', '$[' => $[);
Specify assumed pragmas for the ambient scope. Parameters:
- `strict` — String, e.g., `'subs refs'` or `'all'`.
- `$[` — Number (array base, obsolete).
- `bytes`, `utf8`, `integer` — Boolean.
- `re` — String or array ref, e.g., `'eval'`.
- `warnings` — String or array ref, e.g., `[qw/void io/]` or `[FATAL => qw/void io/]`.
- `hint_bits`, `warning_bits` — Raw values from `$^H` and `${^WARNING_BITS}`.
- `%^H` — Hash reference for extra hints.

### `coderef2text`
perl
$body = $deparse->coderef2text(\&func)
Return source code for the body of a subroutine (block, optionally with prototype). Includes a `package` declaration if not in `main::`. Prepending `sub subname` or `sub` yields a complete definition.

## Examples

**Using `-p` to see parse structure:**
shell
perl -MO=Deparse,-p -e 'if ($var & 0x7f == 65) {print "Gimme an A!"}'
Output:
perl
if (($var & 0)) {
    print('Gimme an A!')
};
**Expanding `for` loops (`-x3`):**
shell
perl -MO=Deparse,-x3 -e 'for ($i=0; $i<10; ++$i) { print $i }'
Output:
perl
$i = 0;
while ($i < 10) {
    print $i;
} continue {
    ++$i
}
**Expanding `use` declarations (`-x5`):**
shell
perl -MO=Deparse,-x5 -e 'use strict "refs"'
Output:
perl
sub BEGIN {
    require strict;
    do {
        'strict'->import('refs')
    };
}
## See Also

- [perlrun](https://perldoc.perl.org/perlrun) — for `-MO` switch
- [B](https://perldoc.perl.org/B) — the Perl compiler backend
- [Data::Dumper](https://perldoc.perl.org/Data::Dumper)
- [Data::Dump::Streamer](https://perldoc.perl.org/Data::Dump::Streamer) — serializes closures with PadWalker

## Bugs

- Only pragmas fully supported: `use warnings`, `use strict`, `use bytes`, `use integer`, `use feature`.
- Pragma placement may be incorrect (e.g., at block start instead of inside).
- `BEGIN` blocks and `use` declarations may not appear in the correct order.
- Dual-valued constants (e.g., from `use constant E2BIG => ($!=7)`) print incorrectly.
- Source-filtered input may not be deparsed into runnable code.
- Optimized-away statements are rendered as `'???'` (e.g., `my $x if 0`).
- Lexical (`my`) variables from outer scopes appear as package variables in `coderef2text`.
- Bugs on non-ASCII platforms (EBCDIC).