Newbie Moose,MooseX :: Storage和MooseX :: Log4Perl问题:无法存储对象

时间:2011-09-02 16:38:16

标签: perl moose moosex-types

我正在编写我的第一个驼鹿基础程序。该程序创建一个“状态”对象,该对象是其他几个驼鹿对象的组合。在程序结束时,我希望将“状态”对象保存到文件中。我正在尝试使用MooseX :: Storage来执行此操作。我得到一个错误(下面),这意味着它正在尝试序列化MooseX :: Log :: Log4perl对象。

Object (Log::Log4perl::Logger=HASH(0x2211c800)) does not have a &pack method, cannot collapse at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Engine.pm line 205
    MooseX::Storage::Engine::__ANON__('Log::Log4perl::Logger=HASH(0x2211c800)', 'HASH(0x20c54890)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Engine.pm line 88
MooseX::Storage::Engine::collapse_attribute_value('MooseX::Storage::Engine=HASH(0x2211cac0)', 'Moose::Meta::Attribute=HASH(0x20419510)', 'HASH(0x20c54890)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Engine.pm line 60
MooseX::Storage::Engine::collapse_attribute('MooseX::Storage::Engine=HASH(0x2211cac0)', 'Moose::Meta::Attribute=HASH(0x20419510)', 'HASH(0x20c54890)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Engine.pm line 141
MooseX::Storage::Engine::map_attributes('MooseX::Storage::Engine=HASH(0x2211cac0)', 'collapse_attribute', 'HASH(0x20c54890)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Engine.pm line 37
MooseX::Storage::Engine::collapse_object('MooseX::Storage::Engine=HASH(0x2211cac0)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Basic.pm line 13
MooseX::Storage::Basic::pack('ScanCtr::State=HASH(0x2100ee40)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/Format/JSON.pm line 24
MooseX::Storage::Format::JSON::freeze('ScanCtr::State=HASH(0x2100ee40)') called at /usr/lib/perl5/site_perl/5.8.8/MooseX/Storage/IO/File.pm line 19

这是我的设置。我有一个名为“ScanCtr :: ScanMe”的类,它只做这个:

package ScanCtr::ScanMe;
use Moose;
with 'MooseX::Log::Log4perl';
use namespace::autoclean;
use Log::Log4perl qw(:easy);
BEGIN {
    Log::Log4perl->easy_init({
        level   => $DEBUG,
        file    => ">>/var/log/sc.log",
        layout  => '%d %p [%P] %l %m%n',
    });
};
1;

我的其他对象包括扩展此对象(这可能是我的问题)所以我可以在每个对象中执行$self->log->debug("debug message");之类的操作。例如:

package ScanCtr::State;
use ScanCtr::Request;
use Moose;
extends 'ScanCtr::ScanMe';
use Moose::Util::TypeConstraints;
use MooseX::Storage;
our $VERSION = '0.01';
with Storage ( 'format' => 'JSON', 'io' => 'File');
use namespace::autoclean;


has 'requests'          => (
    traits  => [ 'Hash' ],
    is      => 'rw',
    isa     => 'HashRef[ScanCtr::Request]',
    default => sub { {} },
    handles => {
        count           => 'count',
        get             => 'get',
        set             => 'set',
        delete_request  => 'delete',
        request_keys    => 'keys',
        kv              => 'kv',
    }
);
1;

那么有没有办法从存储中排除MooseX :: Log :: Log4perl?有没有更好的方法可以避免这个问题?我还有其他新手的错误吗?

感谢您的帮助, 托德。

1 个答案:

答案 0 :(得分:1)

这个应该有效(但我还没有测试过)。在ScanCtr::ScanMe内,您需要覆盖Logger属性并应用DoNotSerialize中的MooseX::Storage特征。

package ScanCtr::ScanMe;
use Moose;
with 'MooseX::Log::Log4perl';
use namespace::autoclean;
use Log::Log4perl qw(:easy);
BEGIN {
    Log::Log4perl->easy_init({
        level   => $DEBUG,
        file    => ">>/var/log/sc.log",
        layout  => '%d %p [%P] %l %m%n',
    });
};

has '+logger' => { traits => ['DoNotSerialize'] };

1;