@node Data/Dumper, Data/Flow, DCE/rgybase, Module List
@unnumbered stringified perl data structures, suitable for both printing and eval


@unnumberedsec NAME

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

@unnumberedsec SYNOPSIS

@example
use Data::Dumper;
@end example

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

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

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

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

@unnumberedsec DESCRIPTION

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

The return value can be evaled 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 @code{$VARn}, and other duplicate
references to substructures within @code{$VARn} will be appropriately labeled
using arrow notation.

The default output of self-referential structures can be evaled, but the
nested references to @code{$VARn} will be undefined, since a recursive structure
cannot be constructed using one Perl statement.  You can set
$Data::Dumper::Purity 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.

Several styles of output are possible, all controlled by setting
$Data::Dumper::Indent or using the corresponding method name.  Style 0
spews output without any newlines, indentation, or spaces between list
items.  It is the most compact format possible that can still be called
valid perl.  Style 1 outputs a readable form with newlines but no fancy
indentation (each level in the structure is simply indented by a fixed
amount of whitespace).  Style 2 (the default) outputs a very readable form
which takes into account the length of hash keys (so the hash value lines
up).  Style 3 is like style 2, but also annotates the elements of arrays
with their index (but the comment is on its own line, so array output
consumes twice the number of lines).

@unnumberedsubsec Methods

@table @asis
@item @emph{PACKAGE}->new(@emph{ARRAYREF [}, @emph{ARRAYREF]})
Returns a newly created 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
@code{$} 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.

The prefix specified by $Data::Dumper::Varname will be used with a
numeric suffix if the name for a value is undefined.

@item $@emph{OBJ}->Dump  @emph{or}  @emph{PACKAGE}->Dump(@emph{ARRAYREF [}, @emph{ARRAYREF]})
Returns the stringified form of the values stored in the object (preserving
the order in which they were supplied to new), subject to the
configuration options below.

The second form, for convenience, simply calls the new method on its
arguments before dumping the object immediately.

@item $@emph{OBJ}->Dumpxs  @emph{or}  @emph{PACKAGE}->Dumpxs(@emph{ARRAYREF [}, @emph{ARRAYREF]})
This method is available if you were able to compile and install the XSUB
extension to @code{Data::Dumper}. It is exactly identical to the @code{Dump} method 
above, only about 4 to 5 times faster, since it is written entirely in C.

@item $@emph{OBJ}->Seen(@emph{[HASHREF]})
Queries or adds to the internal table of already encountered references.
You must use Reset to explicitly clear the table if needed.  Such
references are not dumped; instead, their names are inserted wherever they
are to be dumped 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.

@item $@emph{OBJ}->Values(@emph{[ARRAYREF]})
Queries or replaces the internal array of values that will be dumped.

@item $@emph{OBJ}->Names(@emph{[ARRAYREF]})
Queries or replaces the internal array of user supplied names for the values
that will be dumped.

@item $@emph{OBJ}->Reset
Clears the internal table of "seen" references.

@end table
@unnumberedsubsec Functions

@table @asis
@item Dumper(LIST)
Returns the stringified form of the values in the list, subject to the
configuration options below.  The values will be named @code{$VARn} in the
output, where n is a numeric suffix.

@item DumperX(LIST)
Identical to the Dumper function above, but this calls the XSUB 
implementation, and is therefore about 3 to 4 times faster.  Only available
if you were able to compile and install the XSUB extensions in 
@code{Data::Dumper}.

@end table
@unnumberedsubsec Configuration Variables/Methods

Several configuration variables can be used to control the kind of output
generated when using the procedural interface.  These variables are usually
localized 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.

@table @asis
@item $Data::Dumper::Indent  @emph{or}  $@emph{OBJ}->Indent(@emph{[NEWVAL]})
Controls the style of indentation.  It can be set to 0, 1, 2 or 3.  2 is the
default.

@item $Data::Dumper::Purity  @emph{or}  $@emph{OBJ}->Purity(@emph{[NEWVAL]})
Controls the degree to which the output can be evaled 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.

@item $Data::Dumper::Pad  @emph{or}  $@emph{OBJ}->Pad(@emph{[NEWVAL]})
Specifies the string that will be prefixed to every line of the output.
Empty string by default.

@item $Data::Dumper::Varname  @emph{or}  $@emph{OBJ}->Varname(@emph{[NEWVAL]})
Contains the prefix to use for tagging variable names in the output. The
default is "VAR".

@end table
@unnumberedsubsec Exports

@table @asis
@item Dumper
@end table
@unnumberedsec EXAMPLE

@example
use Data::Dumper;
@end example

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

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

@example
package main;
$foo = Foo->new;
$fuz = Fuz->new;
$boo = [ 1, [], "abcd", \*foo,
         @{1 => @'a@', 023 => @'b@', 0x45 => @'c@'@}, 
         \\"p\q\@'r", $foo, $fuz];
$bar = eval(Dumper($boo));
print($@@) if $@@;
print Dumper($boo), Dumper($bar);  # pretty print (no array indices)

$Data::Dumper::Indent = 0;         # turn off all pretty print
print Dumper($boo), "\n";
@end example

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

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

@example
# recursive structure
@@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)]);
@end example

@example
$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
@end example

@example
$d = Data::Dumper->new([$a,$b], [qw(a b)]);     # go OO
$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 $d->Dump;
@end example

@unnumberedsec BUGS

Due to limitations of Perl subroutine call semantics, you cannot pass an
array or hash.  Prepend it with a @code{\} 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.

Dumper cheats with CODE references.  If a code reference is encountered in
the structure being processed, an anonymous subroutine returning the perl
string-interpolated representation of the original CODE reference will be
inserted in its place, and a warning will be printed if @code{Purity} is
set.  You can eval the result, but bear in mind that the anonymous sub
that gets created is a dummy placeholder. Someday, perl will have a switch
to cache-on-demand the string representation of a compiled piece of code, I
hope.

SCALAR objects have the wierdest looking bless workaround.

@unnumberedsec AUTHOR

Gurusamy Sarathy        gsar@@umich.edu

Copyright (c) 1995 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.

@unnumberedsec VERSION

Version 2.02beta    13 April 1996

@unnumberedsec SEE ALSO

perl(1)

