http://www.perlmonks.org?node_id=476819


in reply to Re: Can I serialize an object and have it remember it's type?
in thread Can I serialize an object and have it remember it's type?

Evaling didn't work in this case, but thanks for your comment. I didn't realize that was how serialization was supposed to work, but now this all makes more sense. I'm going to try serializing and evaling with Storable now.
**********
Update, yep, this works.
use strict; use warnings; use Storable qw(store retrieve freeze thaw dclone); use LWP::UserAgent; use Test::More qw(no_plan); my $user_agent = LWP::UserAgent->new; isa_ok($user_agent, 'LWP::UserAgent'); store($user_agent, 'myuseragent') or die "Can't store useragent!\n"; my $user_agent2 = retrieve('myuseragent'); isa_ok($user_agent2, 'LWP::UserAgent');

Replies are listed 'Best First'.
Re^3: Can I serialize an object and have it remember it's type?
by Tanktalus (Canon) on Jul 21, 2005 at 14:14 UTC

    Be forewarned - Storable does not provide forward/backward compatability. That is, if you upgrade your perl, there is a significant chance that all your stored objects will be unretrievable. If you switch platforms, it's almost guaranteed (use nstore - that may help).

    At least, that was my experience with Storable, oh so many years ago. I'd be happy to hear that has changed.

      We use storable extensively on many different systems with very little trouble.

      Version problems are potentially still there, but only if a newer version stores a data type that an older version cannot handle. (it now will try to handle the data from the newer storable instead of just croaking)

      I second your comment about nstore though. I'd highly recommend using nstore by default. Having to convert a couple million stored objects because you add a 64bit machine to your farm of 32bit machines sucks.

Re^3: Can I serialize an object and have it remember it's type?
by tlm (Prior) on Jul 21, 2005 at 13:39 UTC

    Evaling didn't work in this case...

    How exactly did it fail? Does the error go away if you precede the eval'ing with my $VAR1;?

    I'm going to try serializing and evaling with Storable now.

    With Storable explicit eval'ing is not required; just use its store and retrieve functions.

    Update: Gah! I entirely missed that in the serialization string the blessing had the wrong class. As tphyahoo kindly points out, no amount of eval-ing will fix this.

    the lowliest monk

      Thanks again tlm. As you can see above, I got the thing working with storable. As to the failure with DBM::Deep, I don't think evaling will help here because, if you look at the Dumper output, it's clear that this is getting blessed into a DBM::Deep object, and LWP::UserAgent is never mentioned.