Data::Dumper#
Stringify Perl data structures into valid Perl code - feed the result back through eval and you rebuild the original structure.
Two calling styles cover almost everything:
Functional:
Dumper(\%h, \@a)- one call, one string, uses the package-global configuration variables ($Data::Dumper::Indentetc.).Object-oriented:
Data::Dumper->new([\%h, \@a], ['hash', 'array'])returns a configurable dumper object. Chain accessors to tune output, then call->Dumpto produce the string.
The main knobs:
$Indent-0compact,1line-wrapped,2line-wrapped with alignment (the default and the most readable).$Purity- when true, round-trip fidelity for self-referential and cyclic structures at the cost of extra fixup statements.$Sortkeys-1for alphabetical, or a coderef returning the key order per hash.$Deepcopy- break shared references and emit each one as a copy.$Terse- omit the$VAR1 =prefix; emits a bare expression.$Useqq- quote strings with""and visible escapes (\n,\t,\x{...}) instead of'...'.$Sparseseen- skip populating the internal “seen” hash for values that the dumper can prove are never revisited. Serialization gets faster; the price is that the contents of the seen hash, as read throughSeen, can no longer be relied on.
Cycles and shared references are tracked through an internal seen table keyed by referent address; a second visit prints as a backref like $VAR1->[0] instead of recursing forever.
The full surface is the exported Dumper, the constructor new, the serializers Dump and Dumpxs, the seen-table controls Reset and Seen, the input accessors Values and Names, and the configuration accessors listed above. Dump and Dumpxs take the same arguments and produce the same output; Dumpxs is the faster of the two, and Dump delegates to it unless $Data::Dumper::Useperl is true.
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;
print Dumper({ name => 'pperl', list => [1, 2, 'three'] });
# $VAR1 = {
# 'list' => [
# 1,
# 2,
# 'three'
# ],
# 'name' => 'pperl'
# };
my $d = Data::Dumper->new([[1, 2]], ['pair']);
print $d->Indent(0)->Dump, "\n"; # $pair = [1,2];
{
local $Data::Dumper::Indent = 0;
local $Data::Dumper::Terse = 1;
print Dumper([1, { a => undef }]), "\n"; # [1,{'a' => undef}]
}
This is a native implementation of the XS half of Data::Dumper 2.192; its Data/Dumper.pm is used unchanged, and use Data::Dumper exports Dumper.
Differences from upstream#
None.
Functions#
Core API#
Dumpxs#
Serialize one or more values into valid Perl code, fast.