Moose (Perl)

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.

Contents

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

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.

Extensions

There are a number of Moose extension modules on CPAN. As of April 2011 there are 617 modules in 192 distributions in the MooseX namespace.[1] Most of them can be optionally installed with the Task::Moose module.[2]

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

References

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Moose (disambiguation) — A moose is the largest member of the deer family (Alces alces) Moose may also refer to: Contents 1 People 2 Places 3 Organizations …   Wikipedia

  • Perl — Семантика: мультипарадигменный: императивный, объектно ориентированный, функциональный Тип исполнения: интерпретатор Появился в: 1987 Автор(ы) …   Википедия

  • Perl 6 — Saltar a navegación, búsqueda Perl 6 Paradigma: multiparadigma Apareció en: 2000 Diseñado por: Larry Wall y la comunidad Perl Tipo de dato: Dinámico y estático Implementaciones …   Wikipedia Español

  • Perl 6 — Класс языка: Мультипарадигмальный Появился в: 2000 …   Википедия

  • Perl — This article is about the programming language. For other uses, see Perl (disambiguation). Perl Paradig …   Wikipedia

  • Perl 6 — Infobox programming language name = Perl paradigm = Multi paradigm year = 2000 designer = Larry Wall latest release version = pre release latest release date = typing = dynamic, static influenced by = Perl 5, Haskell, Smalltalk influenced =… …   Wikipedia

  • Perl module — A Perl module is a discrete component of software for the Perl programming language. Technically, it is a particular set of conventions for using Perl s package mechanism that has become universally adopted.[discuss] A module defines its source… …   Wikipedia

  • V6 (Perl) — v6 is a Perl module which runs under Perl version 5, and transforms Perl 6 code into Perl 5 code on the fly. To quote the release notes:: In summary: Perl 5 is now a first class virtual machine for Pugs, and in this journey toward self hosting,… …   Wikipedia

  • Mason (Perl) — Mason Stable release 2.14 / September 7, 2011; 2 months ago (2011 09 07) Written in Perl Operating system Cross platform …   Wikipedia

  • Metaprogramming — This article is about the computer programming technique. For the management technique, see Metaprogramming (management). Programming paradigms Agent oriented Automata based Component based …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”