Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl可存储检索Moose对象数组_Perl_Moose_Storable - Fatal编程技术网

Perl可存储检索Moose对象数组

Perl可存储检索Moose对象数组,perl,moose,storable,Perl,Moose,Storable,我尝试将Moose对象数组存储到YAML或JSON 保存效果很好,但当我尝试恢复对象时,它们是空的: $VAR1 = bless({}, 'Note'); $VAR2 = bless({}, 'Note'); 这是我的密码: 注:下午六时 package Note; use strict; use warnings; use Moose; use MooseX::Storage; with Storage('format' => 'JSON', 'io' => 'File');

我尝试将Moose对象数组存储到YAML或JSON

保存效果很好,但当我尝试恢复对象时,它们是空的:

$VAR1 = bless({}, 'Note');
$VAR2 = bless({}, 'Note');
这是我的密码:

注:下午六时

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

希望有人能提供帮助:)

可能会有所帮助。请从
注释中删除您的构造函数。pm
-Moose会为您这样做,实现您自己的构造函数可能会干扰它。同样地,
$obj->{'message'}=“firstmessage”应该是
$obj->message($firstmessage)尼尔·斯莱特:谢谢,我现在就来解决这个问题。我没有hashref或类似的东西;)