views:

64

answers:

2

What is the best module or approach to serialize data into a database?

Currently I am looking into Storable functions freeze and thaw, example:

use Storable qw(freeze thaw);
use strict;

my %array_test = ('Year Average' => 0.1, 'Color Average' => 0.8, 'Humans' => 0, 'Units' => 1);
my $serialized_data = freeze(\%array_test);
my %deserialized_data = %{ thaw($serialized_data) };

What I would like to know:

  1. Are there any native command in Perl to serialize and deserialize data?
  2. Is Storable a good approach to serialize and deserialize into a database or is there better approch / modules around?
  3. Should I do anything else with the serialization before storing it, like encoding it?
+2  A: 

The answers depend on the type of data you need to serialize.

  1. Native: there's pack/unpack, for plain numeric arrays you can join with commas, etc... Any of the simple native methods are very domain specific and don't apply to generic data.

  2. Storable is a good, standard approach. There are others (I heard of FreeseThaw but never used). You can also do YAML or JSON formatting - for comparisons, please see a recent StackOverflow question on sending an array of data over IO::Socket::INET (which also involves serialization).

  3. Further encoding need depends on serialization you do, and what you do with the data.

    E.g. if serialization preserves plain-text strings, and those strings can contain quotes, and you use the serialized data in the middle of an SQL statement (instead of binding the variables), you need to encode the quotes.

DVK
Thanks that helps a lot... Is there a need to sanitize what i serialize ? i mean, is it easy to break the serialization some how ?
Prix
@Prix - depends on the serialization and what you want to sanitize and how. Most normal encoding schemas are 100% lossless, so the result of unseialize(decode(encode(serialize(D)))) will be the original data D (as an example, look at URI encoding, or escaping quotes in a CSV data).
DVK
About the topic you mention can you confirm that the link is this one http://stackoverflow.com/questions/2824363/how-can-i-share-perl-data-structures-through-a-socket and if so update it in your reply for further reference to others that may come across it ? thanks
Prix
@Prix - that's the one. Updated
DVK
Another option is to use the serialization portions of KiokuDB (you don't have to actually use KiokuDB for the storage if you don't want to). This is nice as you have one interface to various different serialization formats, and issues such as cyclic references will be managed for you. It is, however, fairly heavy weight.
aCiD2
+1  A: 

What are you trying to serialize? There are many Perl modules that can handle this sort of thing, but each of them has shortcomings. I have a chapter in Mastering Perl about this, but there's also the comparison of serializers from the Indonesian Perl mongers.

If it's really just strings, like you show, then most things can handle your data just fine. If you want to store things such as code references or Perl objects, you have a tougher time.

One thing to consider, however, is that you don't necessarily want to limit your data to Perl. If a program in another language wants to get your data, something like YAML or JSON is much more friendly. I hate to do things that lock me into future decisions, so I don't prefer Perl-only solutions unless I really need them.

Watch out for people who answer before they know what you want to store. :)

brian d foy
+1 nice link and info thanks.
Prix