[CDBI] Using freeze/thaw to munge a column value.

Perrin Harkins perrin at elem.com
Mon Oct 24 19:53:16 BST 2005


On Mon, 2005-10-24 at 11:45 -0700, George Hartzell wrote:
> I want to create a simple class that maps to a two-column table, with
> a key and a value (something to pass args around in Mason components,
> since sessions don't behave well).
[...]
> Does anyone have a simpleton's example?

This was my solution, for passing parameters to a job queue system that
keeps state in the database.  Probably could be more efficient.

__PACKAGE__->has_a(
    'parameters' => 'Arcos::DB::Job::Serialized',
    inflate => sub { Arcos::DB::Job::Serialized->inflate(shift) },
    deflate => sub {
        Arcos::DB::Job::Serialized->deflate(shift);
});

# define simple in-line class to handle serializing parameters
BEGIN {

    package Arcos::DB::Job::Serialized;
    use Storable;

    sub inflate {
        my ($class, $serialized) = @_;
        my $hashref = Storable::thaw($serialized);
        bless $hashref, $class;
        return $hashref;
    }

    sub deflate {
        my ($class, $hashref) = @_;
        my $serialized = Storable::nfreeze($hashref);
        return $serialized;
    }
}

- Perrin





More information about the ClassDBI mailing list