Hello, i tried to store an array of Moose Object to YAML or JSON. The saving works well, but when i'm trying to restore my objects,i get an empty array of Objects who looks like this:
$VAR1 = bless({}, 'Note'); $VAR2 = bless({}, 'Note');
Here my code: Note.pm:
package Note; use strict; use warnings; use Moose; use MooseX::Storage; with Storage('format' => 'JSON', 'io' => 'File'); has 'message' =>(is=> 'rw', isa =>'Str'); 1;
testNote.pl:
use strict; use warnings; use utf8; use feature 'say'; use Note; use Storable; use MooseX::Storage; use Data::Dumper; use JSON; my @container=(); my $obj = Note->new; $obj->message("firstmessage"); say $obj->message; push(@container,$obj); my $obj2 = Note->new; $obj2->message("secondmessage"); push(@container,$obj2); my @output=(); for my $counter (0 .. $#container){ push(@output,$container[$counter]->pack()); } say "Output packed strings:" ; for my $counter(0 .. $#output){ say $output[$counter]; } store \@output, 'saveNotes'; my @Notes=(); my @fin=@{retrieve('saveNotes') }; say "After reading file:"; @Arr=(); for my $counter (0 .. $#fin){ push(@Arr,Note->unpack($fin[$counter])); } say Dumper(@Arr);
I think, that the problem is in this line:
for my $counter (0 .. $#fin){ push(@Arr,Note->unpack($fin[$counter])); }
Hope someone could help :)

In reply to Storing an array of Moose objects by demonking

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.