Moose is an extension of the Perl 5 object system. It brings
modern object-oriented language features to Perl 5, making object- oriented
programming more consistent and less tedious.
Classes
Moose allows a
programmer to create classes.
- A class has zero or more attributes.
- A class has zero or more methods.
- A class has zero or more superclasses.
- A class inherits from its superclass(es).
Moose supports multiple inheritance.
- A class has zero or more method modifiers. These
modifiers can apply to its own methods, methods that are inherited from
its ancestors or methods that are provided by roles.
- A class does zero or more roles (also known
as traits in other programming languages).
- A class has a constructor and
a destructor.
Attributes
- An attribute is a property of the class that defines
it.
- An attribute always has a name, and it may have a
number of other defining characteristics.
- An attribute's characteristics may include a read/write
flag, a type, accessor methods, names, delegations,
a default value and lazy initialization.
Roles
Roles in Moose are based
on traits. They perform a similar task as mixins, but are
composed horizontally rather than inherited. They are also somewhat
like interfaces, but unlike interfaces they can provide a default
implementation. Roles can be applied to individual instances as well as
Classes.
- A role has zero or more attributes.
- A role has zero or more methods.
- A role has zero or more method modifiers.
- A role has zero or more required methods.
Method Modifiers
A method
modifier is a hook that is called when a named method is called. For
example, you could say "before calling login(), call this modifier first". Modifiers come
in different flavors like "before", "after",
"around", and "augment", and you can apply more than one
modifier to a single method.
Method modifiers are
often used as an alternative to overriding a method in a parent class. They are
also used in roles as a way of modifying methods in the consuming class.
Under the hood, a method
modifier is just a plain old Perl subroutine that gets called before or after
(or around, etc.) some named method.