我有一个Moose类,我想用Apache :: Session :: File存储。
但是,默认情况下,Apache :: Session :: File不会存储它,而是收到错误消息:
(in cleanup) Can't store CODE items at blib\lib\Storable.pm (autosplit into blib\lib\auto\Storable\_freeze.al)...
通过设置
可以避免此问题$Storable::Deparse = 1;
$Storable::Eval = 1;
以便允许序列化CODE引用。
下面列出了Moose类中的违规方法,该方法从mysql数据库中检索列:
sub _build_cell_generic {
my ($self,$col) = @_;
my $sth = $self->call_dbh('prepare','select '.$col.' from '.$self->TABLE.' where CI = ? and LAC = ? and IMPORTDATE = ?');
$sth->execute($self->CI,$self->LAC,$self->IMPORTDATE);
my $val = $sth->fetchrow_array;
$sth->finish;
return defined $val ? $val : undef;
}
所以推测dbh对象(isa DBIx :: Connector)包含CODE引用。
为了允许这个Moose类的序列化比设置$ Storable :: Deparse和$ Storable :: Eval有更好的选择吗?
以下测试脚本会产生错误:
#!/usr/bin/perl -w
use Apache::Session::File;
use Test::More;
use strict;
use warnings;
require_ok( 'GSM::TestCell' );
require_ok( 'GSM::SQLConnection');
my $db = new_ok('GSM::SQLConnection');
my $cell4 = new_ok( 'GSM::TestCell' => [{LAC => 406, CI => 24491, DB => $db }] );
my %session;
tie %session, 'Apache::Session::File', undef, {Directory =>"./", LockDirectory => "./" };
print "BCCH is ",$cell4->BCCH,"\n";
$session{$cell4->ID} = $cell4;
done_testing();
__END__
SQL连接类定义为:
package GSM::SQLConnection;
#use DBI;
use Moose;
use DBIx::Connector;
has dbixc => (is => 'ro', isa => 'DBIx::Connector', lazy_build => 1, handles => [ qw(dbh) ]);
sub _build_dbixc {
my $self = shift;
my $dsn = 'DBI:mysql:testDB;host=127.0.0.1;port=3306';
return DBIx::Connector->new($dsn,'user','pwd');
}
sub call_dbh {
my $self = shift;
my $method = shift;
my @args = @_;
$self->dbixc->run(fixup => sub { $_->$method(@args) });
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
TestCell类定义为:
package GSM::TestCell;
use MooseX::NaturalKey;
use strict;
use warnings;
has [qw(LAC CI)] => (is => 'ro', required => 1);
has [qw(ID BCCH IMPORTDATE)] => (is => 'rw', lazy_build => 1);
has 'DB' => (is => 'rw', isa => 'GSM::SQLConnection', required => 1, );
has 'TABLE' => (is => 'rw', default => 'Cell');
primary key =>('LAC','CI');
sub _build_ID {
my $self = shift;
return join(',',$self->LAC,$self->CI);
}
sub _build_IMPORTDATE {return '2010-06-21'}
sub _build_BCCH {(shift)->_build_cell_generic('BCCHFrequency');}
sub _build_cell_generic {
my ($self,$col) = @_;
my $sth = $self->DB->call_dbh('prepare','select '.$col.' from '.$self->TABLE.' where CI = ? and LAC = ? and IMPORTDATE = ?');
$sth->execute($self->CI,$self->LAC,$self->IMPORTDATE);
my $val = $sth->fetchrow_array;
$sth->finish;
return defined $val ? $val : undef;
}
no Moose;
__PACKAGE__->meta->make_immutable;
1;
答案 0 :(得分:4)
我真的怀疑你真的需要序列化代码引用;你包含的例子没有。您不希望序列化DBIx :: Connector对象,因为它们仅特定于当前运行时实例。
DBIx :: Connector对象中可能包含一个小代码,因为在子代中包装对dbh的访问通常会捕获连接消失的情况(参见'fixup'的讨论){{ 3}}
Moose对象的序列化由documentation处理,可以轻松扩展。您可以在其中自定义序列化程序以满足您的需求 - 即选择要序列化的属性和要忽略的属性。
答案 1 :(得分:0)
Apache :: Session :: File使用的Storable类的文档使用$ Storable :: Deparse和$ Storable :: Eval,但是还继续提出了一个序列化CODE引用和反序列化的方法。一个'安全'的隔间。本页的示例部分给出了一个示例: