Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Features  



1.1  Classes  





1.2  Attributes  





1.3  Roles  







2 Extensions  





3 Examples  





4 See also  





5 References  





6 External links  














Moose (Perl)






Русский
 

Edit links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 




In other projects  



Wikibooks
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


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:

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.

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.
  • ^ Moose extensions on CPAN
  • ^ Task::Moose
  • External links[edit]


    Retrieved from "https://en.wikipedia.org/w/index.php?title=Moose_(Perl)&oldid=1229689087"

    Category: 
    Perl modules
    Hidden categories: 
    Articles lacking in-text citations from May 2010
    All articles lacking in-text citations
    Articles containing potentially dated statements from September 2012
    All articles containing potentially dated statements
    Articles with example Perl code
     



    This page was last edited on 18 June 2024, at 05:33 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki