10.3 Serializing Data

10.3  Serializing Data

      We discuss in Section 10.1, in detail, how data that can be 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. We need to perform serialization in such cases. Serialization 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 deserialization facilities.           These include FreezeThaw.pm, Storable.pm, Data::Dumper.pm, Data::Denter.pm,
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 although several additional options are available. In this section, we discuss only the Data::Serializer.pm module, and only its default behavior. Additionally, we do not discuss how objects and classes can be deserialized. References to codes can also cause complications. Interested readers are referred to Perl documentation for more details.