| Mouse documentation | Contained in the Mouse distribution. |
Mouse::Role - The Mouse Role
This document describes Mouse version 0.93
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
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.
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.
Importing Mouse::Role will give you sugar. -traits are also supported.
Please unimport (no Mouse::Role) so that if someone calls one of the
keywords (such as has) it will break loudly instead breaking subtly.
Role support has only a few caveats:
extends keyword; it will throw an exception for now.
The same is true of the augment and inner keywords (not sure those
really make sense for roles). All other Mouse keywords will be deferred
so that they can be applied to the consuming class. | Mouse documentation | Contained in the Mouse distribution. |
package Mouse::Role; use Mouse::Exporter; # enables strict and warnings our $VERSION = '0.93'; use Carp (); use Scalar::Util (); use Mouse (); Mouse::Exporter->setup_import_methods( as_is => [qw( extends with has before after around override super augment inner requires excludes ), \&Scalar::Util::blessed, \&Carp::confess, ], ); sub extends { Carp::croak "Roles do not support 'extends'"; } sub with { Mouse::Util::apply_all_roles(scalar(caller), @_); return; } sub has { my $meta = Mouse::Meta::Role->initialize(scalar caller); my $name = shift; $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )}) if @_ % 2; # odd number of arguments for my $n(ref($name) ? @{$name} : $name){ $meta->add_attribute($n => @_); } return; } sub before { my $meta = Mouse::Meta::Role->initialize(scalar caller); my $code = pop; for my $name($meta->_collect_methods(@_)) { $meta->add_before_method_modifier($name => $code); } return; } sub after { my $meta = Mouse::Meta::Role->initialize(scalar caller); my $code = pop; for my $name($meta->_collect_methods(@_)) { $meta->add_after_method_modifier($name => $code); } return; } sub around { my $meta = Mouse::Meta::Role->initialize(scalar caller); my $code = pop; for my $name($meta->_collect_methods(@_)) { $meta->add_around_method_modifier($name => $code); } return; } sub super { return if !defined $Mouse::SUPER_BODY; $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS); } sub override { # my($name, $code) = @_; Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_); return; } # We keep the same errors messages as Moose::Role emits, here. sub inner { Carp::croak "Roles cannot support 'inner'"; } sub augment { Carp::croak "Roles cannot support 'augment'"; } sub requires { my $meta = Mouse::Meta::Role->initialize(scalar caller); $meta->throw_error("Must specify at least one method") unless @_; $meta->add_required_methods(@_); return; } sub excludes { Mouse::Util::not_supported(); } sub init_meta{ shift; my %args = @_; my $class = $args{for_class} or Carp::confess("Cannot call init_meta without specifying a for_class"); my $metaclass = $args{metaclass} || 'Mouse::Meta::Role'; my $meta = $metaclass->initialize($class); $meta->add_method(meta => sub{ $metaclass->initialize(ref($_[0]) || $_[0]); }); # make a role type for each Mouse role Mouse::Util::TypeConstraints::role_type($class) unless Mouse::Util::TypeConstraints::find_type_constraint($class); return $meta; } 1; __END__