use DateTime; subtype DateTime => as Object => where { $_->isa('DateTime') }; #### use DateTime; subtype 'DateTime' => as 'Object' => where { $_->isa('DateTime') }; #### use strict; use warnings; package Foo; use Moose; use TmpTypes qw(NewType); has 'item' => (isa => NewType, is => 'rw', required => 1, coerce => 1); sub is_new { my ($self, $item) = @_; return "Yup, ($item) is new!\n" if (is_NewType($item)); return "Nope, ($item) is old.\n"; } package main; use TmpTypes qw(NewType); my $obj = Foo->new(item => 'car'); my $thing = $obj->item; print "Is $thing new? ".$obj->is_new($thing); $thing = 'house'; print "Is $thing new? ".$obj->is_new($thing); $thing = to_NewType('House'); print "Is $thing new? ".$obj->is_new($thing); #### package TmpTypes; use strict; use warnings; use MooseX::Types -declare => [qw(NewType)]; use MooseX::Types::Moose qw(Str); subtype NewType, as Str, where {$_ =~ /^new_/}; coerce NewType, from Str, via { 'new_'.$_ }; #### subtype 'NewType', as 'Str', where {$_ =~ /^new_/}; coerce 'NewType', from 'Str', via { 'new_'.$_ }; #### use strict; use warnings; use Moose::Util::TypeConstraints; package Foo; use Moose; use TmpTypes qw(NewType); has 'item' => (isa => 'NewType', is => 'rw', required => 1, coerce => 1); sub is_new { my ($self, $item) = @_; return "Yup, ($item) is new!\n" if (is_NewType($item)); return "Nope, ($item) is old.\n"; } package main; use TmpTypes qw(NewType); my $obj = Foo->new(item => 'car'); my $thing = $obj->item; print "Is $thing new? ".$obj->is_new($thing); $thing = 'house'; print "Is $thing new? ".$obj->is_new($thing); $thing = to_NewType('House'); print "Is $thing new? ".$obj->is_new($thing); #### package TmpTypes; use strict; use warnings; use MooseX::Types -declare => [qw(NewType)]; use MooseX::Types::Moose qw(Str); subtype 'NewType', as 'Str', where {$_ =~ /^new_/}; coerce 'NewType', from 'Str', via { 'new_'.$_ };