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

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 :)

Replies are listed 'Best First'.
Re: Storing an array of Moose objects
by Anonymous Monk on May 29, 2013 at 07:15 UTC

    Me again, according to the docs, pack/unpack DO NOT write to files, or json, they make/break an unblessed perl hash

    I concocted the following based on the docs and http://search.cpan.org/dist/MooseX-Storage/MANIFEST

    It is freeze/thaw that does the JSON thing in your class (cause Storage format JSON)

    Example

    #!/usr/bin/perl -- use strict; use warnings; Main( @ARGV ); exit( 0 ); BEGIN { package Note; use strict; use warnings; use Moose; use MooseX::Storage; with Storage('format' => 'JSON', 'io' => 'File'); has 'message' =>(is=> 'rw', isa =>'Str'); no Moose; $INC{'Note.pm'} = __FILE__; # true } sub Main { use strict; use warnings; use Storable qw/ nfreeze thaw /; use Data::Dump qw/ dd /; use Note; my @notes = map { Note->new( message => $_.' '.localtime ) } qw/ R +o Sham Bo /; dd( \@notes ); my $ice = nfreeze( \@notes ); dd( $ice, thaw( $ice ) ); dd( [ map { $_->pack } @notes ] ); dd( [ map { Note->unpack( $_ ) } map { $_->pack } @notes ] ); dd( [ map { $_->freeze } @notes ] ); dd( [ map { Note->thaw( $_ ) } map { $_->freeze } @notes ] ); } __END__ [ bless({ message => "Ro Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Sham Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Bo Wed May 29 00:14:43 2013" }, "Note"), ] ( "\5\t\2\0\0\0\3\4\21\4Note\3\0\0\0\1\n\eRo Wed May 29 00:14:43 2013\ +0\0\0\amessage\4\22\0\3\0\0\0\1\n\35Sham Wed May 29 00:14:43 2013\0\0 +\0\amessage\4\22\0\3\0\0\0\1\n\eBo Wed May 29 00:14:43 2013\0\0\0\ame +ssage", [ bless({ message => "Ro Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Sham Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Bo Wed May 29 00:14:43 2013" }, "Note"), ], ) [ { __CLASS__ => "Note", message => "Ro Wed May 29 00:14:43 2013" }, { __CLASS__ => "Note", message => "Sham Wed May 29 00:14:43 2013" }, { __CLASS__ => "Note", message => "Bo Wed May 29 00:14:43 2013" }, ] [ bless({ message => "Ro Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Sham Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Bo Wed May 29 00:14:43 2013" }, "Note"), ] [ "{\"__CLASS__\":\"Note\",\"message\":\"Ro Wed May 29 00:14:43 2013\" +}", "{\"__CLASS__\":\"Note\",\"message\":\"Sham Wed May 29 00:14:43 2013 +\"}", "{\"__CLASS__\":\"Note\",\"message\":\"Bo Wed May 29 00:14:43 2013\" +}", ] [ bless({ message => "Ro Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Sham Wed May 29 00:14:43 2013" }, "Note"), bless({ message => "Bo Wed May 29 00:14:43 2013" }, "Note"), ]
      Thanks Anonymous Monk :) Now it works and i have learned a bit more. ;) But what is "no Moose" suppose to do? Never seen it.
        But what is "no Moose" suppose to do? Never seen it.

        As the entry in perlfunc states:
        See the use function, of which no is the opposite.

        update: fixed b0rken links

Re: Storing an array of Moose objects
by Anonymous Monk on May 29, 2013 at 06:59 UTC
    I get Global symbol "@Arr" requires explicit package name , after I fix that up, I get
    firstmessage Output packed strings: HASH(0xe0adb4) HASH(0x13ae43c) After reading file: $VAR1 = [ bless( { 'message' => 'firstmessage' }, 'Note' ), bless( { 'message' => 'secondmessage' }, 'Note' ) ];

    Is that what you wanted?