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"), ]
In reply to Re: Storing an array of Moose objects
by Anonymous Monk
in thread Storing an array of Moose objects
by demonking
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |