[CDBI] has_a infation of a Time::Piece woes

Peter Speltz peterspeltz at gmail.com
Mon Oct 24 17:31:40 BST 2005

Hi. I'm using 3.09 and Mysql. I had this code for this TIMESTAMP
column named 'finished'.  The column is 0000-00-00 00:00:00 by
default.  If  'finished' has not been set to  a real time, then the
inflation code gives an "Out of Range' Time::Piece error.

__PACKAGE__->has_a("finished" => 'Time::Piece',
        inflate => sub { Time::Piece->strptime(shift, "%Y-%m-%d %T") },
        deflate => sub { my $tp = shift; return $tp->ymd.' '.$tp->hms; },

Anyone know how I would
1) make Time::Piece not have a date IE - stringify to ''.  T::P docs
did not help here.
2)change the inflate sub to return undef rather than a Time::Piece
object if the column is not defined yet.


For (1) I tried blessing an empty array into Time::Piece. Same out of
range error.

So i tried this for (2)  which from reading the docs , seems it should work. :

__PACKAGE__->has_a("finished" => 'Time::Piece',
        inflate => sub {
            my $t = shift;
            if ($t =~ /^0000-/) {
                  return;  # This makes Time::Piece have current time.
                  # return 0;      # Has beginning of epoch (Jan 1 1970)
            else {
                return Time::Piece->strptime($t, "%Y-%m-%d %T");
      . . .

But what is happening is the value my inflate sub returns is getting
passed to the Time::Piece constructor so i am getting some Times i do
not want..

>From has_a section of docs annotated by me [pjs . . . ]
If the foreign class is another Class::DBI representation retrieve is
called on that class with the column value.   [ pjs -- Ok. ]  Any
other object will be instantiated either by calling new($value) or
using the given 'inflate' method. [ pjs -- Ok. call constructor with
value of column *OR* use the inflate method ]   If the inflate method
name is a subref, it will be executed, and will be passed the value
and the Class::DBI object as arguments.
[ pjs -- No mention of calling new() with the result of the inflate subref.  ]


Thanks for any help.


More information about the ClassDBI mailing list