2 use Mouse::Exporter; # enables strict and warnings
11 Mouse::Exporter->setup_import_methods(
21 \&Scalar::Util::blessed,
28 Carp::croak "Roles do not support 'extends'";
32 Mouse::Util::apply_all_roles(scalar(caller), @_);
37 my $meta = Mouse::Meta::Role->initialize(scalar caller);
40 $meta->throw_error(q{Usage: has 'name' => ( key => value, ... )})
41 if @_ % 2; # odd number of arguments
43 for my $n(ref($name) ? @{$name} : $name){
44 $meta->add_attribute($n => @_);
50 my $meta = Mouse::Meta::Role->initialize(scalar caller);
52 for my $name($meta->_collect_methods(@_)) {
53 $meta->add_before_method_modifier($name => $code);
59 my $meta = Mouse::Meta::Role->initialize(scalar caller);
61 for my $name($meta->_collect_methods(@_)) {
62 $meta->add_after_method_modifier($name => $code);
68 my $meta = Mouse::Meta::Role->initialize(scalar caller);
70 for my $name($meta->_collect_methods(@_)) {
71 $meta->add_around_method_modifier($name => $code);
78 return if !defined $Mouse::SUPER_BODY;
79 $Mouse::SUPER_BODY->(@Mouse::SUPER_ARGS);
83 # my($name, $code) = @_;
84 Mouse::Meta::Role->initialize(scalar caller)->add_override_method_modifier(@_);
88 # We keep the same errors messages as Moose::Role emits, here.
90 Carp::croak "Roles cannot support 'inner'";
94 Carp::croak "Roles cannot support 'augment'";
98 my $meta = Mouse::Meta::Role->initialize(scalar caller);
99 $meta->throw_error("Must specify at least one method") unless @_;
100 $meta->add_required_methods(@_);
105 Mouse::Util::not_supported();
112 my $class = $args{for_class}
113 or Carp::confess("Cannot call init_meta without specifying a for_class");
115 my $metaclass = $args{metaclass} || 'Mouse::Meta::Role';
117 my $meta = $metaclass->initialize($class);
119 $meta->add_method(meta => sub{
120 $metaclass->initialize(ref($_[0]) || $_[0]);
123 # make a role type for each Mouse role
124 Mouse::Util::TypeConstraints::role_type($class)
125 unless Mouse::Util::TypeConstraints::find_type_constraint($class);
136 Mouse::Role - The Mouse Role
140 This document describes Mouse version 0.93
145 use Mouse::Role; # the package is now a Mouse role
147 # Declare methods that are required by this role
148 requires qw(compare);
150 # Define methods this role provides
152 my($self, $other) = @_;
153 return $self->compare($other) == 0;
159 with qw(Comparable); # Now MyObject can equals()
165 my $foo = MyObject->new();
166 my $bar = MyObject->new();
167 $obj->equals($bar); # yes, it is comparable
171 This module declares the caller class to be a Mouse role.
173 The concept of roles is documented in L<Moose::Manual::Roles>.
174 This document serves as API documentation.
176 =head1 EXPORTED FUNCTIONS
178 Mouse::Role supports all of the functions that Mouse exports, but
179 differs slightly in how some items are handled (see L</CAVEATS> below
182 Mouse::Role also offers two role-specific keywords:
184 =head2 C<< requires(@method_names) >>
186 Roles can require that certain methods are implemented by any class which
189 Note that attribute accessors also count as methods for the purposes of
190 satisfying the requirements of a role.
192 =head2 C<< excludes(@role_names) >>
194 This is exported but not implemented in Mouse.
196 =head1 IMPORT AND UNIMPORT
200 Importing Mouse::Role will give you sugar. C<-traits> are also supported.
204 Please unimport (C<< no Mouse::Role >>) so that if someone calls one of the
205 keywords (such as L</has>) it will break loudly instead breaking subtly.
209 Role support has only a few caveats:
215 Roles cannot use the C<extends> keyword; it will throw an exception for now.
216 The same is true of the C<augment> and C<inner> keywords (not sure those
217 really make sense for roles). All other Mouse keywords will be I<deferred>
218 so that they can be applied to the consuming class.
222 Role composition does its best to B<not> be order-sensitive when it comes to
223 conflict resolution and requirements detection. However, it is order-sensitive
224 when it comes to method modifiers. All before/around/after modifiers are
225 included whenever a role is composed into a class, and then applied in the order
226 in which the roles are used. This also means that there is no conflict for
227 before/around/after modifiers.
229 In most cases, this will be a non-issue; however, it is something to keep in
230 mind when using method modifiers in a role. You should never assume any
241 L<Moose::Manual::Roles>