On Mon, 14 Jul 2003, Rick Measham wrote:
> Maybe an example will explain why I want to have an undef DateTime:
>
> print $natural->parse_datetime("Feb 29th, this year")
> ->set( day => 31 )
> ->add( days => 12)
> ->datetime;
>
> Of course, this won't always work because:
> 1. the string "Feb 29th this year" returns undef 75% of the time.
> 2. We can't always set the day to 31, especially in Feb
>
> If I could return DateTime::Undef when it doesn't parse, and if set()
> returned DateTime::Undef if it was out of bounds, then we can turn this
> code:
>
> $parsed = $natural->parse_datetime("Feb 29th, this year");
> $parsed->set( day => 31 ) if defined($parsed);
> $parsed->add(days => 12) if defined($parsed);
> print $parsed->datetime if defined($parsed);
It hardly has to be this awkward.
my $parsed = $natural->parse_datetime("Feb 29th, this year");
if ($parsed) {
print $parsed->set( day => 31 )->add( days => 12 )->datetime;
} else {
# actually be forced to handle failure case, which is good!
}
I like the idea that code _breaks_ if the date can't be parsed, because
that way programmers have to include explicit paths for handling failure.
This is a _good_ thing, as it encourages better programming practices.
-dave
/*=======================
House Absolute Consulting
www.houseabsolute.com
=======================*/