home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


8.39 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:

  • Simple procedural interface:

    print Dumper($foo, $bar);

  • Extended usage with names:

    print Data::Dumper->Dump([$foo, $bar], [qw(foo *ary)]);

  • Object-oriented interface:

    $d = Data::Dumper->new([$foo, $bar], [qw(foo *ary)]);
       ...
    print $d->Dump;

By default, references are identified as $VAR n , where n is a numeric suffix. References to substructures within $VAR n 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 builtin operator used to create objects. A function with the specified name should exist and should accept the same arguments as the builtin. Default is bless .

$Data::Dumper::Deepcopy
$obj->Deepcopy([ newval ])

If set to Boolean value, enables deep copies of structures; cross-referencing is then only done 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 that is 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. $VAR n 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 are to 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:


Previous: 8.38 Cwd Perl in a Nutshell Next: 8.40 DB_File
8.38 Cwd Book Index 8.40 DB_File

Library Navigation Links

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