sereal
,json::xs和
asn.1,avro,bert,bbor,cbor,jsync,messagepack, -protocolbuffers,thriftcolcocol.intift..
其他通常提到的选择是 -storable和data::Dumper(或类似)/print <FILE>
,但是我不能推荐它们,因为Storable的格式与Perl版本有关,并且不安全,因为它执行任意代码。截至2012年,解析反对党::undump还没有取得很远的进展。我也不建议使用XML,因为它不能很好地映射Perl数据类型,并且存在多个竞争/不兼容的模式如何在XML和DATA之间翻译。
代码示例(测试):
eval
下一步从这里升起
对象持续。 也请阅读:perl的serializers:何时使用what what
perlmonks对序列化有两个很好的讨论。
use JSON::XS qw(encode_json decode_json);
use File::Slurp qw(read_file write_file);
my %hash;
{
my $json = encode_json \%hash;
write_file('dump.json', { binmode => ':raw' }, $json);
}
{
my $json = read_file('dump.json', { binmode => ':raw' });
%hash = %{ decode_json $json };
}
use YAML::XS qw(Load Dump);
use File::Slurp qw(read_file write_file);
my %hash;
{
my $yaml = Dump \%hash;
write_file('dump.yml', { binmode => ':raw' }, $yaml);
}
{
my $yaml = read_file('dump.yml', { binmode => ':raw' });
%hash = %{ Load $yaml };
}
#Load a file into a hash.
#My Text file has the following format.
#field1=value1
#field2=value2
#<FILE1> is an opens a sample txt file in read-only mode.
my %hash;
while (<FILE1>)
{
chomp;
my ($key, $val) = split /=/;
$hash{$key} .= exists $hash{$key} ? ",$val" : $val;
}