![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
eval
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::Dump::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); $d->Terse(1); $d->Deepcopy(1); eval $d->Dump;
The return value can be eval
ed to get back the original reference structure. Bear in mind that a
reference so created will not preserve pointer equalities with the original
reference.
Handles self-referential structures correctly. Any references that are the
same as one of those passed in will be marked $VAR
n (where n is a numeric suffix), and other duplicate references to substructures
within
$VAR
n 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 and $Data below.
The default output of self-referential structures can be eval
ed, but the nested references to $VAR
n will be undefined, since a recursive structure cannot be constructed using
one Perl statement. You can set the
Purity
flag to 1 to get additional statements that will correctly fill in these
references.
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. Output of names will be avoided where possible if
the Terse
flag is set.
Several styles of output are possible, all controlled by setting the Indent
flag. See Configuration Variables or Methods below for details.
Data::Dumper
object. The first argument is an anonymous array of values to be dumped.
The optional second argument is an anonymous array of names for the values.
The names need not have a leading
$
sign, and must be comprised of alphanumeric characters. You can begin a
name with a *
to specify that the dereferenced type must be dumped instead of the
reference itself, for ARRAY and HASH references.
The prefix specified by $Data::Dumper::Varname
will be used with a numeric suffix if the name for a value is undefined.
Data::Dumper will catalog all references encountered while dumping the values. Cross-references (in the form of names of substructures in perl syntax) will be inserted at all possible points, preserving any structural interdependencies in the original set of values. Structure traversal is depth-first, and proceeds in order from the first supplied value to the last.
new
), subject to the configuration options below. In an array context, it
returns a list of strings corresponding to the supplied values.
The second form, for convenience, simply calls the new
method on its arguments before dumping the object immediately.
Data::Dumper
. It is exactly identical to the Dump
method above, only about 4 to 5 times faster, since it is written entirely
in C.
Reset
to explicitly clear the table if needed. Such references are not dumped;
instead, their names are inserted wherever they are encountered
subsequently.
Expects a anonymous hash of name => value pairs. Same rules apply for
names as in new
. If no argument is supplied, will return the ``seen'' list of name =>
value pairs, in an array context.
$VAR
n in the output, where n is a numeric suffix. Will return a list of strings in an array context.
Data::Dumper
.
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.
eval
ed to recreate the supplied reference structures. Setting it to 1 will
output additional perl statements that will correctly recreate nested
references. The default is 0.
[\n\t\r]
, ``unsafe'' characters will be backslashed, and unprintable characters
will be output as quoted octal integers. Since setting this variable
imposes a performance penalty, the default is 0. The Dumpxs()
method does not honor this flag yet.
$VAR
n names will be avoided where possible, but be advised that such output may
not always be parseable by eval
.
bless(DATA, CLASS)-
METHOD()>.
Note that this means that the method specified will
have to perform any modifications required on the object (like creating new
state within it, and/or reblessing it in a different package) and then
return it. The client is responsible for making sure the method can be
called via the object, and that it returns a valid object. Defaults to an
empty string.
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); ######## # 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)]); ######## # 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; # empty the seen cache $d->Purity(0); 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 Foo; use Data::Dumper; my $a = Foo->new; my $b = Data::Dumper->new([$a], ['c']); $b->Freezer('Freeze'); $b->Toaster('Thaw'); my $c = $b->Dump; print $c; my $d = eval $c; print Data::Dumper->Dump([$d], ['d']);
\
to pass its reference instead. This will be remedied in time, with the
arrival of prototypes in later versions of Perl. 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, 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. Someday, perl will have a switch to cache-on-demand the
string representation of a compiled piece of code, I hope.
The Useqq
flag is not honored by Dumpxs()
(it always outputs strings in single quotes).
SCALAR objects have the weirdest looking bless
workaround.
Copyright (c) 1996 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.
perl(1)