我有一个组成鼠标角色的Moose类。
package My::Moose::Class;
use Moose;
has 'mouse_obj' => (
is => 'ro',
does => 'NotMy::Mouse::Role',
);
package NotMy::Mouse::Role;
use Mouse::Role;
package NotMy::Mouse::Class;
use Mouse;
with 'NotMy::Mouse::Role';
这将导致错误,因为鼠标角色在Moose中未被识别为类型:
my $f = My::Moose::Class->new( mouse_obj => NotMy::Mouse::Class->new );
属性(mouse_obj)没有传递类型约束,因为:'NotMy :: Mouse :: Role'的验证失败,值为NotMy :: Mouse :: Class = HASH(0x23932dc)(不是isa NotMy :: Mouse ::角色)......
在不将鼠标对象更改为Moose的情况下,有哪些方法可以实现此功能?
答案 0 :(得分:1)
您可以这样做的一种方法是创建一个自定义类型约束来执行您想要的检查。
use Moose::Util::TypeConstraints;
subtype 'MouseRole'
=> as 'Object'
=> where sub { $_->does('NotMy::Mouse::Role') };
has 'mouse_obj' => (
is => 'ro',
isa => 'MouseRole', # "isa" not "does"!
);