Roles
Explore how roles in Perl, implemented via Moose, help organize named collections of behavior and state. Learn to define roles with required methods, compose roles into classes, and employ allomorphism to write efficient and maintainable object-oriented code.
We'll cover the following...
A role is a named collection of behavior and state. Whereas a class organizes behaviors and state into a template for objects, a role organizes a named collection of behaviors and state. We can instantiate a class but not a role. A role is something a class does.
Defining a role
Given an Animal that has an age and a Cheese that can age, one difference may be that Animal does the LivingBeing role, while Cheese does the Storable role:
The requires keyword provided by Moose::Role allows us to list methods that this role requires of its composing classes. ...