藤 吾郎 > Mouse-1.04 > Mouse::Role

Download:
Mouse-1.04.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  6
Open  9
View/Report Bugs
Module Version: 1.04   Source   Latest Release: Mouse-2.4.1

NAME ^

Mouse::Role - The Mouse Role

VERSION ^

This document describes Mouse version 1.04

SYNOPSIS ^

    package Comparable;
    use Mouse::Role; # the package is now a Mouse role

    # Declare methods that are required by this role
    requires qw(compare);

    # Define methods this role provides
    sub equals {
        my($self, $other) = @_;
        return $self->compare($other) == 0;
    }

    # and later
    package MyObject;
    use Mouse;
    with qw(Comparable); # Now MyObject can equals()

    sub compare {
        # ...
    }

    my $foo = MyObject->new();
    my $bar = MyObject->new();
    $obj->equals($bar); # yes, it is comparable

DESCRIPTION ^

This module declares the caller class to be a Mouse role.

The concept of roles is documented in Moose::Manual::Roles. This document serves as API documentation.

EXPORTED FUNCTIONS ^

Mouse::Role supports all of the functions that Mouse exports, but differs slightly in how some items are handled (see "CAVEATS" below for details).

Mouse::Role also offers two role-specific keywords:

requires(@method_names)

Roles can require that certain methods are implemented by any class which does the role.

Note that attribute accessors also count as methods for the purposes of satisfying the requirements of a role.

excludes(@role_names)

This is exported but not implemented in Mouse.

IMPORT AND UNIMPORT ^

import

Importing Mouse::Role will give you sugar. -traits are also supported.

unimport

Please unimport (no Mouse::Role) so that if someone calls one of the keywords (such as "has") it will break loudly instead breaking subtly.

CAVEATS ^

Role support has only a few caveats:

SEE ALSO ^

Mouse

Moose::Role

Moose::Manual::Roles

Moose::Spec::Role

syntax highlighting: