In our discussions of so-called “flat files” we’ve so far been storing, retrieving, and manipulating only that most basic of datatypes: the humble string. What can you do if you want to store more complex data, such as lists, hashes, or deeply nested data structures using references?
The answer is to convert whatever it is you want to store into a string. Technically that’s known as marshalling or serializing the data. The Perl Module List has a section that lists several Perl modules that implement data marshalling.
We’re going to take a look at two of the most popular modules,
see how we can use them to put some fizz into our flat files. These
techniques are also applicable to storing complex Perl data
structures in relational databases using the DBI, so pay attention.
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
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 ...