6

I am trying to serialize a hash of hashes and then deserializing it to get back the original hash of hashes ..the issue is whenever i deserialize it ..it appends an autogenerated $var1 eg.

original hash

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

comes out as $VAR1 = { 'simpsons' => { 'kid' => 'bart', 'wife' => 'marge', 'husband' => 'homer' }, 'flintstones' => { 'husband' => 'fred', 'pal' => 'barney' }, };

is there any way i can get the original hash of hashes without the $var1..??

user1547285
  • 63
  • 1
  • 3
  • `$VAR1` is not prepended. It's part of the serialisation. What makes you think that gives a different hash after you deserialise it? How do you deserialise it? – ikegami Jul 24 '12 at 01:57
  • 1
    Personally, I would serialise to JSON using JSON::XS. Data::Dumper is a debugging tool, not a good serialiser. Especially not with the default options. – ikegami Jul 24 '12 at 01:58
  • i have serialized/deserialized using Freeze/thaw...I need to get the original hash so that i can do some calculations on it ... – user1547285 Jul 24 '12 at 02:10
  • 1
    The output you provided is the serialisation created by Data::Dumper. `$VAR1` is part of Data::Dumper's serialisation. You've just showed that Storable worked correctly. – ikegami Jul 24 '12 at 02:12

2 Answers2

10

You've proved that Storable worked perfectly fine. The $VAR1 is part of Data::Dumper's serialisation.

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

As you can see, both the original and the copy are identical:

$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'
                           }
        };
ikegami
  • 367,544
  • 15
  • 269
  • 518
3

If you set $Data::Dumper::Terse to 1, then Data::Dumper will try to skip these variable names (but the result could sometimes be no longer parseable by eval).

use Data::Dumper;
$Data::Dumper::Terse = 1;
print Dumper \%hash;

becomes now:

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

Maybe something like JSON or YAML would be better for your purpose?

Sebastian Stumpf
  • 2,761
  • 1
  • 26
  • 34