[CDBI] Inflating/deflating with Storable

Dan Lenski dlenski at gmail.com
Fri Feb 17 08:11:18 GMT 2006


Hi,
I'm a Class::DBI noob (love it so far!) and I'm having a problem
trying to use the Storable module with Class::DBI.

I want to store an *arbitrary* Perl data structure in a column of type
BLOB (random variable-length binary data).  This is easy to do by
serializing/deserializing the data with Storable::freeze/thaw.  I
would like to make the data automatically inflate/deflate with
Class::DBI.

The problem is that the output of the freeze() function is simply a
string, and the output of thaw() can have pretty much any type at all.
 Class::DBI::Relationship::HasA doesn't like this state of affairs
since it expects a specific class.

First, I tried this:

__PACKAGE__->has_a(udata => 'UNIVERSAL', inflate => sub { freeze
\shift }, deflate => sub { my $d=shift; $d and ${thaw $d} });

This doesn't work, it complains about "unable to find new method for
UNIVERSAL class".  Plus, it won't work at all if the inflated item
happens not to be a blessed reference :-(

So, next I tried making a stupid little wrapper class for Storable
that has_a will like:

package IceCube;
use base qw(Class::Accessor);
__PACKAGE__->mk_accessors(qw(data));
use Storable qw(freeze thaw);
sub new { my ($class,$obj) = shift; bless {data=>thaw($obj)}, $class }
sub as_string { freeze \shift->{data} }

Now, I can access the data via $obj->udata->data, but when I modify
the data with the accessor, Class::DBI doesn't realize it has changed
and won't actually update the database :-(

I subsequently tried a few other things, triggers and such, and that
was even worse.  I'm thinking there's got to be a simple way to make
inflate/deflate work with Storable.  Can anyone give me a hint? 
Thanks!

Dan Lenski




More information about the ClassDBI mailing list