http://www.perlmonks.org?node_id=985309


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
    I think the arguments to with_traits might be wrong though. I had to modify this:
    my $point = with_traits( 'MyClass', ( 'Point', 'Named' ), )->new( x => 1, y => 1, name => 'my_point', );#To satisfy the required values
    To this:
    my $point = with_traits( 'Point', 'Named', )->new( x => 1, y => 1, name => 'my_point', );#To satisfy the required values
    The first argument should be the base class you are composing roles into and all remaining arguments should be roles. I wouldn't have bothered correcting this here but maybe it will help someone else. Excellent solution.

      You are correct the syntax for the code you posted needs the class followed by roles. I initially misread Point as a Role. I tend to put all of the appended roles in a list form though just to improve readability (even if there is just one). Also if you don't like the method call on a method result you can separate them into two and the required=>1, elements of the attributes still won't error.

      my $named_point_class = with_traits( 'Point', ( 'Named', ), ); my $point_instance = $named_point_class->new( x => 1, y => 1, name => 'my_point', );
      Update:If you are using this in boilerplate the role can be a dummy name as well and it still works!
Re^2: Moose composing role with required attribute into existing instance
by tj_thompson (Monk) on Aug 03, 2012 at 19:55 UTC
    The first solution is good. I create the instance at the same time that I apply my selected roles, so this works nicely. Syntax is a little ugly, but I've seen worse :) Thank you Jandrew for the help!