Skip Menu |

This queue is for tickets about the Moose CPAN distribution.

Report information
The Basics
Id: 55369
Status: rejected
Priority: 0/
Queue: Moose

People
Owner: Nobody in particular
Requestors: sjq-perl [...] jadevine.org.uk
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 0.98
Fixed in: (no value)



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
Stephen, The "+attr" option only works in classes, this should probably be more explicitly documented (patches welcome). This is because it is really only meant for two possible situations; 1) Adding to or changing attributes inherited from superclasses. 2) Adding or changing details of attributes consumed from roles. To be honest, the second case was an accidental "feature" that too many people started to use and so we made official. - Stevan On Tue Mar 09 07:07:08 2010, SJQUINNEY wrote: Show quoted text
> 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(0x9b8a7e 4)') Show quoted text
> 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::ToC lass=HASH(0x9f355ec)', Show quoted text
> '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(0x9f355 ec)', Show quoted text
> '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)', Show quoted text
> '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