[英]Newbie Moose, MooseX::Storage, and MooseX::Log4Perl problem: Can't store object
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.
我正在尝试使用MooseX :: Storage来执行此操作。 I get an error (below) that implies to me that it is trying to serialize the MooseX::Log::Log4perl object.
我得到一个错误(下面),这意味着它正在尝试序列化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
Here's my setup. 这是我的设置。 I have a class called "ScanCtr::ScanMe" and it does only this:
我有一个名为“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;
My other objects including extend this object (which may be my problem) so I can do stuff like $self->log->debug("debug message");
我的其他对象包括扩展这个对象(这可能是我的问题)所以我可以做像
$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? 那么有没有办法从存储中排除MooseX :: Log :: Log4perl? Is there a better approach that will avoid this problem?
有没有更好的方法可以避免这个问题? What other newbie mistakes am I making?
还有什么其他新手的错误?
Thanks for any help, Todd. 谢谢你的帮助,托德。
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
. 在
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.