in reply to Moose composing role with required attribute into existing instance
Two possibilities occur to me.
One, compose the class and role together on the fly when you build the instance (instead of adding it to the instance after creation) so that when you need it you have it and when you don't you don't have to use it. This will allow your attribute to retain the required=>1 status. (This is my preference.)
use Modern::Perl; use Moose::Util qw( with_traits ); my $point = with_traits( 'MyClass', ( 'Point', 'Named' ), )->new( x => 1, y => 1, name => 'my_point', );#To satisfy the required values
If that doesn't seem to be a good fit then just add a default value and a setter (safer than 'rw') to your 'name' attribute. This will eliminate the error. You can then use your setter to change the value to the correct one when and where you want.
has 'name' => ( isa => 'Str', is => 'ro',# Note the change required => 1, default => 'change_me', writer => 'set_name', );
TIMTOWTDI applies to Moose too!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Moose composing role with required attribute into existing instance
by tj_thompson (Monk) on Aug 03, 2012 at 20:02 UTC | |
by jandrew (Chaplain) on Aug 03, 2012 at 20:33 UTC | |
Re^2: Moose composing role with required attribute into existing instance
by tj_thompson (Monk) on Aug 03, 2012 at 19:55 UTC |
In Section
Seekers of Perl Wisdom