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.