Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The following appears to work correctly!

#!/usr/bin/env perl use Data::Dx; use Dios { accessors => 'unified' }; use Storable; use Test::Expr; #use strict; # made redundant by use 5.014_00 #use warnings; # made redundant by use 5.014_00 <== +======= INCORRECT see post below! use 5.014_00; class Nada { has Int $.number is rw; }; class Yada { has Int $.number is rw; has Nada @.nadas is rw; }; # class Yada: sub test_our_yada { my $self=shift; ok $self->isa('Yada'); ok $self->number() == 0; ok @{$self->nadas()} == 2; Dx $self->nadas(); ok $self->nadas()->[0]->number() == 1; ok $self->nadas()->[1]->number() == 2; } { # The "initial" Yada my $yada=Yada->new(number=>0,nadas=>[Nada->new(number=>1),Nada->ne +w(number=>2)]); $yada->main::test_our_yada(); # Serialize $yada and write it to 'Yada.dat' my $dump=$yada->dump(); # NB: Without an argument! store($dump,'Yada.dat'); } { # The restored from 'Yada.dat' Yada object my $dump=retrieve('Yada.dat'); my $yada=Object::InsideOut->pump($dump); $yada->main::test_our_yada(); } done_testing;

Which yields

ok 1 - $self->isa('Yada') ok 2 - $self->number() == 0 ok 3 - @{$self->nadas()} == 2 #line 25 test_03.t $self->nadas() = [ bless(do{\(my $o = 1)}, "Nada"), bless(do{\(my $o = 2)}, "Nada"), ] ok 4 - $self->nadas()->[0]->number() == 1 ok 5 - $self->nadas()->[1]->number() == 2 ok 6 - $self->isa('Yada') ok 7 - $self->number() == 0 ok 8 - @{$self->nadas()} == 2 #line 25 test_03.t $self->nadas() = [ bless(do{\(my $o = 1)}, "Nada"), bless(do{\(my $o = 2)}, "Nada"), ] ok 9 - $self->nadas()->[0]->number() == 1 ok 10 - $self->nadas()->[1]->number() == 2 1..10

Somewhat simpler is

#!/usr/bin/env perl use Data::Dx; use Dios { accessors => 'unified' }; use Storable; use Test::Expr; use 5.014_00; BEGIN { # Must go before the classes are declared! $Nada::storable=1; $Yada::storable=1; } class Nada { has Int $.number is rw; }; class Yada { has Int $.number is rw; has Nada @.nadas is rw; }; # class Yada: # Yeah, I'm lazy sub Yada::test_our_yada { my $self=shift; ok $self->isa('Yada'); ok $self->number() == 0; ok @{$self->nadas()} == 2; Dx $self->nadas(); ok $self->nadas()->[0]->number() == 1; ok $self->nadas()->[1]->number() == 2; } { # The "initial" Yada my $yada=Yada->new(number=>0,nadas=>[Nada->new(number=>1),Nada->ne +w(number=>2)]); $yada->test_our_yada(); # Serialize and write to 'Yada.dat' $yada->store('Yada.dat'); } { # Yada as restored from 'Yada.dat' my $yada=retrieve('Yada.dat'); $yada->test_our_yada(); } done_testing;

Yabba Dabba Doo!


In reply to Re: How to serialize and deserialize Dios/Object::InsideOut objects? by clueless newbie
in thread How to serialize and deserialize Dios/Object::InsideOut objects? by clueless newbie

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



  • 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.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-20 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found