开发者

How can I serialize and deserialize Perl data to/from database?

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($seri开发者_如何学编程alized_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?


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.


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. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜