Data Serialization in Perl

Before I dig more into details let’s try to understand some basic facts about Serialization :

What exactly Serialization is?

Serialization is the process of converting a data structure or object into a format that can be stored (for example, in a file or memory buffer, or transmitted across a network connection link) and “resurrected” later in the same or another computer environment. When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object.

Serialization in Perl:

In Perl, the data which is represented as key-value pairs can be stored in a file. This gives persistence to such data so that the data can be retrieved and manipulated by many independent, but possibly related, programs. However, if the structure of the data is more complex,  associative arrays or hashes do not provide adequate representation. For example, if the data item is an anonymous array of arrays, or a hash of hashes, or an object belonging to a certain class, it is not enough to be able to tie a file to a hash in order to be able to save the data to a file. So, we need to perform serialization in such cases which involves converting the contents of any complex data structure into a string following a certain well-designed algorithm.

The string produced by the serialization algorithm can then be deserialized later in the program when and if necessary. The serialized data structure can be sent over socket connections or used in remote procedure calls as long as the receiving end deserializes it using an appropriate algorithm. The serialized string can be stored to a text file, or a DBM file if it can be somehow associated with a key, or can be stored in a database. The only requirement that before using again, the string should be deserialized. There are several Perl modules that provide for serialization and de-serialization facilities. These include FreezeThaw.pm, Storable.pm, Data::Dumper.pm, Data::Denter.pm XML::DumperJSON::XS
etc. A module called Data::Serializer.pm provides a common interface to some of the serialization modules. The default module used by Data::Serializer.pm is Data::Dumper.pm.

In my next post I will try to show some examples.

Author: Pradeep Pant

Software Engineer

4 thoughts on “Data Serialization in Perl”

  1. The module Benchmark::Serialize tries to collect all serialization modules and enables you to benchmark the time ans space usage all the modules with the data structure you provides

    It would be nice to have tests benchmarking the functionality of the different formats. But I havn’t succeeded in implementing this in a way that I really like.

    ObDisclosure: I am the maintainer of Benchmark::Serialize

Leave a Reply

Your email address will not be published. Required fields are marked *