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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.