Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Can I serialize an object and have it remember it's type?

by tphyahoo (Vicar)
on Jul 21, 2005 at 13:34 UTC ( #476819=note: print w/ replies, xml ) Need Help??


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


Comment on Re^2: Can I serialize an object and have it remember it's type?
Download Code
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.
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://476819]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (11)
As of 2014-11-27 08:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (182 votes), past polls