kcott has asked for the wisdom of the Perl Monks concerning the following question:
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:
$ 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.
-- Ken
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose::Util::TypeConstraints - Querying subtype() Caveat
by Khen1950fx (Canon) on Jul 20, 2012 at 11:50 UTC | |
by kcott (Archbishop) on Jul 20, 2012 at 13:11 UTC | |
by choroba (Cardinal) on Jul 20, 2012 at 11:59 UTC | |
|
Re: Moose::Util::TypeConstraints - Querying subtype() Caveat
by tobyink (Canon) on Jul 20, 2012 at 12:24 UTC | |
by kcott (Archbishop) on Jul 20, 2012 at 13:56 UTC | |
|
Re: Moose::Util::TypeConstraints - Querying subtype() Caveat
by Anonymous Monk on Jul 20, 2012 at 10:25 UTC | |
by kcott (Archbishop) on Jul 20, 2012 at 12:06 UTC | |
by Anonymous Monk on Jul 28, 2012 at 09:09 UTC |