Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Using Storable effectively

by Hrunting (Pilgrim)
on Apr 18, 2001 at 20:34 UTC ( [id://73561]=perlquestion: print w/replies, xml ) Need Help??

Hrunting has asked for the wisdom of the Perl Monks concerning the following question:

So, I want to use the Storable module to make my objects persistent, but I can't seem to use what Storable says are "inheritable" features. For instance, so I have some code like this:

package MyModule; sub STORABLE_freeze { my $this = shift; my $value = $this->{'key'}; delete $this->{'key'}; my $serialized = $this->SUPER::STORABLE_freeze( @_ ); # or $this->SUPER::freeze( @_ ) or etc. etc. $this->{'key'} = $key; return $serialized; } sub STORABLE_thaw { my $this = shift; $this->SUPER::STORABLE_thaw( @_ ); # or $this->SUPER::thaw( @_ ) or any other arrangment $this->post_loaded_function(); } 1;
The problem is, that once I'm in STORABLE_freeze/thaw, I can't access the functions of my parent without either a) generating an error or b) throwing myself into an endless loop where I'm repeatedly calling myself. How in the world are you supposed to use Storable's routines from when you're being called as the Storable freeze/thaw function?

Replies are listed 'Best First'.
Re: Using Storable effectively
by arturo (Vicar) on Apr 18, 2001 at 20:40 UTC

    Inheritance in Perl is handled via the @ISA array. You should be able to do something like this:

    package MyModule; use Storable; use vars qw(@ISA); @ISA=qw(Storable); #oops! original had quotes in it. # other methods # overrides Storable's thaw sub thaw { my $this = shift; $this->SUPER::thaw( @_ ); $this->post_loaded_function(); } 1;

    HTH

      Right, I'm sorry, I left out the @ISA portion of the code. Yes, the Storable module is being inherited. Actually, the problem now that I use your code is that I was inheriting STORABLE_freeze/thaw rather than just inheriting freeze/thaw. The documentation makes it sound like that's what you need to do if you want to override those abilities. Misread that, I guess.

      Thanks for the help.

      Actually, I lied, that's not working. I'm freezing and thawing this stuff separately (ie, not OO). Therefore, freeze() or thaw() never gets called in an OO fashion, and inheritance doesn't apply. STORABLE_freeze() and STORABLE_thaw() methods are supposed to allow you to "hook" into the Storable processing, but once in, you can't back out and use Storable's default (C-based) serialization routines.

      It appears

Re: Using Storable effectively
by repson (Chaplain) on Apr 19, 2001 at 12:58 UTC
    From Storable:
    > Returned value: A LIST "($serialized, $ref1, $ref2,
    > ...)" where $serialized is the serialized form to be
    > used, and the optional $ref1, $ref2, etc... are extra
    > references that you wish to let the Storable engine
    > serialize.
    
    > At deserialization time, you will be given back the
    > same LIST, but all the extra references will be
    > pointing into the deserialized structure.
    So basically you need to return values to be serialized furthur. I'm not sure how exactly what they want, but then I'm not the one doing what your doing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://73561]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-20 03:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found