[go: nahoru, domu]

Jump to content

Moose (Perl): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Rfl (talk | contribs)
→‎Examples: more complex example
m →‎Examples: lang="perl"
 
(39 intermediate revisions by 30 users not shown)
Line 1: Line 1:
{{more footnotes|date=May 2010}}
'''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.
'''Moose''' is an extension of the object system of the [[Perl]] [[programming language]]. Its stated purpose<ref>{{cite web | url=http://moose.iinteractive.com/en/about.html |title=Moose - A postmodern object system for Perl |access-date = 2017-03-06}}</ref> is to bring modern object-oriented language features to Perl 5, and to make [[object-oriented]] Perl programming more consistent and less tedious.


==Features==
==Features==
Moose is built on top of Class::MOP, a [[metaobject protocol]] ({{aka}} MOP). Using the MOP, Moose provides complete [[type introspection|introspection]] for all Moose-using classes.

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===
===Classes===
Moose allows a programmer to create [[class (computer programming)|class]]es:

* A class has zero or more [[attribute (computing)|attribute]]s.
Moose allows a programmer to create [[Class (computer science)|classes]]:
* A class has zero or more [[method (computer programming)|method]]s.

* A class has zero or more [[superclass (computer science)|superclass]]es (a.k.a. parent classes). A class [[Inheritance (object-oriented programming)|inherits]] from its superclass(es). Moose supports [[multiple inheritance]].
* A class has zero or more [[Attribute (computing)|attributes]].
* A class has zero or more [[Method (computer science)|methods]].
* A class has zero or more [[Superclass (computer science)|superclasses]] (aka parent classes). A class [[Inheritance (computer science)|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 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 [[Trait (abstract type)|traits]] in other programming languages).
* A class does zero or more roles (also known as [[trait (computer programming)|trait]]s in other programming languages).
* A class has a [[Constructor (computer science)|constructor]] and a [[Destructor (computer science)|destructor]].
* A class has a [[constructor (object-oriented programming)|constructor]] and a [[destructor (computer programming)|destructor]].
* A class has a [[metaclass]].
* A class has a [[metaclass]].


===Attributes===
===Attributes===

An attribute is a property of the class that defines it.
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 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, [[Mutator method|accessor method]] names, [[Delegation (programming)|delegations]], a [[Default (computer science)|default]] value and [[lazy initialization]].
* An attribute's characteristics may include a read/write flag, a type, [[accessor method]] names, [[delegation (object-oriented programming)|delegation]]s, a [[default (computer science)|default]] value and [[lazy initialization]].


===Roles===
===Roles===
Roles in Moose are based on [[trait (computer programming)|trait]]s. They perform a similar task as [[mixin]]s, but are composed horizontally rather than inherited. They are also somewhat like [[interface (object-oriented programming)|interface]]s, but unlike some implementations of interfaces they can provide a default implementation. Roles can be applied to individual instances as well as Classes.

A role is something that a class does. It is somewhat like [[mixin]]s or [[Interface (computer science)|interfaces]] in other [[object-oriented programming language]]s. Unlike mixins and interfaces, roles can be applied to individual instances.

* A role has zero or more attributes.
* A role has zero or more attributes.
* A role has zero or more methods.
* A role has zero or more methods.
Line 34: Line 28:


==Extensions==
==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.<ref>[https://metacpan.org/search?q=MooseX Moose extensions on CPAN]</ref> Most of them can be optionally installed with the Task::Moose module.<ref>[http://metacpan.org/module/Task::Moose Task::Moose]</ref>

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.<ref>[http://search.cpan.org/search?query=MooseX&mode=dist Moose extensions on CPAN]</ref> Most of them can be optionally installed with the Task::Moose module.<ref>[http://search.cpan.org/perldoc?Task%3A%3AMoose Task::Moose]</ref>


==Examples==
==Examples==
This is an example of a class <code>Point</code> and its subclass <code>Point3D</code>:


<syntaxhighlight lang="perl">
This is an example of a class Point and its subclass Point3D:

<source lang="perl">
package Point;
package Point;
use Moose;
use Moose;
use Carp;

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

sub clear {
sub clear {
my $self = shift;
my $self = shift;
$self->x(0);
$self->x(0);
$self->y(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;
package Point3D;
use Moose;
use Moose;
use Carp;


extends 'Point';
extends 'Point';

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

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


sub set_to {
==References==
@_ == 4 or croak "Bad number of arguments";
my $self = shift;
my ($x, $y, $z) = @_;
$self->x($x);
$self->y($y);
$self->z($z);
}
</syntaxhighlight>


There is a new <code>set_to()</code> method in the <code>Point3D</code> class so the method of the same name defined in the <code>Point</code> class is not invoked in the case of <code>Point3D</code> instances. The <code>clear()</code> 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 <code>MooseX::Declare</code> extension:

<syntaxhighlight lang="perl">
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);
}
}
</syntaxhighlight>

==See also==
* {{slink|Raku (programming language)|Object-oriented programming}}, the inspiration for Moose
* [[Joose (framework)]], a [[JavaScript]] framework inspired by Moose
* [[Catalyst (software)]], a web application framework using Moose

==References==
{{reflist}}
{{reflist}}


==External links==
==External links==
* [http://moose.iinteractive.com/ Moose Homepage]
* [http://metacpan.org/module/Moose Moose Documentation]


{{Perl}}
* [http://moose.perl.org/ Moose Homepage]
* [http://search.cpan.org/perldoc?Moose Moose Documentation]


[[Category:Perl modules]]
[[Category:Perl modules]]
[[Category:Articles with example Perl code]]

{{compu-lang-stub}}

Latest revision as of 05:33, 18 June 2024

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[edit]

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[edit]

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[edit]

An attribute is a property of the class that defines it.

Roles[edit]

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 some implementations of 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[edit]

There are a number of Moose extension modules on CPAN. As of September 2012 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[edit]

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 {
    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[edit]

References[edit]

  1. ^ "Moose - A postmodern object system for Perl". Retrieved 2017-03-06.
  2. ^ Moose extensions on CPAN
  3. ^ Task::Moose

External links[edit]