Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Is there a way to serialize an object in such a way that when I retrieve it later it remembers what "type" it was blessed into?

I tried to do this with DBM::Deep, but as you see, no dice.

#test.pl use warnings; use strict; use Data::Dumper; use DBM::Deep; use LWP::UserAgent; use Test::More qw(no_plan); my $hash = DBM::Deep->new("foo.db"); my $user_agent = LWP::UserAgent->new; isa_ok($user_agent, 'LWP::UserAgent'); #passes $hash->{my_user_agent} = $user_agent; my $user_agent2 = $hash->{my_user_agent}; isa_ok($user_agent2, 'LWP::UserAgent');#fails print Dumper($user_agent2);
Output of test.pl 2>>test.pl and then test.pl >>test.pl
Failed test (read.pl at line 15) The object isn't a 'LWP::UserAgent' it's a 'DBM::Deep' Looks like you failed 1 tests of 2. ok 1 - The object isa LWP::UserAgent not ok 2 - The object isa LWP::UserAgent $VAR1 = bless( { 'requests_redirectable' => bless( [ 'GET', 'HEAD' ], 'DBM::Deep' ), 'max_redirect' => '7', 'proxy' => undef, 'parse_head' => '1', 'use_eval' => '1', 'timeout' => '180', 'protocols_allowed' => undef, 'agent' => 'libwww-perl/5.79', 'protocols_forbidden' => undef, 'no_proxy' => bless( [], 'DBM::Deep' ), 'from' => undef, 'max_size' => undef }, 'DBM::Deep' ); 1..2
This is sort of a followup to How's mldbm with concurrency?, where I was doing serialization with mldbm. I wound up switching to DBM::Deep on merlyn's advice, but that was before it occurred to me I might be checking for what kind of object I pulled out.

**********

UPDATE: I think chromatic's Object Serialization Basics is a good place to learn how to do this.


In reply to Can I serialize an object and have it remember it's type? by tphyahoo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2015-07-04 17:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls