Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Getting back $object from eval(Dumper($object))

by tphyahoo (Vicar)
on Jul 27, 2005 at 08:45 UTC ( [id://478513]=perlquestion: print w/replies, xml ) Need Help??

tphyahoo has asked for the wisdom of the Perl Monks concerning the following question:

This is a followup to Can I serialize an object and have it remember it's type?. Eventually this has applications for serialization, but for now I just want to be able to dump an object and get it back using eval, which I was told I should be able to do. This is my code that doesn't work.
use warnings; use strict; use Data::Dumper; use Test::More qw(no_plan); package Foo; sub new {bless {'a' => 1}, $_[0]}; package main; my $foo = Foo->new(); my $foo_dumped_evaled; $foo_dumped_evaled = eval(Dumper($foo)); isa_ok($foo_dumped_evaled,'Foo'); #fails $foo_dumped_evaled = eval{Dumper($foo)}; isa_ok($foo_dumped_evaled,'Foo'); #fails $foo_dumped_evaled = bless( { 'a' => 1 }, 'Foo' ); isa_ok($foo_dumped_evaled,'Foo'); #fails print Dumper($foo);
Output from perl dumperIntro.pl > output.txt 2>&1:
not ok 1 - The object isa Foo # Failed test (dumperIntro.pl at line 14) # The object isn't defined not ok 2 - The object isa Foo # Failed test (dumperIntro.pl at line 17) # The object isn't a reference ok 3 - The object isa Foo $VAR1 = bless( { 'a' => 1 }, 'Foo' ); 1..3 # Looks like you failed 2 tests of 3.
Any ideas? Thanks for your help!

Replies are listed 'Best First'.
Re: Getting back $object from eval(Dumper($object))
by broquaint (Abbot) on Jul 27, 2005 at 08:56 UTC
    Turn on $Data::Dumper::Terse and only use eval STRING as eval BLOCK is not what you want e.g
    use warnings; use strict; use Data::Dumper; use Test::More qw(no_plan); package Foo; sub new {bless {'a' => 1}, $_[0]}; package main; my $foo = Foo->new(); my $foo_dumped_evaled; $Data::Dumper::Terse = 1; $foo_dumped_evaled = eval(Dumper($foo)); isa_ok($foo_dumped_evaled,'Foo'); #fails $foo_dumped_evaled = eval{Dumper($foo)}; isa_ok($foo_dumped_evaled,'Foo'); #fails print Dumper($foo); __output__ ok 1 - The object isa Foo not ok 2 - The object isa Foo # Failed test (pmsopw_478513.pl at line 19) # The object isn't a reference bless( { 'a' => 1 }, 'Foo' ) 1..2 # Looks like you failed 1 test of 2.
    HTH

    _________
    broquaint

Re: Getting back $object from eval(Dumper($object))
by borisz (Canon) on Jul 27, 2005 at 09:02 UTC
    use warnings; use strict; use Data::Dumper; use Test::More qw(no_plan); package Foo; sub new {bless {'a' => 1}, $_[0]}; package main; my $foo = Foo->new(); my $foo_dumped_evaled; eval Data::Dumper->Dump([$foo], [ qw /foo_dumped_evaled/ ]) ; isa_ok($foo_dumped_evaled,'Foo'); #fails print Dumper($foo);
    Boris
Re: Getting back $object from eval(Dumper($object))
by revdiablo (Prior) on Jul 27, 2005 at 18:20 UTC

    You may already be aware of this, but I saw it mentioned neither in this thread nor the previous one you linked to. There is one major pitfall with using something like Data::Dumper or Storable for object serialization: it only grabs what's in the blessed data structure.

    It's perfectly possible, reasonable, and some even would say preferable, to store object data outside of the blessed reference. For example, lexical variables captured by closures within the object. This includes the very nice inside-out object technique.

    That means there's no fool-proof, general purpose way to serialize objects in Perl. Granted, most modules probably strictly adhere to their blessed reference, but it's certainly a caveat you should be aware of.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-19 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found