我无法使用MooseX::Declare
覆盖已声明的属性。
use MooseX::Declare;
role Person {
has 'name' => (
is => 'ro',
isa => 'Str',
default => 'John',
);
}
class Me with Person {
has '+name' => (
default => 'Michael',
);
}
执行代码报告的错误:
Could not find an attribute by the name of 'name' to inherit from in Me at /usr/lib/perl5/Moose/Meta/Class.pm line 711
Moose::Meta::Class::_process_inherited_attribute('Moose::Meta::Class=HASH(0x2b20628)', 'name', 'default', 'Michael', 'definition_context', 'HASH(0x114fd38)') called at /usr/lib/perl5/Moose/Meta/Class.pm line 694
Moose::Meta::Class::_process_attribute('Moose::Meta::Class=HASH(0x2b20628)', '+name', 'default', 'Michael', 'definition_context', 'HASH(0x114fd38)') called at /usr/lib/perl5/Moose/Meta/Class.pm line 566
Moose::Meta::Class::add_attribute('Moose::Meta::Class=HASH(0x2b20628)', '+name', 'default', 'Michael', 'definition_context', 'HASH(0x114fd38)') called at /usr/lib/perl5/Moose.pm line 79
Moose::has('Moose::Meta::Class=HASH(0x2b20628)', '+name', 'default', 'Michael') called at /usr/lib/perl5/Moose/Exporter.pm line 370
Moose::has('+name', 'default', 'Michael') called at Test.pm line 12
main::__ANON__() called at /usr/share/perl5/MooseX/Declare/Syntax/MooseSetup.pm line 81
MooseX::Declare::Syntax::MooseSetup::__ANON__('CODE(0x2b0be20)') called at Test.pm line 21
这有效,但不是基于角色:
class Person {
has 'name' => (
is => 'ro',
isa => 'Str',
default => 'John',
);
}
class Me extends Person {
has '+name' => (
default => 'Michael',
);
}
使用角色时我的代码出了什么问题?是否有可能覆盖属性行为?
答案 0 :(得分:3)
irc.perl.org上的irc用户#moose给出了解决方案:
<phaylon> iirc MX:Declare will consume the roles declared in the block at the end. try with 'Person'; in the class block before the has
所以以下代码现在正在运行:
use MooseX::Declare;
role Person {
has 'name' => (
is => 'ro',
isa => 'Str',
default => 'John',
);
}
class Me {
with 'Person';
has '+name' => (
default => 'Michael',
);
}
非常感谢phaylon
。