develooper Front page | perl.module-authors | Postings from February 2005


Daisuke Maki
February 5, 2005 00:27
Message ID:
I have a module that deferes inflation of columns in Class::DBI until
when they are used... is there something like this available?

=head1 NAME

Class::DBI::LazyInflator - Defer Inflating Of Columns Until They Are Used


  package MyData;
  use base qw(Class::DBI);
  use Class::DBI::LazyInflator;
  use DateTime;
  use DateTime::Format::MySQL;

    inflate => sub { DateTime::Format::MySQL->parse_datetime(shift) },
    deflate => sub { DateTime::Format::MySQL->format_datetime(shift) },

  my $obj = MyData->retrieve($key); # lastmod is not inflated yet
  $obj->lastmod()->year();          # now it is.



Class::DBI::LazyInflator is a utility class that allows you to create
DBI columns that only inflate to an object when it is required. When a
row is fetched, the column that was specified via has_lazy() is wrapped
by Class::DBI::LazyInflator, such that it is inflated only when the
column is actually used.

As seen in the SYNOPSIS section, one application of this class is for
columns that inflate to objects that are costly to create, such as
DateTime. Class::DBI::LazyInflator allows you defer materialization of
such objects until when you really need it

=head1 METHODS

=head2 has_lazy($col, $class, inflate => ..., deflate => ...)

has_lazy() declares that column is to be inflated lazily, and is
installedto the calling package's namespace upon call to "use
Class::DBI::LazyInflator".The arguments are exactly the same has has_a().

=head1 AUTHOR

Daisuke Maki <>

=head1 SEE ALSO


=cut Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About