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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.