我已经整理了一个小测试用例来演示我的问题:
package P1; use Moose; use MooseX::Storage; with Storage; has 'blah' => ( is => 'rw',); package P2; use Moose; use MooseX::Storage; with Storage; has 'lol' => ( is => 'rw',traits => ['DoNotSerialize'] ); package P3; use Moose; extends 'P2'; has 'magic' => ( is => 'rw',); package Test; my $obj = P3->new( magic => 'This ok!',lol => sub { 'weee' } ); my $stored = P1->new(blah => $obj); use Data::Dumper; print Dumper ($stored->pack);
我希望这打印对象,但不打印P2包中的’lol’属性 – 但是,我仍然可以在$stored-> pack的结果中看到这一点
$VAR1 = { '__CLASS__' => 'P1','blah' => bless( { 'magic' => 'This ok!','lol' => sub { "DUMMY" } },'P3' ) };
解决方法
你可以把’等等’变成P3的isa ……
has 'blah' => ( is => 'rw',isa => 'P3',);
现在Dumper($stored-> pack)显示了这个….
$VAR1 = { '__CLASS__' => 'P1','blah' => { '__CLASS__' => 'P3','magic' => 'This ok!' } };
看起来像这个Moose对象的正确序列化?