5 use Scalar::Util 'blessed';
14 use Moose::Meta::Role;
15 use Moose::Util::TypeConstraints;
18 croak "Roles do not support 'extends' (you can use 'with' to specialize a role)";
22 Moose::Util::apply_all_roles( shift, @_ );
27 croak "Must specify at least one method" unless @_;
28 $meta->add_required_methods(@_);
33 croak "Must specify at least one role" unless @_;
34 $meta->add_excluded_roles(@_);
40 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
41 my %options = ( definition_context => Moose::Util::_caller_info(), @_ );
42 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
43 $meta->add_attribute( $_, %options ) for @$attrs;
46 sub _add_method_modifier {
50 if ( ref($_[0]) eq 'Regexp' ) {
51 croak "Roles do not currently support regex "
52 . " references for $type method modifiers";
55 Moose::Util::add_method_modifier($meta, $type, \@_);
58 sub before { _add_method_modifier('before', @_) }
60 sub after { _add_method_modifier('after', @_) }
62 sub around { _add_method_modifier('around', @_) }
64 # see Moose.pm for discussion
66 return unless $Moose::SUPER_BODY;
67 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
72 my ( $name, $code ) = @_;
73 $meta->add_override_method_modifier( $name, $code );
77 croak "Roles cannot support 'inner'";
81 croak "Roles cannot support 'augment'";
84 Moose::Exporter->setup_import_methods(
86 qw( with requires excludes has before after around override )
89 qw( extends super inner augment ),
91 \&Scalar::Util::blessed,
99 my $role = $args{for_class};
103 Moose->throw_error("Cannot call init_meta without specifying a for_class");
106 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
107 my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta';
109 Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.")
110 unless $metaclass->isa('Moose::Meta::Role');
112 # make a subtype for each Moose role
113 role_type $role unless find_type_constraint($role);
116 if ( $meta = Class::MOP::get_metaclass_by_name($role) ) {
117 unless ( $meta->isa("Moose::Meta::Role") ) {
118 my $error_message = "$role already has a metaclass, but it does not inherit $metaclass ($meta).";
119 if ( $meta->isa('Moose::Meta::Class') ) {
120 Moose->throw_error($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.');
122 Moose->throw_error($error_message);
127 $meta = $metaclass->initialize($role);
130 if (defined $meta_name) {
131 # also check for inherited non moose 'meta' method?
132 my $existing = $meta->get_method($meta_name);
133 if ($existing && !$existing->isa('Class::MOP::Method::Meta')) {
134 Carp::cluck "Moose::Role is overwriting an existing method named "
135 . "$meta_name in role $role with a method "
136 . "which returns the class's metaclass. If this is "
137 . "actually what you want, you should remove the "
138 . "existing method, otherwise, you should rename or "
139 . "disable this generated method using the "
140 . "'-meta_name' option to 'use Moose::Role'.";
142 $meta->_add_meta_method($meta_name);
150 # ABSTRACT: The Moose Role
159 use Moose::Role; # automatically turns on strict and warnings
164 my ($self, $other) = @_;
165 !$self->equal($other);
168 # ... then in your classes
171 use Moose; # automatically turns on strict and warnings
176 my ($self, $other) = @_;
177 $self->as_float == $other->as_float;
193 my $currency1 = Currency->new(...);
194 my $currency2 = Currency->new(...);
195 Comparator->new(compare_to => $currency1)->equal($currency2);
199 The concept of roles is documented in L<Moose::Manual::Roles>. This document
200 serves as API documentation.
202 =head1 EXPORTED FUNCTIONS
204 Moose::Role currently supports all of the functions that L<Moose> exports, but
205 differs slightly in how some items are handled (see L</CAVEATS> below for
208 Moose::Role also offers two role-specific keyword exports:
212 =item B<requires (@method_names)>
214 Roles can require that certain methods are implemented by any class which
217 Note that attribute accessors also count as methods for the purposes
218 of satisfying the requirements of a role.
220 =item B<excludes (@role_names)>
222 Roles can C<exclude> other roles, in effect saying "I can never be combined
223 with these C<@role_names>". This is a feature which should not be used
230 Moose::Role offers a way to remove the keywords it exports, through the
231 C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
232 your code for this to work.
236 When you use Moose::Role, you can specify traits which will be applied to your
239 use Moose::Role -traits => 'My::Trait';
241 This is very similar to the attribute traits feature. When you do
242 this, your class's C<meta> object will have the specified traits
243 applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
246 =head1 APPLYING ROLES
248 In addition to being applied to a class using the 'with' syntax (see
249 L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles'
250 method, roles may also be applied to an instance of a class using
251 L<Moose::Util> 'apply_all_roles' or the role's metaclass:
253 MyApp::Test::SomeRole->meta->apply( $instance );
255 Doing this creates a new, mutable, anonymous subclass, applies the role to that,
256 and reblesses. In a debugger, for example, you will see class names of the
257 form C< Moose::Meta::Class::__ANON__::SERIAL::6 >, which means that doing a
258 'ref' on your instance may not return what you expect. See L<Moose::Object> for
261 Additional params may be added to the new instance by providing
262 'rebless_params'. See L<Moose::Meta::Role::Application::ToInstance>.
266 Role support has only a few caveats:
272 Roles cannot use the C<extends> keyword; it will throw an exception for now.
273 The same is true of the C<augment> and C<inner> keywords (not sure those
274 really make sense for roles). All other Moose keywords will be I<deferred>
275 so that they can be applied to the consuming class.
279 Role composition does its best to B<not> be order-sensitive when it comes to
280 conflict resolution and requirements detection. However, it is order-sensitive
281 when it comes to method modifiers. All before/around/after modifiers are
282 included whenever a role is composed into a class, and then applied in the order
283 in which the roles are used. This also means that there is no conflict for
284 before/around/after modifiers.
286 In most cases, this will be a non-issue; however, it is something to keep in
287 mind when using method modifiers in a role. You should never assume any
294 See L<Moose/BUGS> for details on reporting bugs.