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

use*_*285 5 perl perl-data-structures

我试图序列化散列哈希值,然后反序列化它以获取哈希的原始散列.问题是每当我反序列化它时..附加一个自动生成的$ var1例如.

原始哈希

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

出现在$ VAR1 = {'simpsons'=> {'kid'=>'bart','wife'=>'marge','husband'=>'homer'},'flintstones'=> {'丈夫' >'fred','pal'=>'barney'},};

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

ike*_*ami 9

你已经证明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",
     "his boy" => "elroy",  
   },
);

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

print(Dumper(\%hash1));
print(Dumper(\%hash2));
Run Code Online (Sandbox Code Playgroud)

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

$VAR1 = {
          'jetsons' => {
                         'his boy' => 'elroy',
                         'wife' => 'jane',
                         'husband' => 'george'
                       },
          'flintstones' => {
                             'husband' => 'fred',
                             'pal' => 'barney'
                           }
        };
$VAR1 = {
          'jetsons' => {
                         'his boy' => 'elroy',
                         'wife' => 'jane',
                         'husband' => 'george'
                       },
          'flintstones' => {
                             'husband' => 'fred',
                             'pal' => 'barney'
                           }
        };
Run Code Online (Sandbox Code Playgroud)