10.3.1 Serializing and Deserializing using Data::Serializer.pm
10.3.1 Serializing and Deserializing using Data::Serializer.pm
The following program shows how the Data::Serializer.pm module is used. The
Data::Serializer.pm provides a consistent interface to the following modules: Storable.pm,
FreezeThaw.pm, Data::Denter.pm and Data::Dumper.pm. It uses Data::Dumper.pm as the default serialization and deserialization module. It is possible to specify that any one of the other modules be used for serialization and deserialization as well. The following program serializes or deserializes a scalar, a list or an array, a hash, and an array of arrays.
Program 10.8
#!/usr/bin/perl
#serialize.pl
use Data::Serializer;
use strict;
my $dumper = Data::Serializer -> new ();
#serialize a scalar; not of much use
my $friend = "Justin O'Malley";
my $serializedFriend = $dumper->serialize (\$friend);
my $friendAgain = $dumper -> deserialize ($serializedFriend);
print "friendAgain = $$friendAgain \n";
#serialize a list or array
my @friends = ("Justin O'Malley", "Christopher Paul", "Shane Jahnke",
"Seth Gross", "Seth Musselman", "Greg Eisenbeis");
my $serializedFriends = $dumper->serialize (\@friends);
#print "serializedFriends = $serializedFriends\n";
my $friendsAgain = $dumper->deserialize ($serializedFriends);
print "friendsAgain = ", join ("\n\t ", @$friendsAgain), "\n";
#serialize a hash
my %hobbies = ("Chris Paul" => "Skiing/Working out",
"Justin O'Malley" => "Singing/Acting", ,
"Shane Jahnke" => "Soccer",
"Seth Gross" => "Soccer/School",
"Seth Musselman" => "Sailing",
"Greg Eisenbeis" => "Girls");
my $serializedHobbies = $dumper->serialize(\%hobbies);
#print "serializedHobbies = $serializedHobbies\n";
my $hobbiesAgain = $dumper-> deserialize ($serializedHobbies);
my $person;
print "\nFriends and their hobbies:\n";
foreach $person (keys %$hobbiesAgain){
print $person, "\t\t", $$hobbiesAgain{$person}, "\n";
}
#serialize an array of arrays
my $friendsStats = [ ['Chris Paul', 'Colorado Springs', 23, 76, 180],
['Shane Jahnke', 'Colorado Springs', 19, 73, 150],
['Seth Gross', 'Colorado Springs', 20, 70, 160],
['Seth Musselman', 'Colorado Springs', 22, 74, 170],
['Greg Eisenbeis', 'Colorado Springs', 22, 76, 165]
];
my $serializedStats = $dumper->serialize ($friendsStats);
my $statsAgain = $dumper->deserialize ($serializedStats);
my $i;
print "\nFriends and their stats:\n";
for ($i=0; $i <= 4; $i++){
printf "%-15s %-15s %3d %3d %3d\n",
$statsAgain -> [$i] -> [0], $statsAgain -> [$i] -> [1],
$statsAgain -> [$i] -> [2], $statsAgain -> [$i] -> [3],
$statsAgain -> [$i] -> [4];
}
The program uses the Data::Serializer.pm module. An instance of a Data::Serializer object is needed for serialization and deserialization. The instance is called $dumper.
my $dumper = Data::Serializer -> new ();
For purposes of illustration, the program starts by serializing a scalar. The serialize method of the
Data::Serializer object takes a reference to a data structure, here that of a scalar, as argument.
my $serializedFriend = $dumper->serialize (\$friend);
This is an illustrative program, and hence, not particularly useful. The serialize method returns a reference to a string that results from the serialization algorithm. The program deserializes this string immediately and prints the output. deserialize takes a reference to a serialized string and produces a reference to the deserialized data structure.
my $friendAgain = $dumper -> deserialize ($serializedFriend);
We can clearly see, in the output that follows the program that deserialization obtains the original scalar value back.
Next, the program serializes an array @friends. Once again serialize takes a reference to the array. The serialize method produces a string that is deserialized again, immediately. Once again, this program is for illustration purposes only. By looking at the output that follows the program, we see that deserialization reproduces the original list.
The program follows by assigning values to a hash %hobbies that relates the name of a person and his hobbies. Following the assignment, the hash is serialized. The serialization process produces a string. This string is deserialized immediately. The foreach loop prints the key-value pairs in the deserialzed hash. Examination of the output that follows the program confirms that serialization and subsequent deserialization reproduces the original set of key-value pairs.
Finally, the program assigns value to an anonymous array of arrays that contains names of friends and some information about each friend. $friendStats stores a reference to the array of arrays. This reference is passed as an argument to serialize to obtain a string. The string is immediately deserialized.
my $serializedStats = $dumper->serialize ($friendsStats);
my $statsAgain = $dumper->deserialize ($serializedStats);
The rest of the program traverses the contents of the deserialized data structure and prints the information in a readable manner. The printing confirms that the array of arrays is reproduced without any glitch. The output of the program follows.
friendAgain = Justin O'Malley
friendsAgain = Justin O'Malley
Christopher Paul
Shane Jahnke
Seth Gross
Seth Musselman
Greg Eisenbeis
Friends and their hobbies:
Justin O'Malley Singing/Acting
Greg Eisenbeis Girls
Seth Gross Soccer/School
Seth Musselman Sailing
Chris Paul Skiing/Working out
Shane Jahnke Soccer
Friends and their stats:
Chris Paul Colorado Springs 23 76 180
Shane Jahnke Colorado Springs 19 73 150
Seth Gross Colorado Springs 20 70 160
Seth Musselman Colorado Springs 22 74 170
Greg Eisenbeis Colorado Springs 22 76 165
