3

我有一个想要使用 Apache::Session::File 存储的 Moose 类。

但是,默认情况下 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 对象(即 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;
4

2 回答 2

4

我真的怀疑你真的需要序列化代码引用;您包含的示例没有。无论如何,您都不想序列化 DBIx::Connector 对象,因为它们只特定于当前运行时实例。

DBIx::Connector 对象中可能有一个小的 coderef,因为通常会将对 dbh 的访问包装在 sub 中以捕获连接消失的情况(请参阅文档中的“fixup”讨论) 。

Moose 对象的序列化由MooseX::Storable处理,易于扩展。您可以在其中自定义一个序列化程序以满足您的需求 - 即选择要序列化的属性和忽略的属性。

于 2010-06-18T14:55:27.780 回答
0

Apache::Session::File 使用的 Storable 类的文档使用 $Storable::Deparse 和 $Storable::Eval,但也继续建议一种在“安全”中对 CODE 引用进行序列化和反序列化的方法'隔间。本页的示例部分给出了一个示例:

http://perldoc.perl.org/Storable.html#CODE-REFERENCES

于 2010-06-18T13:36:33.640 回答