Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Moose composing role with required attribute into existing instance

by tj_thompson (Scribe)
on Aug 03, 2012 at 20:02 UTC ( #985340=note: print w/ replies, xml ) Need Help??


in reply to Re: Moose composing role with required attribute into existing instance
in thread Moose composing role with required attribute into existing instance

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.


Comment on Re^2: Moose composing role with required attribute into existing instance
Select or Download Code
Re^3: Moose composing role with required attribute into existing instance
by jandrew (Hermit) on Aug 03, 2012 at 20:33 UTC

    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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985340]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2014-04-19 11:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (480 votes), past polls