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 参照が含まれています。

$Storable::Deparse と $Storable::Eval を設定するよりも、この Moose クラスのシリアル化を許可するためのより良い代替手段はありますか?

次のテスト スクリプトでは、エラーが発生します。

#!/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 へのアクセスをラップするのが一般的であるためです (「修正」の説明を参照) 。

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 に答える