1

I can't override a already declared attribute in a role with MooseX::Declare.

use MooseX::Declare;

role Person {
has 'name' => (
    is => 'ro',
    isa => 'Str',
    default => 'John',
    );
}

class Me with Person {
has '+name' => (
        default => 'Michael',
    );
}

The error which is reported by executing the code:

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

This works, but its not based on roles:

class Person {
has 'name' => (
    is => 'ro',
    isa => 'Str',
    default => 'John',
    );
}

class Me extends Person {
has '+name' => (
    default => 'Michael',
    );
}

What is wrong with my code when using roles? Is there no possibility to override a attribute behaviour?

mino
  • 41
  • 5
  • 1
    Please post your own answer as an actual answer below instead of an edit to your question, and accept it. That helps other people finding it, and your question does no longer appear as unanswered. – simbabque Mar 27 '13 at 15:08
  • 2
    I will do when the timer is gone, that i cant answer my own question. I thought it would be helpfull to post already my found solution, otherwise people will try to help for a already solved problem. – mino Mar 27 '13 at 15:17

1 Answers1

3

A irc user at irc.perl.org #moose gave the solution:

<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 

So the following code is working now:

use MooseX::Declare;

role Person {
  has 'name' => (
    is => 'ro',
    isa => 'Str',
    default => 'John',
  );
}

class Me {
  with 'Person';

  has '+name' => (
    default => 'Michael',
  );
}

Thanks a lot to phaylon.

oalders
  • 5,239
  • 2
  • 23
  • 34
mino
  • 41
  • 5