Moose (Perl)
   HOME

TheInfoList



OR:

Moose is an extension of the
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an a ...
system of the
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
. Its stated purpose is to bring modern
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of '' objects''. Objects can contain data (called fields, attributes or properties) and have actions they can perform (called procedures or methods and impl ...
language features to Perl 5, and to make object-oriented Perl programming more consistent and less tedious.


Features

Moose is built on Class::MOP, a metaobject protocol (MOP). Using the MOP, Moose provides complete
type introspection In computing, type introspection is the ability of a program to ''examine'' the type or properties of an object at runtime. Some programming languages possess this capability. Introspection should not be confused with reflection, which goes a ...
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 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 ...
. * 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 In object-oriented programming, a metaclass is a Class (computer science), class whose Instance (computer programming), instances are classes themselves. Unlike ordinary classes, which define the behaviors of objects, metaclasses specify the beha ...
.


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, which returns the value of the private member variable. They are also ...
names,
delegation Delegation is the process of distributing and entrusting work to another person.Schermerhorn, J., Davidson, P., Poole, D., Woods, P., Simon, A., & McBarron, E. (2017). ''Management'' (6th ed., pp. 282–286). Brisbane: John Wiley & Sons Australia. ...
s, 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 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 software repository of over 220,000 software modules and accompanying documentation for 45,500 distributions, written in the Perl programming language by over 14,500 contributors. ''CPAN'' can de ...
. 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

* , the inspiration for Moose * Joose (framework), a
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
framework inspired by Moose * Catalyst (software), a web application framework using Moose


References


External links


Moose Homepage

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