Categories
Programming

Storing Perl structures…

I’ve been looking at alternatives for storing Perl structures within a database such as MySQL. Here are the main contenders:
Storable (freeze/thaw)
XML::Dumper
Data::Dumper


Storable is great – except that it produces binary data and as such it’s difficult to spot corruption and more importantly, difficult to fix corruption. The internal format is also not necessarily static and this makes the fact that it produces binary data even less useful.
Data::Dumper is quite basic in what it outputs, but it does represent the structure very nicely and in a way that could be loaded back into a structure through the use of eval. Data::Dumper output looks like this…
$VAR1 = {
‘data’ => ‘simon’,
‘nothing’ => undef,
‘array’ => [
‘blue’,
‘red’,
‘green’
],
‘nested’ => {
‘level2a’ => 1,
‘level2b’ => 1
}
};
XML::Dumper is very similar to Data::Dumper except that it has no way to represent an undefined (undef) field so you just end up with the same as if it was an empty string. It also throws up warnings about “Use of uninitialized value” which if it’s used from within a CGI script tends to fill up the error log quickly.
The output from XML::Dumper looks like this:
<perldata>
<hash>
<item key=”data”>simon</item>
<item key=”nothing”></item>
<item key=”array”>
<array>
<item key=”0″>blue</item>
<item key=”1″>red</item>
<item key=”2″>green</item>
</array>
</item>
<item key=”nested”>
<hash>
<item key=”level2a”>1</item>
<item key=”level2b”>1</item>
</hash>
</item>
</hash>
</perldata>
XML has it’s advantages such as being easy to read and the code to convert it back to a data structure isn’t difficult either. The one problem I’ve come across in my limited testing is that if you have binary data in the structure although you can convert it from a Perl structure to XML, you can’t seem to convert it back as it complains about invalid tokens. In case anyone else looks at the documentation for it and just cannot get the xml2pl function working, the docs are lacking some steps..
Assume that $xml is the XML structure above (including the <perldata></perldata> tags).
my $dump = new XML::Dumper;
my $parser = new XML::Parser(Style => “Tree”);
my $tree = $parser->parse($xml)
my $structure = $dump->xml2pl($tree);
The documentation misses out the middle two lines so you end up with a variable being passed to xml2pl that hasn’t been declared anywhere.
Still not decided which to opt for, but I suspect it’ll be Data::Dumper.