Moose (Perl)

Last updated

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.

Contents

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:

Attributes

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

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.

Extensions

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

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

packagePoint; useMoose; useCarp;  has'x' => (isa => 'Num', is => 'rw'); has'y' => (isa => 'Num', is => 'rw');  subclear {     my$self = shift;     $self->x(0);     $self->y(0); }  subset_to {     @_ == 3orcroak"Bad number of arguments";     my$self = shift;     my ($x, $y) = @_;     $self->x($x);     $self->y($y); }  packagePoint3D; useMoose; useCarp;  extends'Point';  has'z' => (isa => 'Num', is => 'rw');  after'clear' => sub {     my$self = shift;     $self->z(0); };  subset_to {     @_ == 4orcroak"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:

useMooseX::Declare;  classPoint {     has'x' => (isa => 'Num', is => 'rw');     has'y' => (isa => 'Num', is => 'rw');          methodclear {         $self->x(0);         $self->y(0);     }     methodset_to (Num$x, Num$y) {         $self->x($x);         $self->y($y);     } }  classPoint3DextendsPoint {     has'z' => (isa => 'Num', is => 'rw');      afterclear {         $self->z(0);     }     methodset_to (Num$x, Num$y, Num$z) {         $self->x($x);         $self->y($y);         $self->z($z);     } } 

See also

Related Research Articles

Lua (programming language) Lightweight programming language

Lua is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded use in applications. Lua is cross-platform, since the interpreter of compiled bytecode is written in ANSI C, and Lua has a relatively simple C API to embed it into applications.

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created. This is in contrast to a mutable object, which can be modified after it is created. In some cases, an object is considered immutable even if some internally used attributes change, but the object's state appears unchanging from an external point of view. For example, an object that uses memoization to cache the results of expensive computations could still be considered an immutable object.

In computing, the Perl DBI offers a standardized way for programmers using the Perl programming language to embed database communication within their programs. The latest DBI module for Perl from CPAN can run on a range of operating systems.

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.

In computer programming, a function object is a construct allowing an object to be invoked or called as if it were an ordinary function, usually with the same syntax. Function objects are often called functors.

A Perl module is a discrete component of software for the Perl programming language. Technically, it is a particular set of conventions for using Perl's package mechanism that has become universally adopted.

Perl Data Language is a set of free software array programming extensions to the Perl programming language. PDL extends the data structures built into Perl, to include large multidimensional arrays, and adds functionality to manipulate those arrays as vector objects. It also provides tools for image processing, machine learning, computer modeling of physical systems, and graphical plotting and presentation. Simple operations are automatically vectorized across complete arrays, and higher-dimensional operations are supported.

In class-based object-oriented programming, a constructor is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

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.

Raku (programming language) Programming language derived from Perl

Raku is a member of the Perl family of programming languages. Formerly known as Perl 6, it was renamed in October 2019. Raku introduces elements of many modern and historical languages. Compatibility with Perl was not a goal, though a compatibility mode is part of the specification. The design process for Raku began in 2000.

Catalyst (software)

Catalyst is an open source web application framework written in Perl, that closely follows the model–view–controller (MVC) architecture, and supports a number of experimental web patterns. It is written using Moose, a modern object system for Perl. Its design is heavily inspired by such frameworks as Ruby on Rails, Maypole, and Spring.

String functions are used in computer programming languages to manipulate a string or query information about a string.

Glossary of Unified Modeling Language (UML) terms provides a compilation of terminology used in all versions of UML, along with their definitions. Any notable distinctions that may exist between versions are noted with the individual entry it applies to.

The syntax of the Python programming language is the set of rules that defines how a Python program will be written and interpreted. The Python language has many similarities to Perl, C, and Java. However, there are some definite differences between the languages.

v6 is a module for the Perl programming language which runs under Perl version 5, and transforms Raku code into Perl 5 code on the fly. To quote the release notes:

This article describes the syntax of the C# programming language. The features described are compatible with .NET Framework and Mono.

Joose (framework)

Joose is an open-source self-hosting metaobject system for JavaScript with support for classes, inheritance, mixins, traits and aspect-oriented programming.

Dancer is an open source lightweight web application framework written in Perl and inspired by Ruby's Sinatra.

The structure of the Perl programming language encompasses both the syntactical rules of the language and the general ways in which programs are organized. Perl's design philosophy is expressed in the commonly cited motto "there's more than one way to do it". As a multi-paradigm, dynamically typed language, Perl allows a great degree of flexibility in program design. Perl also encourages modularization; this has been attributed to the component-based design structure of its Unix roots, and is responsible for the size of the CPAN archive, a community-maintained repository of more than 100,000 modules.

Objective-C is a general-purpose, object-oriented programming language that adds Smalltalk-style messaging to the C programming language. Originally developed by Brad Cox and Tom Love in the early 1980s, it was selected by NeXT for its NeXTSTEP operating system. Objective-C was the standard programming language supported by Apple for developing macOS and iOS applications using their respective application programming interfaces (APIs), Cocoa and Cocoa Touch, until the introduction of Swift in 2014.

References

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