Moose is an extension of the object system of the
Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
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 l ...
. 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 Consciousness, conscious thoughts and feelings. In psychology, the process of introspection relies on the observation of one's Mental representation, mental state, while in a Spirituality, spiritual c ...
for all Moose-using classes.
Classes
Moose allows a programmer to create
classes:
* A class has zero or more
attributes
Attribute may refer to:
* Attribute (philosophy), an extrinsic property of an object
* Attribute (research), a characteristic of an object
* Grammatical modifier, in natural languages
* Attribute (computing), a specification that defines a pro ...
.
* 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
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 o ...
.
* 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 may refer to:
Science and technology
* Constructor (object-oriented programming), object-organizing method
* Constructors (Formula One), person or group who builds the chassis of a car in auto racing, especially Formula One
* Construc ...
and a
destructor.
* A class has a
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-orient ...
.
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
Default may refer to:
Law
* Default (law), the failure to do something required by law
** Default (finance), failure to satisfy the terms of a loan obligation or failure to pay back a loan
** Default judgment, a binding judgment in favor of ei ...
value and
lazy initialization.
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, 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 eith ...
. 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
Joose (phonetically pronounced like "juice") is a flavored malt beverage introduced by United Brands Co. in 2005. Joose is the first premium malt beverage at 9.9% ABV in a 24 oz can. It is also sold at 12% ABV and 14% ABV in a 23.5 oz can.
Pro ...
, 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 Website, websites use JavaScript on the Client (computing), client side ...
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