Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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

by tlm (Prior)
on Jul 21, 2005 at 13:25 UTC ( [id://476811]=note: print w/replies, xml ) Need Help??


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

I'm not familiar with DBM::Deep, but don't you need to eval the serialization (string) to recover the desired object?

FWIW, I serialize objects with Storable all the time, so it's definitely possible.

the lowliest monk

  • Comment on Re: Can I serialize an object and have it remember it's type?

Replies are listed 'Best First'.
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
    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');

      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.

      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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-04-18 07:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found