2

I tried to store an array of Moose Objects to YAML or JSON.

The saving works very well, but when I try to restore my Objects, they're empty:

$VAR1 = bless({}, 'Note');
$VAR2 = bless({}, 'Note');

Here is 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);

Hope someone could help :)

RobEarl
  • 7,862
  • 6
  • 35
  • 50
demonking
  • 2,584
  • 5
  • 23
  • 28
  • http://stackoverflow.com/questions/3999875/which-recommended-perl-modules-can-serialize-moose-objects may be of some help. – mpapec May 27 '13 at 06:42
  • 4
    Remove your constructor from `Note.pm` - Moose does that for you, and implementing your own could interfere with it. Likewise `$obj->{'message'}= "firstmessage";` should be `$obj->message( $firstmessage );` – Neil Slater May 27 '13 at 06:50
  • Neil Slate: thx, i will fix this now. mpaped: i don't have hashref or something like that ;) – demonking May 27 '13 at 07:03

0 Answers0