[go: nahoru, domu]

Jump to content

Moose (Perl)

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Rfl (talk | contribs) at 11:34, 3 February 2010 (adding sections). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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.

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',
);

References

External links