Actually this can all be done without MooseX::Types just as easily.
class_type 'DateTime';
subtype 'DT' => as 'Maybe[DateTime]';
and then the rest is pretty much the same too.
- Stevan
On Jun 14, 2010, at 5:39 PM, Oliver Charles wrote:
(Sorry, forgot to reply to all)
On Mon, Jun 14, 2010 at 10:13 PM, Octavian Rasnita
<orasn...@gmail.com> wrote:
Hello,
I am trying to create a module that accepts a string property as
2010-05-01 and coerces it to DateTime by default, but that property
might be also undef.
I have tried:
If you're happy using MooseX::Types, this is fairly easy:
package Foo;
use Moose;
use DateTime;
use DateTime::Format::Natural;
use MooseX::Types -declare => [qw( DT )];
use MooseX::Types::Moose qw( Maybe Str Undef );
subtype DT, as Maybe[class_type('DateTime')];
coerce DT,
from Str, via { DateTime::Format::Natural->new-
>parse_datetime($_) };
has start => (is => 'rw', isa => DT, coerce => 1);
package main;
my $foo = Foo->new;
my $date;
#$date = '2010-05-01';
$foo->start($date);
print $foo->start->month_name;
The code still errors, because you can't call month_name on undef, but
that's your problem ;)
--
Oliver Charles / aCiD2