[CDBI] Inflating/deflating with Storable

Marius Feraru altblue at n0i.net
Tue Feb 21 01:20:12 GMT 2006

Hash: SHA1

Dan Lenski wrote:
> 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 :-(

1) thaw/freeze - inflate/deflate, that's the right process ;-)
2) forgot to bless the inflated reference:

    udata   => 'MyFridge',
    inflate => sub { bless thaw(shift), 'MyFridge' },
    deflate => sub { freeze(shift) }

now you have it...

> So, next I tried making a stupid little wrapper class
... so I agree on its quality (err, usefulness?) ;-)

> Now, I can access the data via $obj->udata->data
$obj->udata is enough now.

> but when I modify the data with the accessor, Class::DBI doesn't
> realize it has changed and won't actually update the database :-(
If you mean something like

  $obj->udata->{foo} = 'bar';

then remember the "udata" just provides access to that _reference_
(which will not be noticed by CDBI whenever you modify something "inside").

Else, if you meant (which you should've):

  $obj->udata({foo => 'bar'});

you should add a trigger to take care of preparing your data, as
(obviously) CDBI expects your data to be already inflated upon "set":

__PACKAGE__->add_trigger(before_set_udata => sub {
    my ($self, $value) = @_;
    if (!defined $value || !ref $value) {
        $self->_croak('I only work with data references!');
    if (!blessed($value) || ref $value ne 'MyFridge') {
        my $blessed_value = bless $value, 'MyFridge';
        $self->_attribute_store({ udata => $blessed_value });

("blessed" is in Scalar::Util - core module)

- --
Marius Feraru

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3162 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.digitalcraftsmen.net/mailman/private/classdbi/attachments/20060221/165c93f8/smime.bin

More information about the ClassDBI mailing list