I'm using HTML::FormHandler, and in an effort to not repeat a lot of code I am trying to split my form up into reuasable pieces as suggested by the HTML::FormHandler Cookbook. It discusses being able to create your own fields or your own roles to accomplish this, however I cannot really a clearcut difference between the two. I understand that they are different in how you implement/use them, but as far as I can tell they seem to pretty similarly accomplish the same things. My question is are there any situations where it is a clear choice to use one over the other? Or is this something that tends to be more a preference kind of thing? Thanks for the help. I'd like to do this right before the code is in production and it's harder to change later :)
Asked
Active
Viewed 108 times
1 Answers
0
One option is to use Moose classes the other is to use Moose roles.
The concepts are slightly different. Here is a snippet from the Moose Roles manual:
A role encapsulates some piece of behavior or state that can be shared between classes. It is something that classes do. It is important to understand that roles are not classes. You cannot inherit from a role, and a role cannot be instantiated. We sometimes say that roles are consumed, either by classes or other roles.
Classes let you use inheritance, roles are good when you want to share functionality without inheriting.
For a (contrived) example of when you would/would not want to use inheritance, see this article on Roles vs Inheritance

nick
- 1,369
- 2
- 13
- 28