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

三世轮回 提交于 2019-12-24 10:58:08

问题


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?

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/3902812/how-can-i-serialize-and-deserialize-perl-data-to-from-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!