[CDBI] Inflating/deflating with Storable

[prev] [thread] [next] [Date index for 2006/02/17]

From: Dan Lenski
Subject: [CDBI] Inflating/deflating with Storable
Date: 08:11 on 17 Feb 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 =3D> 'UNIVERSAL', inflate =3D> sub { freeze
\shift }, deflate =3D> sub { my $d=3Dshift; $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) =3D shift; bless {data=3D>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?=20
Thanks!

Dan Lenski

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] Inflating/deflating with Storable
Dan Lenski 08:11 on 17 Feb 2006

[CDBI] Re: Inflating/deflating with Storable
Edward J. Sabol 23:02 on 17 Feb 2006

Re: [CDBI] Inflating/deflating with Storable
Marius Feraru 01:20 on 21 Feb 2006

Generated at 12:31 on 28 Feb 2006 by mariachi v0.52