Putting Complex Data into Flat Files

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[11] 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, Data::Dumper and Storable, and 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.

[11] The Perl Module List can be found at http://www.perl.com/CPAN/.

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.