Subject: | new constructor doesn't work properly on object |
Date: | Sun, 14 Sep 2008 17:51:58 +0300 |
To: | bug-Time-Piece [...] rt.cpan.org |
From: | Dotan Dimet <dotan [...] corky.net> |
Hi,
I've noticed that the Time::Piece constructor is broken.
use Time::Piece;
my $t1 = Time::Piece->new();
my $t2 = $t->new();
print $t2->ymd; # ERROR - $h is a date string, NOT a Time::Piece object.
I'm not calling new() on an object directly - it gets called by the
module code in a method called get_epochs when I'm trying to compare
dates. Specifically, when I have an undefined date value.
The code for new() assumes that the variable class might contain an
object, but using it directly in "bless" (as in the last line of the
method) just causes it to be stringified. It should probably be changed to:
return bless $self, ref($class) || $class;
The existing code is:
sub new {
my $class = shift;
my ($time) = @_;
my $self;
if (defined($time)) {
$self = $class->localtime($time);
}
elsif (ref($class) && $class->isa(__PACKAGE__)) {
$self = $class->_mktime($class->epoch, $class->[c_islocal]);
}
else {
$self = $class->localtime();
}
return bless $self, $class;
}
Thanks,
Dotan.