views:

70

answers:

2

I have a Moose class that i would like to store using Apache::Session::File.

However, Apache::Session::File by default will not store it and instead i get the error message:

 (in cleanup) Can't store CODE items at blib\lib\Storable.pm (autosplit into blib\lib\auto\Storable\_freeze.al)...

This problem can be circumvented by setting

$Storable::Deparse = 1;
$Storable::Eval = 1;

in order to allow CODE references to be serialized.

The offending method in the Moose class is listed below, which retrieves a column from a mysql database:

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;
}

So presumably the dbh object (isa DBIx::Connector) contains CODE references.

Is there a better alternative in order to allow serialization of this Moose class than setting $Storable::Deparse and $Storable::Eval ?

The following test script produces the error:

#!/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__

The SQL connection class is defined as:

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;

The TestCell class is defined as:

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;
A: 

The documentation for the Storable class, which is used by Apache::Session::File uses $Storable::Deparse and $Storable::Eval, but also goes on to suggest a method for serialization of CODE references and deserialization in a 'safe' compartment. An example is given in the Examples section of this page:

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

Mike
+3  A: 

I really doubt you really need to serialize code references; the example you included doesn't have any. You don't want to be serializing DBIx::Connector objects anyway, as they are only specific to the current runtime instance.

DBIx::Connector objects may have a small coderef in them, as it is common to wrap access to the dbh in a sub to catch cases where the connection goes away (see the discussion of 'fixup') in the documentation.

Serialization of Moose objects is handled by MooseX::Storable, which is easily extendable. You could customize a serializer in there to fit your needs - i.e. select which attributes to serialize and which to ignore.

Ether
PS. I hope that you're not creating a new DBIx::Connector object for every one of your Moose classes; you'll very quickly run out of DB handles that way. You should share objects as much as possible to make your application more efficient.
Ether
I have expanded the problem description a bit. I am not creating DB objects for every one of the Moose classes.Thanks for your suggestion: MooseX::Storage seems to be the way to go. If I use it in conjuction with a traits => ['DoNotSerialize'] on the DB attribute then i can serialize the object.Interestingly, if i replace the DBIx::Connector impelmentation in GSM::SQLConnection with an equivalent one using DBI, then i dont get the problem of "Can't store CODE items".
Hartmut Behrens