|XP is just a number|
Moose composing role with required attribute into existing instanceby tj_thompson (Monk)
|on Aug 03, 2012 at 18:10 UTC||Need Help??|
tj_thompson has asked for the
wisdom of the Perl Monks concerning the following question:
I'm back with another Moose question. Fortunately, this one's easy to lay out.
I have the following code:
This code fails with the following:
The problem is that the 'name' attribute in the Named role is required. However, I've already created my Point instance before I compose the Named role into this. This problem goes away if you remove the required parameter from the 'name' attribute in the Named role.
Obviously, there are many workarounds to this issue. Disabling the required parameter and initializing the attribute after role application is one. Ensuring the attribute is already defined by the instance is another. Composing the role into the class prior to instantiation is a third.
I believe having to remove the required parameter from the attribute and relying on the programmer to remember to properly initialize the attribute is the wrong way to do this. Moose provides the required parameter explicitly to ensure that a given attribute is properly initialized. Ensuring the instance already has an attribute that satisfies the requirement seems to be a convoluted way to solve the issue, definitely worse than the first workaround. Composing the role into the class prior to instantiation limits flexibility as I am trying to apply differing roles to differing instances.
So, the summary: Is there a good way to compose a role with a required attribute into an instance other than what I've outlined above?
Thanks in advance for taking the time to read my question, it is always appreciated!