Subject: | Moose::Util::TypeConstraint type/subtype error messages |
Date: | Wed, 09 Jul 2008 16:21:15 -0400 |
To: | bug-Moose [...] rt.cpan.org |
From: | Charles Alderman <charlie-perl [...] aldermania.com> |
$Moose::Version eq '0.54'
Regarding type() and subtype() under Moose::Util::TypeConstraint:
$_ passed to the message clause is a stringyfied version of the value
passed into an attribute declared of that custom type. It seems like
$_ should be the actual value, just like the $_ in the where clause of
the custom type.
Here's an example:
package MyObject;
use Moose;
package Foo;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'MyArrayRef'
=> as 'ArrayRef'
=> where { $_->[0] eq 'some element' }
=> message { ref $_ ? "ref: ". ref $_ : 'scalar' } # stringy
looking 'ARRAY(0x123..)'
;
subtype 'MyObjectType'
=> as 'Object'
=> where { $_->isa('MyObject') }
=> message {
if ( $_->isa('SomeObject') ) {
return 'More detailed error message';
}
elsif ( blessed $_ ) {
return 'Well it is an object';
}
else {
return 'Doh!';
}
}
;
type 'NewType'
=> where { $_->isa('MyObject') }
=> message { blessed $_ ? 'blessed' : 'scalar' }
;
has 'obj' => ( is => 'rw', isa => 'MyObjectType' );
has 'ar' => ( is => 'rw', isa => 'MyArrayRef' );
has 'nt' => ( is => 'rw', isa => 'NewType' );
package main;
my $foo = Foo->new;
my $obj = MyObject->new;
# Error message
$foo->ar([]); # scalar
$foo->obj($foo); # Doh!
$foo->nt($foo); # scalar
Thanks,
Charles Alderman