develooper Front page | perl.perl5.porters | Postings from November 2012

Question about STORABLE_freeze

Thread Next
From:
Florent Angly
Date:
November 28, 2012 10:01
Subject:
Question about STORABLE_freeze
Message ID:
50B5E0DF.8070509@gmail.com
Hi all,

I am trying to serialize Bio::DB::Fasta Bioperl objects using Storable 
freeze() and thaw(). Bio::DB::Fasta uses Any_DBM to keep a large hash 
tied to a file. The problem is that the thawing operation triggers a 
segmentation fault, which I could trace back to this tied hash. It looks 
like I should simply untie the tied hash before serializing.

Storable has these nice hooks, STORABLE_freeze and STORABLE_thaw, that 
are perfect for customizing how freeze() and thaw() operate. So, my idea 
was to implement these hooks in Bio::DB::Fasta in order to automatically 
perform a few operations (e.g. untieing the hash) before serialization 
with freeze() or after deserialization thaw(). Note that I do not want 
to roll my own serialization, simply do a few operations before or after 
Storable's serialization routines are called.

That's the code I added at the moment:

     sub STORABLE_freeze {
         my ($self, $cloning) = @_;
         return if $cloning; # Regular default serialization
         # Do some operations before serialization
         $self->_pre_freeze;
         # Serialize object using Storable
         my $serialized = Storable::freeze($self);
         return $serialized;
     }

     sub _pre_freeze {
         my ($self) = @_;
         # Close tied hash, etc...
         return $self;
     }

The problem in this approach is the call to Storable::freeze(), which 
calls STORABLE_freeze(), leading to an infinite recursion. The question 
is then: how can I serialize an object from within STORABLE_freeze?

Storable's documentation mentions that it's possible to return an empty 
list from STORABLE to further discard the hook and let Storable finish 
the job. While this approach seemed to freeze my objects as intended, I 
noted that it ignored my STORABLE_thaw hook.

Of course, I could use the internals of Storable directly, e.g. the 
_freeze() function, but that would be very hackish. I feel like I am 
missing something obvious. Any idea?

Thanks,

Florent


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About