新手 Moose、MooseX::Storage 和 MooseX::Log4Perl 问题:无法存储对象

发布于 2024-12-02 17:03:22 字数 2903 浏览 1 评论 0原文

我正在编写我的第一个驼鹿基础程序。该程序创建一个“状态”对象,它是几个其他驼鹿对象的组合。在程序结束时,我希望将“状态”对象保存到文件中。我正在尝试使用 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 之类的操作(“调试消息”); 在我的每个对象中。例如:

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 ?有没有更好的方法可以避免这个问题?我还犯过哪些新手错误?

感谢您的帮助, 托德。

I'm writing my first moose base program. The program creates a "state" object that is a composition of several other moose objects. At the end of the program, I wish to save the "state" object to a file. I'm attempting to use MooseX::Storage to do this. I get an error (below) that implies to me that it is trying to serialize the MooseX::Log::Log4perl object.

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

Here's my setup. I have a class called "ScanCtr::ScanMe" and it does only this:

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;

My other objects including extend this object (which may be my problem) so I can do stuff like $self->log->debug("debug message"); within each of my objects. For example:

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;

So is there a way to exclude the MooseX::Log::Log4perl from the Storage? Is there a better approach that will avoid this problem? What other newbie mistakes am I making?

Thanks for any help,
Todd.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱她像谁 2024-12-09 17:03:22

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

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;

This should work (but I haven't tested it). Inside ScanCtr::ScanMe you need to override the Logger attribute and apply the DoNotSerialize trait from 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文