请输入您要查询的百科知识:

 

词条 Moose (Perl)
释义

  1. Features

     Classes  Attributes  Roles 

  2. Extensions

  3. Examples

  4. See also

  5. References

  6. External links

{{more footnotes|date=May 2010}}

Moose is an extension of the object system of the Perl programming language. Its stated purpose[1] is to bring modern object-oriented language features to Perl 5, and to make object-oriented Perl programming more consistent and less tedious.

Features

Moose is built on top of Class::MOP, a metaobject protocol (a.k.a. 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 (a.k.a. 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

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|2012|09}} there are 855 modules in 266 distributions in the MooseX namespace.[2] Most of them can be optionally installed with the Task::Moose module.[3]

Examples

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

package Point;

use Moose;

use Carp;

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;

use Carp;

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 {

    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, a JavaScript framework inspired by Moose
  • Catalyst, a web application framework using Moose

References

1. ^{{cite web | url=http://moose.iinteractive.com/en/about.html |title=Moose - A postmodern object system for Perl |access-date = 2017-03-06}}
2. ^[https://metacpan.org/search?q=MooseX Moose extensions on CPAN]
3. ^Task::Moose

External links

  • Moose Homepage
  • Moose Documentation
{{Perl}}

2 : Perl modules|Articles with example Perl code

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/13 9:07:30