Moose (Perl)
   HOME

TheInfoList



OR:

Moose is an extension of the object system of the
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offic ...
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
. Its stated purpose 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 Introspection is the examination of one's own conscious thoughts and feelings. In psychology, the process of introspection relies on the observation of one's mental state, while in a spiritual context it may refer to the examination of one's sou ...
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 Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to: *Scien ...
. * A class has zero or more superclasses (a.k.a. parent classes). A class inherits from its superclass(es). Moose supports
multiple inheritance Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit features from more than one parent object or parent class. It is distinct from single inheritance, where an object or ...
. * 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 In computer science, a mutator method is a method used to control changes to a variable. They are also widely known as setter methods. Often a setter is accompanied by a getter (together also known as accessors), which returns the value of the priv ...
names, delegations, a default value and
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. It is a kind of lazy evaluation that refers specificall ...
.


Roles

Roles in Moose are based on traits. They perform a similar task as
mixin In object-oriented programming languages, a mixin (or mix-in) is a class that contains methods for use by other classes without having to be the parent class of those other classes. How those other classes gain access to the mixin's methods depen ...
s, but are composed horizontally rather than inherited. They are also somewhat like
interfaces Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * '' Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Int ...
, 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

There are a number of Moose extension modules on
CPAN The Comprehensive Perl Archive Network (CPAN) is a repository of over 250,000 software modules and accompanying documentation for 39,000 distributions, written in the Perl programming language by over 12,000 contributors. ''CPAN'' can denote ei ...
. there are 855 modules in 266 distributions in the MooseX namespace. Most of them can be optionally installed with the Task::Moose module.Task::Moose
/ref>


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 sub set_to package Point3D; use Moose; use Carp; extends 'Point'; has 'z' => (isa => 'Num', is => 'rw'); after 'clear' => sub ; sub set_to 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 class Point3D extends Point


See also

* Raku object system, is the inspiration for Moose * Joose, a
JavaScript JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of websites use JavaScript on the client side for webpage behavior, of ...
framework inspired by Moose *
Catalyst Catalysis () is the process of increasing the rate of a chemical reaction by adding a substance known as a catalyst (). Catalysts are not consumed in the reaction and remain unchanged after it. If the reaction is rapid and the catalyst recyc ...
, a web application framework using Moose


References


External links


Moose Homepage

Moose Documentation
{{Perl Perl modules Articles with example Perl code