info > Data::Dumper

Data::Dumper(3perl) - Perl Programmers Reference Guide

📖 NAME

Data::Dumper - stringified perl data structures, suitable for both printing and "eval"

🚀 Quick Reference

Use CaseCommandDescription
Procedural Dumpprint Dumper($foo, $bar);Simple interface to dump variables
Named Dumpprint Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);Extended usage with user-specified names
OO Dump$d = Data::Dumper->new(...); $d->Dump;Object-oriented interface for complex dumping
Method Chaining$d->Purity(1)->Terse(1)->Deepcopy(1);Configure dump style via chained methods
Eval Reconstructioneval $d->Dump;Recreate original structure from dump
Self-referentiallocal $Data::Dumper::Purity = 1;Handle self-referential structures correctly
Pretty Printlocal $Data::Dumper::Indent = 2;Readable output with indentation (default)
Sort Keyslocal $Data::Dumper::Sortkeys = 1;Dump hash keys in sorted order
Code Deparselocal $Data::Dumper::Deparse = 1;Turn code refs into Perl source code
Deep Copylocal $Data::Dumper::Deepcopy = 1;Avoid cross-references in output
Max Depthlocal $Data::Dumper::Maxdepth = 3;Limit structure depth in output

📋 SYNOPSIS

use Data::Dumper;

# simple procedural interface
print Dumper($foo, $bar);

# extended usage with names
print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);

# configuration variables
{
  local $Data::Dumper::Purity = 1;
  eval Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);
}

# OO usage
$d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]);
   ...
print $d->Dump;
   ...
$d->Purity(1)->Terse(1)->Deepcopy(1);
eval $d->Dump;

📝 DESCRIPTION

Given a list of scalars or reference variables, writes out their contents in perl syntax. The references can also be objects. The content of each variable is output in a single Perl statement. Handles self-referential structures correctly.

The return value can be "eval"ed to get back an identical copy of the original reference structure. (Please do consider the security implications of eval'ing code from untrusted sources!)

Any references that are the same as one of those passed in will be named $VARn (where n is a numeric suffix), and other duplicate references to substructures within $VARn will be appropriately labeled using arrow notation. You can specify names for individual values to be dumped if you use the "Dump()" method, or you can change the default $VAR prefix to something else. See $Data::Dumper::Varname and $Data::Dumper::Terse below.

The default output of self-referential structures can be "eval"ed, but the nested references to $VARn will be undefined, since a recursive structure cannot be constructed using one Perl statement. You should set the "Purity" flag to 1 to get additional statements that will correctly fill in these references. Moreover, if "eval"ed when strictures are in effect, you need to ensure that any variables it accesses are previously declared.

In the extended usage form, the references to be dumped can be given user-specified names. If a name begins with a "*", the output will describe the dereferenced type of the supplied reference for hashes and arrays, and coderefs. Output of names will be avoided where possible if the "Terse" flag is set.

In many cases, methods that are used to set the internal state of the object will return the object itself, so method calls can be conveniently chained together.

Several styles of output are possible, all controlled by setting the "Indent" flag. See "Configuration Variables or Methods" below for details.

📌 Methods

📌 Functions

📌 Configuration Variables or Methods

Several configuration variables can be used to control the kind of output generated when using the procedural interface. These variables are usually "local"ized in a block so that other parts of the code are not affected by the change.

These variables determine the default state of the object created by calling the "new" method, but cannot be used to alter the state of the object thereafter. The equivalent method names should be used instead to query or set the internal state of the object.

The method forms return the object itself when called with arguments, so that they can be chained together nicely.

📌 Exports

Dumper

💡 EXAMPLES

use Data::Dumper;

package Foo;
sub new {bless {'a' => 1, 'b' => sub { return "foo" }}, $_[0]};

package Fuz;                       # a weird REF-REF-SCALAR object
sub new {bless \($_ = \ 'fu\'z'), $_[0]};

package main;
$foo = Foo->new;
$fuz = Fuz->new;
$boo = [ 1, [], "abcd", \*foo,
         {1 => 'a', 023 => 'b', 0x45 => 'c'},
         \\"p\q\'r", $foo, $fuz];

########
# simple usage
########

$bar = eval(Dumper($boo));
print($@) if $@;
print Dumper($boo), Dumper($bar);  # pretty print (no array indices)

$Data::Dumper::Terse = 1;        # don't output names where feasible
$Data::Dumper::Indent = 0;       # turn off all pretty print
print Dumper($boo), "\n";

$Data::Dumper::Indent = 1;       # mild pretty print
print Dumper($boo);

$Data::Dumper::Indent = 3;       # pretty print with array indices
print Dumper($boo);

$Data::Dumper::Useqq = 1;        # print strings in double quotes
print Dumper($boo);

$Data::Dumper::Pair = " : ";     # specify hash key/value separator
print Dumper($boo);

########
# recursive structures
########

@c = ('c');
$c = \@c;
$b = {};
$a = [1, $b, $c];
$b->{a} = $a;
$b->{b} = $a->[1];
$b->{c} = $a->[2];
print Data::Dumper->Dump([$a,$b,$c], [qw(a b c)]);

$Data::Dumper::Purity = 1;         # fill in the holes for eval
print Data::Dumper->Dump([$a, $b], [qw(*a b)]); # print as @a
print Data::Dumper->Dump([$b, $a], [qw(*b a)]); # print as %b

$Data::Dumper::Deepcopy = 1;       # avoid cross-refs
print Data::Dumper->Dump([$b, $a], [qw(*b a)]);

$Data::Dumper::Purity = 0;         # avoid cross-refs
print Data::Dumper->Dump([$b, $a], [qw(*b a)]);

########
# deep structures
########

$a = "pearl";
$b = [ $a ];
$c = { 'b' => $b };
$d = [ $c ];
$e = { 'd' => $d };
$f = { 'e' => $e };
print Data::Dumper->Dump([$f], [qw(f)]);

$Data::Dumper::Maxdepth = 3;       # no deeper than 3 refs down
print Data::Dumper->Dump([$f], [qw(f)]);

########
# object-oriented usage
########

$d = Data::Dumper->new([$a,$b], [qw(a b)]);
$d->Seen({'*c' => $c});            # stash a ref without printing it
$d->Indent(3);
print $d->Dump;
$d->Reset->Purity(0);              # empty the seen cache
print join "----\n", $d->Dump;

########
# persistence
########

package Foo;
sub new { bless { state => 'awake' }, shift }
sub Freeze {
    my $s = shift;
    print STDERR "preparing to sleep\n";
    $s->{state} = 'asleep';
    return bless $s, 'Foo::ZZZ';
}

package Foo::ZZZ;
sub Thaw {
    my $s = shift;
    print STDERR "waking up\n";
    $s->{state} = 'awake';
    return bless $s, 'Foo';
}

package main;
use Data::Dumper;
$a = Foo->new;
$b = Data::Dumper->new([$a], ['c']);
$b->Freezer('Freeze');
$b->Toaster('Thaw');
$c = $b->Dump;
print $c;
$d = eval $c;
print Data::Dumper->Dump([$d], ['d']);

########
# symbol substitution (useful for recreating CODE refs)
########

sub foo { print "foo speaking\n" }
*other = \&foo;
$bar = [ \&other ];
$d = Data::Dumper->new([\&other,$bar],['*other','bar']);
$d->Seen({ '*foo' => \&foo });
print $d->Dump;

########
# sorting and filtering hash keys
########

$Data::Dumper::Sortkeys = \&my_filter;
my $foo = { map { (ord, "$_$_$_") } 'I'..'Q' };
my $bar = { %$foo };
my $baz = { reverse %$foo };
print Dumper [ $foo, $bar, $baz ];

sub my_filter {
    my ($hash) = @_;
    # return an array ref containing the hash keys to dump
    # in the order that you want them to be dumped
    return [
      # Sort the keys of %$foo in reverse numeric order
        $hash eq $foo ? (sort {$b <=> $a} keys %$hash) :
      # Only dump the odd number keys of %$bar
        $hash eq $bar ? (grep {$_ % 2} keys %$hash) :
      # Sort keys in default order for all other hashes
        (sort keys %$hash)
    ];
}

🐛 BUGS

Due to limitations of Perl subroutine call semantics, you cannot pass an array or hash. Prepend it with a "\" to pass its reference instead. This will be remedied in time, now that Perl has subroutine prototypes. For now, you need to use the extended usage form, and prepend the name with a "*" to output it as a hash or array.

"Data::Dumper" cheats with CODE references. If a code reference is encountered in the structure being processed (and if you haven't set the "Deparse" flag), an anonymous subroutine that contains the string '"DUMMY"' will be inserted in its place, and a warning will be printed if "Purity" is set. You can "eval" the result, but bear in mind that the anonymous sub that gets created is just a placeholder. Even using the "Deparse" flag will in some cases produce results that behave differently after being passed to "eval"; see the documentation for B::Deparse.

SCALAR objects have the weirdest looking "bless" workaround.

Pure Perl version of "Data::Dumper" escapes UTF-8 strings correctly only in Perl 5.8.0 and later.

📝 NOTE

Starting from Perl 5.8.1 different runs of Perl will have different ordering of hash keys. The change was done for greater security, see "Algorithmic Complexity Attacks" in perlsec. This means that different runs of Perl will have different Data::Dumper outputs if the data contains hashes. If you need to have identical Data::Dumper outputs from different runs of Perl, use the environment variable PERL_HASH_SEED, see "PERL_HASH_SEED" in perlrun. Using this restores the old (platform-specific) ordering: an even prettier solution might be to use the "Sortkeys" filter of Data::Dumper.

👤 AUTHOR

Gurusamy Sarathy <gsar AT activestate.com>

Copyright (c) 1996-2019 Gurusamy Sarathy. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

🏷️ VERSION

Version 2.179

🔗 SEE ALSO

perl(1)

Data::Dumper
Data::Dumper(3perl) - Perl Programmers Reference Guide 📖 NAME 🚀 Quick Reference 📋 SYNOPSIS 📝 DESCRIPTION
📌 Methods 📌 Functions 📌 Configuration Variables or Methods 📌 Exports
💡 EXAMPLES 🐛 BUGS 📝 NOTE 👤 AUTHOR 🏷️ VERSION 🔗 SEE ALSO

Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-23 23:10 @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!