2013-03-27 78 views
1

我無法用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', 
    ); 
} 

使用角色時有什麼錯我的代碼?有沒有可能重寫屬性行爲?

+1

請將您自己的答案作爲下面的實際答案發布,而不是編輯您的問題,並接受它。這有助於其他人找到它,並且您的問題不再顯示爲未答覆。 – simbabque 2013-03-27 15:08:07

+2

當定時器不見時,我會做,我不能回答我自己的問題。我認爲發佈已經找到的解決方案會很有幫助,否則人們會試圖幫助解決已經解決的問題。 – mino 2013-03-27 15:17:59

+0

我現在修好了:) – mino 2013-03-28 12:14:58

回答

3

在irc.perl.org #moose一個IRC用戶給出瞭解決方案:

<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