gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:37:12 25 4
gpt4 key购买 nike

我正在编写我的第一个 moose 基础程序。该程序创建了一个“状态”对象,它是由其他几个驼鹿对象组成的。在程序结束时,我希望将“状态”对象保存到文件中。我正在尝试使用 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?有没有更好的方法可以避免这个问题?我还犯了哪些新手错误?

谢谢你的帮助,
托德。

最佳答案

这应该有效(但我还没有测试过)。内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;

关于perl - 新手 Moose、MooseX::Storage 和 MooseX::Log4Perl 问题:无法存储对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7286613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com