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:
package Foo;
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
use DateTime::Format::Natural;
subtype 'BRK::DT' => as class_type('DateTime');
coerce 'BRK::DT' => from 'Str' => via
{DateTime::Format::Natural->new->parse_datetime($_)};
has start => (is => 'rw', isa => 'Maybe[BRK::DT]', coerce => 1);
package main;
my $foo = Foo->new;
my $date;
$date = '2010-05-01';
$foo->start($date);
print $foo->start->month_name;
__END__
This program works, but if I comment the following line:
$date = '2010-05-01';
than it gives the error:
Cannot coerce without a type coercion at
e:/usr/site/lib/Moose/Meta/TypeConstraint.pm line 83
Moose::Meta::TypeConstraint::coerce('Moose::Meta::TypeConstraint::Parameterized=HASH(0x2570c8c)',
'2010-05-01') called at accessor start defined at E:\lucru\moose\test8.pl line
16
Foo::start('Foo=HASH(0x256de04)', '2010-05-01') called at
E:\lucru\moose\test8.pl line 20
Thank you for your help.
Octavian