Book HomeJava and XSLTSearch this book

8.50. Data::Dumper

Converts Perl data structures into strings that can be printed or used with eval to reconstruct the original structures. Takes a list of scalars or reference variables and writes out their contents in Perl syntax. Several interfaces to Data::Dumper are provided:

By default, references are identified as $VARn, in which n is a numeric suffix. References to substructures within $VARn are labeled using arrow notation. In the extended usage form, references can be given user-specified names. See the Data::Dumper manpage for examples of the module's use.

Several configuration variables can be used to control the output generated using the procedural interface. These variables control the default state of the object created by the new method. Each variable has a corresponding method that can be used later to query or modify the object. In the following list, each variable is followed by the corresponding method:

$Data::Dumper::Bless
$obj->Bless([newval])
Can be set to a string that specifies an alternative to the bless built-in operator used to create objects. A function with the specified name should exist and should accept the same arguments as the built-in. Default is bless.

$Data::Dumper::Deepcopy
$obj->Deepcopy([newval])
If set to a Boolean value, enables deep copies of structures; cross-referencing is then done only when absolutely essential. Default is 0.

$Data::Dumper::Indent
$obj->Indent([newval])
Controls the style of indentation for the output. Can be set to:

0
No newlines, indentation, or spaces between list items

1
Newlines, but each level in the structure is indented a fixed amount

2
Default. Takes into account the length of hash keys so the hash values line up.

3
Like 2, but also annotates array elements with their index, with each annotation on its own line

$Data::Dumper::Freezer
$obj->Freezer([newval])
If set to a method name, Data::Dumper invokes that method via the object before attempting to turn it into a string. Set to an empty string to disable. Default is empty string.

$Data::Dumper::Pad
$obj->Pad([newval])
Specifies the string prefixed to every output line. Default is an empty string.

$Data::Dumper::Purity
$obj->Purity([newval])
Controls degree to which eval can recreate the original reference structures. Setting the value to 1 outputs additional Perl statements to correctly recreate nested references. Default is 0.

$Data::Dumper::Quotekeys
$obj->Quotekeys([newval])
If set to a Boolean value, controls whether hash keys are quoted. If false, avoids quoting hash keys that look like a simple string. Default is 1, which always quotes hash keys.

$Data::Dumper::Terse
$obj->Terse([newval])
When set, Data::Dumper outputs single, non-self-referential values as atoms or terms rather than as statements. $VARn names are avoided as much as possible. Such output may not be parsable by eval.

$Data::Dumper::Toaster
$obj->Toaster([newval])
If set to a method name, Data::Dumper issues a method call for any objects that will be dumped using the syntax bless(data, class)->method( ). Set to an empty string to disable. Default is empty string.

$Data::Dumper::Useqq
$obj->Useqq([newval])
When set, enables the use of double quotes to represent string values. In addition, \n, \t, and \r are used to represent non-space whitespace; "unsafe" characters are backslashed; and unprintable characters are output as quoted octal integers. Default is 0. Currently, the Dumpxs method does not honor this flag.

$Data::Dumper::Varnam
$obj->Varname([newval])
Specifies the prefix to use for tagging variable names in the output. Default is VAR.

The following methods and functions are provided.

new

$obj = Data::Dumper->new(arrayref[, arrayref])

Constructor. Creates a new 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 don't need a leading $ 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.

Dump

$obj->Dump
Data::Dumper->Dump(arrayref[, arrayref])

Returns stringified form of values stored in the object with their order preserved, subject to the configuration options. In array context, returns a list of strings corresponding to the supplied values.

Dumper

Dumper (list)

Function that returns the stringified form of the values in the list, subject to the configuration options. The values are named $VARn in the output. Returns a list of strings in array context.

DumperX

DumperX (list)

Identical to Dumper, except that it calls the xsub implementation. Available only if the xsub extension to Data::Dumper is installed.

Dumpxs

$obj->Dumpxs
Data::Dumper->Dumpxs(arrayref[, arrayref])

Identical to Dump, but written in C and therefore much faster. Available only if the xsub extension to Data::Dumper is installed.

Reset

$obj->Reset

Clears the internal table of "seen" references, returning the object itself.

Seen

$obj->Seen([hashref])

Queries or adds to the internal table of references that have been encountered. The references aren't dumped, but their names are inserted when they are encountered subsequently. With no argument, returns the "seen" list of name => value pairs, in array context. Otherwise, returns the object itself.

Values

$obj->Values([arrayref])

Queries or replaces the internal array of values to be dumped. With no arguments, returns the names. Otherwise, returns the object itself.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.