Rate this page

Flattr this

Dump the content of an arbitrary Perl data structure

Tested on

Debian (Etch, Lenny, Squeeze)
Fedora (14)
Ubuntu (Hardy, Intrepid, Jaunty, Karmic, Lucid, Maverick, Natty, Precise, Trusty)


To dump the content of an arbitrary Perl data structure to STDERR.


Suppose that you are attempting to troubleshoot a Perl script and need to inspect the content of a data structure referenced by the variable $data. You know what the layout of this data structure should be, however there is a significant possibility that it has not been constructed as intended.


You can dump the content of a Perl data structure using the Data::Dump module. First the module must be installed. On Debian-based systems it is located in the package libdata-dump-perl:

apt-get install libdata-dump-perl

The next step is to load the module. The specific function that you need to import is called dump:

use Data::Dump qw(dump);

You should now be able to inspect any data structure by passing it to dump:

my $data = {username=>'user',password=>'xyzzy'};
dump $data;

If dump is evaluated in a void context then the output will be sent to STDERR. In this particular example it should look similar to:

{ password => "xyzzy", username => "user" }

This is an executable fragment of Perl code which, if evaluated using eval, would reconstruct a copy of the original data structure.


Dump to a location other than STDERR

When evaluated in a non-void context dump returns its output as a string instead of writing it to STDERR. You can then use it in whatever manner is appropriate, for by example sending it to syslog:

use Data::Dump qw(dump);
use Sys::Syslog;

my $data = {username=>'user',password=>'xyzzy'};


Tags: perl