Perl序列化和反序列化哈希的散列

前端之家收集整理的这篇文章主要介绍了Perl序列化和反序列化哈希的散列前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图序列化散列哈希值,然后反序列化它以获取哈希的原始散列.问题是每当我反序列化它时..附加一个自动生成的$var1例如.

原始哈希

%hash=(flintstones => {
    husband   => "fred",pal       => "barney",},jetsons => {
    husband   => "george",wife      => "jane","his boy" => "elroy",);

出来了
     $VAR1 = {
          ‘simpsons’=> {
                          ‘kid’=> “巴特”,
                          ‘妻子’=> “玛吉”,
                          ‘丈夫’=> “本垒打”
                        },
          ‘flintstones’=> {
                             ‘丈夫’=> “弗雷德”,
                             ‘pal’=> “巴尼”
                           },
};

有没有什么办法可以得到没有$var1的哈希的原始哈希.. ??

解决方法

你已经证明Storable工作得非常好. $VAR1是Data :: Dumper序列化的一部分.

use Storable     qw( freeze thaw );
use Data::Dumper qw( Dumper );

my %hash1 = (
   flintstones => {
      husband  => "fred",pal      => "barney",jetsons => {
      husband  => "george",wife     => "jane",);

my %hash2 = %{thaw(freeze(\%hash1))};

print(Dumper(\%hash1));
print(Dumper(\%hash2));

如您所见,原始版本和副本都是相同的:

$VAR1 = {
          'jetsons' => {
                         'his boy' => 'elroy','wife' => 'jane','husband' => 'george'
                       },'flintstones' => {
                             'husband' => 'fred','pal' => 'barney'
                           }
        };
$VAR1 = {
          'jetsons' => {
                         'his boy' => 'elroy','pal' => 'barney'
                           }
        };

猜你在找的Perl相关文章