dumpxs#
Serialize one or more values into valid Perl code, fast.
Dumpxs is the XS counterpart of Dump: same inputs, same output,
implemented without the pure-Perl recursion overhead. Dump itself
delegates here unless $Data::Dumper::Useperl is true.
Synopsis#
use Data::Dumper;
print Data::Dumper->new([\%h, \@a], ['hash', 'array'])->Dumpxs;
print Data::Dumper::Dumpxs('Data::Dumper', [\%h], ['hash']);
What you get back#
In scalar context, a single string containing one $VAR = ...;
statement per input value, separated by $self->{sep} (default
"\n"). In list context, one string per input — useful when you
want to post-process each dump separately.
With Terse(1), the $VAR = prefix and trailing ; are dropped
and you get a bare Perl expression suitable for embedding.
Examples#
Named output, default formatting:
my %h = (a => 1, b => [2, 3]);
print Data::Dumper->new([\%h], ['h'])->Dumpxs;
## $h = {
## 'a' => 1,
## 'b' => [2, 3]
## };
Compact, sorted, double-quoted:
print Data::Dumper->new([\%h])
->Indent(0)->Sortkeys(1)->Useqq(1)->Dumpxs;
## $VAR1 = {"a" => 1,"b" => [2,3]};
Circular structure — the cycle is emitted as a backref, not an infinite expansion:
my @a = (1, 2);
push @a, \@a;
print Data::Dumper->new([\@a])->Dumpxs;
## $VAR1 = [1, 2, $VAR1];
List context gives you one string per argument:
my ($s1, $s2) = Data::Dumper->new([\%h, \@a])->Dumpxs;
Edge cases#
First argument is not a blessed hashref:
Dumpxswill callnew()internally using the remaining arguments, soData::Dumper::Dumpxs('Data::Dumper', \@vals, \@names)works.maxrecurseexceeded: croaks with"Recursion limit of N exceeded"after finishing the current top-level value so partial output is not lost from the caller’s perspective.maxdepthexceeded: prints[...]or{...}at the cut-off point rather than croaking.CODEreferences: emitted assub { "DUMMY" }unlessDeparse(1)is set.Regexp references: emitted as
qr/.../.Sortkeysas a coderef: accepted but currently treated as a truthy value, i.e. alphabetical sort. The coderef is not invoked.
Differences from upstream#
Sortkeysas a coderef: upstream calls the coderef per hash to obtain a custom key order; pperl treats any truthySortkeysas alphabetical. Scripts relying on coderef ordering need to pre-sort their data or callDumpwithUseperl(1).Deparse(1)emitssub { "DUMMY" }instead of the deparsed source. Scripts that depend on round-tripping closures needUseperl(1).Purity(1)is accepted and stored but does not yet emit the post-dump fixup statements upstream produces for self-referential structures; the backref notation works but arbitrary in-place assignments to reach an already-seen inner value are not emitted.
See also#
Dump— the pure-Perl fallback; identical output, slower.Dumper— the procedural one-liner wrappingnew(...)->Dumpxs.Seen— pre-seed the seen-table to name pre-existing references.Reset— clear the seen-table between unrelatedDumpxscalls.