The Perl Data::Dumper Module
The
Data::Dumper
module takes a list of Perl variables
and writes their values out in the form of Perl
code, which will recreate the original values, no matter
how complex, when executed.
This module allows you to dump the state of a Perl program in a
readable form quickly and easily. It also allows you to restore the
program state by simply executing the dumped code using
eval()
or do()
.
The easiest way to describe what happens is to show you a quick example:
#!/usr/bin/perl -w # # ch02/marshal/datadumpertest: Creates some Perl variables and dumps them out. # Then, we reset the values of the variables and # eval the dumped ones ... use Data::Dumper; ### Customise Data::Dumper's output style ### Refer to Data::Dumper documentation for full details if ($ARGV[0] eq 'flat') { $Data::Dumper::Indent = 0; $Data::Dumper::Useqq = 1; } $Data::Dumper::Purity = 1; ### Create some Perl variables my $megalith = 'Stonehenge'; my $districts = [ 'Wiltshire', 'Orkney', 'Dorset' ]; ### Print them out print "Initial Values: \$megalith = " . $megalith . "\n" . " \$districts = [ ". join(", ", @$districts) . " ]\n\n"; ### Create a new Data::Dumper object from the database my $dumper = Data::Dumper->new( [ $megalith, $districts ], [ qw( megalith districts ) ] ); ### Dump the Perl values out into a variable my $dumpedValues = $dumper->Dump(); ### Show what Data::Dumper has made of the variables! print "Perl code produced by Data::Dumper:\n"; print $dumpedValues . "\n"; ### ...
Get Programming the Perl DBI now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.