如何在数据库中对Perl数据进行序列化和反序列化? [英] How can I serialize and deserialize Perl data to/from database?

查看:78
本文介绍了如何在数据库中对Perl数据进行序列化和反序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将数据序列化到数据库中的最佳模块或方法是什么?

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

目前,我正在研究可存储函数freezethaw,例如:

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) };

我想知道的事情:

  1. Perl中是否有任何本机命令来序列化和反序列化数据?
  2. Storable是将序列化和反序列化成数据库的好方法,还是周围有更好的处理方法/模块?
  3. 在存储序列化之前,我应该对序列化做其他事情吗,例如对它进行编码?

推荐答案

答案取决于您需要序列化的数据类型.

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

  1. 本机:有打包/解包的功能,对于纯数字数组,您可以将其与逗号连接,等等.任何简单的本机方法都是特定于域的,不适用于常规数据.

  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.

Storable是一种很好的标准方法.还有其他(我听说过FreeseThaw但从未使用过).您也可以进行YAML或JSON格式化-为了进行比较,请参阅最新的

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

进一步的编码需求取决于您进行的序列化以及对数据的处理方式.

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

例如如果序列化保留纯文本字符串,并且这些字符串可以包含引号,并且您在SQL语句的中间使用序列化的数据(而不是绑定变量),则需要对引号进行编码.

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.

这篇关于如何在数据库中对Perl数据进行序列化和反序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆