Moose (Perl)
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.
Features
Moose is built on top of Class::MOP, a metaobject protocol (aka MOP). Using the MOP, Moose provides complete introspection for all Moose-using classes.
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 (aka parent classes). 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.
- A class has a metaclass.
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 method names, delegations, a default value and lazy initialization.
Roles
A role is something that a class does. It is somewhat like mixins or interfaces in other object-oriented programming languages. Unlike mixins and interfaces, roles can be applied to individual instances.
- 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.
Extensions
There is a number of Moose extension modules on CPAN. Righ now (February 3, 2010) there are 490 modules in 152 distributions in the MooseX namespace.[1]
Examples
package Person;
use Moose;
# now it's a Moose class!
has 'first_name' => (
is => 'rw',
isa => 'Str',
);