I have a type constraint I've set up, called 'MyApp::DateOnly'. It's for a DateTime object with certain restrictions.
type 'MyApp::DateOnly' => as 'DateTime' => where { blessed($_) && $_->isa('DateTime') && $_->hour == 0 && $_->min == 0 && $_->sec == 0 };
I have also included a couple of simple coercions, so that the user can pass a properly-formatted string as a convenience, and so that DateTime objects are made to follow the limitations of the type:
coerce 'Search::DateOnly' => from 'DateTime' => via { $_->truncate(to => 'day') }; coerce 'Search::DateOnly' => from 'Str' => via { state $parser = DateTime::Format::Strptime->new( pattern => '%Y/%m/%d'); $parser->parse_datetime($_); };
This all works fine.
Now I need to allow undef as a null value for certain attributes. I thought I could set up such an attribute like this:
has foo => ( is => 'ro', isa => 'Maybe[MyApp::DateOnly]', coerce => 1, );
but it doesn't work -- not only does it fail on undef arguments, it won't even accept and coerce the Str or DateTime arguments it did before! Is Maybe[] processing not done automatically? How do I enable it for this type constraint?
In reply to Moose type question by Sue D. Nymme
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |