Subject: | Modifying an attribute in a role? |
I'm not entirely sure if this is a bug or feature but I can't find a
clear example which says it should work and also no clear note saying
"don't do that". Really I'm seeking clarification rather than trying to
report a bug.
I have two roles - RoleA and RoleB. RoleB uses RoleA and attempts to add
a default value to an attribute specified in the first role. I then have
a Test class which uses RoleB:
package SJQ::RoleA;
use Moose::Role;
requires 'message';
has 'text' => (
is => 'ro',
isa => 'Str',
default => "wibble",
);
1;
package SJQ::RoleB;
use Moose::Role;
with 'SJQ::RoleA';
has '+foo' => (
default => 'Hello World',
);
1;
package SJQ::Test;
use Moose;
with 'SJQ::RoleB';
sub message {
my $self = shift;
print $self->text . "\n";
}
1;
and here is the output I get:
Attribute (+foo) of class SJQ::Test has no associated methods (did you
mean to provide an "is" argument?)
at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Meta/Attribute.pm
line 551
Moose::Meta::Attribute::_check_associated_methods('Moose::Meta::Attribute=HASH(0x9b8a7e4)')
called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Meta/Class.pm
line 303
Moose::Meta::Class::add_attribute('Moose::Meta::Class=HASH(0x9ea2bc8)',
'Moose::Meta::Attribute=HASH(0x9b8a7e4)') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Meta/Role/Application/ToClass.pm
line 142
Moose::Meta::Role::Application::ToClass::apply_attributes('Moose::Meta::Role::Application::ToClass=HASH(0x9f355ec)',
'Moose::Meta::Role=HASH(0x9f46bb0)',
'Moose::Meta::Class=HASH(0x9ea2bc8)') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Meta/Role/Application.pm
line 72
Moose::Meta::Role::Application::apply('Moose::Meta::Role::Application::ToClass=HASH(0x9f355ec)',
'Moose::Meta::Role=HASH(0x9f46bb0)',
'Moose::Meta::Class=HASH(0x9ea2bc8)') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Meta/Role/Application/ToClass.pm
line 31
Moose::Meta::Role::Application::ToClass::apply('Moose::Meta::Role::Application::ToClass=HASH(0x9f355ec)',
'Moose::Meta::Role=HASH(0x9f46bb0)',
'Moose::Meta::Class=HASH(0x9ea2bc8)') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Meta/Role.pm
line 419
Moose::Meta::Role::apply('Moose::Meta::Role=HASH(0x9f46bb0)',
'Moose::Meta::Class=HASH(0x9ea2bc8)') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Util.pm
line 132
Moose::Util::_apply_all_roles('Moose::Meta::Class=HASH(0x9ea2bc8)',
'undef', 'SJQ::RoleB') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Util.pm
line 86
Moose::Util::apply_all_roles('Moose::Meta::Class=HASH(0x9ea2bc8)',
'SJQ::RoleB') called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose.pm line 57
Moose::with('Moose::Meta::Class=HASH(0x9ea2bc8)', 'SJQ::RoleB')
called at
/usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi/Moose/Exporter.pm
line 293
Moose::with('SJQ::RoleB') called at
/home/squinney/perllibs/SJQ/Test.pm line 5
require SJQ/Test.pm called at
/afs/inf.ed.ac.uk/user/s/squinney/test.pl line 5
main::BEGIN() called at /home/squinney/perllibs/SJQ/Test.pm line 0
eval {...} called at /home/squinney/perllibs/SJQ/Test.pm line 0
wibble