G'day Monks,
In Moose::Util::TypeConstraints, under Slightly Less Important Caveat, this is shown as silently failing:
use DateTime;
subtype DateTime => as Object => where { $_->isa('DateTime') };
The recommended solution is given as:
use DateTime;
subtype 'DateTime', as 'Object', where { $_->isa('DateTime') };
This didn't look right to me, especially as the quoting mechanism of => is supposed to be implicit and ignoring precedence (see perlop - Comma Operator).
I ran both forms through B::Deparse and got the same results:
$ perl -MO=Deparse,-p -e '
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
subtype DateTime => as Object => where { $_->isa(q{DateTime}) };
'
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
use warnings;
use strict 'refs';
subtype('DateTime', as('Object', where(sub {
$_->isa('DateTime');
}
)));
-e syntax OK
$ perl -MO=Deparse,-p -e '
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
subtype q{DateTime}, as Object => where { $_->isa(q{DateTime}) };
'
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
use warnings;
use strict 'refs';
subtype('DateTime', as('Object', where(sub {
$_->isa('DateTime');
}
)));
-e syntax OK
I ran diff on both outputs: they're identical.
The versions of Moose and Moose::Util::TypeConstraints that I'm using are both 2.0603 and I'm using Perl 5.14.2.
Two thoughts occurred to me regarding this:
-
Moose source has use 5.008;. Perhaps the quoting mechanism of => worked differently under an earlier version of Perl than I'm using: while the wording has changed between versions, I don't see anything indicating delayed quoting. For reference, here's perlop - Comma Operator for 5.8.8, 5.14.2 and latest (5.16.0).
-
Perhaps this is a documentation error. If the type name is not quoted (explicitly or implicitly), the syntax is still fine but the output is quite different:
$ perl -MO=Deparse,-p -e '
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
subtype DateTime, as Object => where { $_->isa(q{DateTime}) };
'
use Moose;
use Moose::Util::TypeConstraints;
use DateTime;
use warnings;
use strict 'refs';
('DateTime'->subtype, as('Object', where(sub {
$_->isa('DateTime');
}
)));
-e syntax OK
Can anyone shed any light on this? Thanks in advance.
Update:
I added use DateTime; to both of the first two statements to avoid ambiguity with similar code in the linked documentation.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.