Trouble while using thaw on the serialized data structure in perl

363 Views Asked by At

I am using perl DB_File module to persist the hash variable into a file.

My hash variable contains key as normal string and value as another hash variable.

I used Storable::freeze(\%value); to serialize the hash value.

But when I tried to retrieve the values, I got an error. For the first time when I run the retrieve code, it works. The next consecutive times, it fails.

I used method like this:

tie(%HASH, "DB_File", "dbfile", O_RDWR, 0444); 
foreach $key (%HASH)
{
    $hashRef = Storable::thaw($HASH{$key};  --> here it fails with the error 
}

Error message

Storable binary image v25.47 more recent than I am (v2.7) at ../../lib/Storable.pm (autosplit into ../../lib/auto/Storable/thaw.al) line 366, at retrieve.pl line 15 at ../../lib/Storable.pm (autosplit into ../../lib/auto/Storable/logcroak.al) line 74 Storable::logcroak('') called at ../../lib/Storable.pm (autosplit into ../../lib/auto/Storable/thaw.al) line 367 Storable::thaw('2/8') called at retrieve.pl line 15

2

There are 2 best solutions below

0
On

Have a look at the error:

.... Storable::thaw('2/8') called ....

The value that you are trying to thaw is the scalar result of a hash.

I assume that the $HASH{$key} in

$hashRef = Storable::thaw($HASH{$key});

contains a hash (may be of frozen objects).

Try to add

use Data::Dumper;
print 'content : '.Dumper $HASH{$key};

before you try to thaw the value, to see its content.

0
On
Storable::thaw($HASH{$key};

You forget to close ()

Storable::thaw($HASH{$key});

And are you shure you need thaw all keys? I think you have few fields and not all of them have to be Storable.