gpt4 book ai didi

perl - Moose属性如何“扮演”鼠标角色?

转载 作者:行者123 更新时间:2023-12-05 00:36:02 26 4
gpt4 key购买 nike

我有一个构成Mouse角色的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 :: Class = HASH(0x23932dc)的'NotMy :: Mouse :: Role'失败(不是isa NotMy :: Mouse :: Role),位于...


有哪些方法可以在不将Mouse对象更改为Moose的情况下正常工作?

最佳答案

您可以执行此操作的一种方法是创建一个自定义类型约束,该约束可以执行所需的检查。

use Moose::Util::TypeConstraints;

subtype 'MouseRole'
=> as 'Object'
=> where sub { $_->does('NotMy::Mouse::Role') };

has 'mouse_obj' => (
is => 'ro',
isa => 'MouseRole', # "isa" not "does"!
);

关于perl - Moose属性如何“扮演”鼠标角色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10810171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com