Moose (Perl)
Encyclopedia
Moose is an extension of the Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

 5 object system. It brings modern object-oriented language features to Perl 5, making object-oriented programming
Object-oriented programming
Object-oriented programming is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction,...

 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
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...

:
  • A class has zero or more attributes
    Attribute (computing)
    In computing, an attribute is a specification that defines a property of an object, element, or file. It may also refer to or set the specific value for a given instance of such....

    .
  • A class has zero or more methods
    Method (computer science)
    In object-oriented programming, a method is a subroutine associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time...

    .
  • A class has zero or more superclasses (aka parent classes). A class inherits
    Inheritance (computer science)
    In object-oriented programming , inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support...

     from its superclass(es). Moose supports multiple inheritance
    Multiple inheritance
    Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....

    .
  • 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
    Constructor (computer science)
    In object-oriented programming, a constructor in a class is a special type of subroutine called at the creation of an object. It prepares the new object for use, often accepting parameters which the constructor uses to set any member variables required when the object is first created...

     and a destructor
    Destructor (computer science)
    In object-oriented programming, a destructor is a method which is automatically invoked when the object is destroyed...

    .
  • A class has a metaclass
    Metaclass
    In object-oriented programming, a metaclass is a class whose instances are classes. Just as an ordinary class defines the behavior of certain objects, a metaclass defines the behavior of certain classes and their instances. Not all object-oriented programming languages support metaclasses...

    .

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
    Mutator method
    In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation...

     names, delegations
    Delegation (programming)
    In object-oriented programming, there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement...

    , a default
    Default (computer science)
    A default, in computer science, refers to a setting or value automatically assigned to a software application, computer program or device, outside of user intervention. Such settings are also called presets, especially for electronic devices...

     value and lazy initialization
    Lazy initialization
    In computer programming, lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed....

    .

Roles

Roles in Moose are based on traits. They perform a similar task as mixin
Mixin
In object-oriented programming languages, a mixin is a class that provides a certain functionality to be inherited or just reused by a subclass, while not meant for instantiation , Mixins are synonymous functionally with abstract base classes...

s, but are composed horizontally rather than inherited. They are also somewhat like interfaces
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...

, 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.

Extensions

There are a number of Moose extension modules on CPAN
CPAN
CPAN, the Comprehensive Perl Archive Network, is an archive of nearly 100,000 modules of software written in Perl, as well as documentation for it. It has a presence on the World Wide Web at and is mirrored worldwide at more than 200 locations...

. there are 617 modules in 192 distributions in the MooseX namespace. Most of them can be optionally installed with the Task::Moose module.

Examples

This is an example of a class Point and its subclass Point3D:


package Point;
use Moose;

has 'x' => (isa => 'Num', is => 'rw');
has 'y' => (isa => 'Num', is => 'rw');

sub clear {
my $self = shift;
$self->x(0);
$self->y(0);
}

sub set_to {
@_

3 or croak "Bad number of arguments";
my $self = shift;
my ($x, $y) = @_;
$self->x($x);
$self->y($y);
}

package Point3D;
use Moose;

extends 'Point';

has 'z' => (isa => 'Num', is => 'rw');

after 'clear' => sub {
my $self = shift;
$self->z(0);
};

sub set_to {
@_

4 or croak "Bad number of arguments";
my $self = shift;
my ($x, $y, $z) = @_;
$self->x($x);
$self->y($y);
$self->z($z);
}


There is a new set_to method in the Point3D class so the method of the same name defined in the Point class is not invoked in the case of Point3D instances. The clear method on the other hand is not replaced but extended in the subclass, so both methods are run in the correct order.

This is the same using the MooseX::Declare extension:


use MooseX::Declare;

class Point {
has 'x' => (isa => 'Num', is => 'rw');
has 'y' => (isa => 'Num', is => 'rw');

method clear {
$self->x(0);
$self->y(0);
}
method set_to (Num $x, Num $y) {
$self->x($x);
$self->y($y);
}
}

class Point3D extends Point {
has 'z' => (isa => 'Num', is => 'rw');

after clear {
$self->z(0);
}
method set_to (Num $x, Num $y, Num $z) {
$self->x($x);
$self->y($y);
$self->z($z);
}
}

See also

  • Perl 6 object system which is the inspiration for Moose
  • Joose
    Joose (framework)
    Joose is an open-source self-hosting meta object system for JavaScript with support for classes, inheritance, mixins, traits and aspect oriented programming....

    , a JavaScript
    JavaScript
    JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

     framework inspired by Moose
  • Catalyst
    Catalyst (software)
    Catalyst is an open source web application framework written in Perl, that closely follows the model–view–controller architecture, and supports a number of experimental web patterns. It is written using Moose, a modern object system for Perl...

    , a web application framework using Moose

External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK