7 use Scalar::Util 'blessed';
8 use Carp 'confess', 'croak';
13 our $VERSION = '0.56';
14 our $AUTHORITY = 'cpan:STEVAN';
19 use Moose::Meta::Role;
20 use Moose::Util::TypeConstraints;
23 croak "Roles do not currently support 'extends'";
28 Moose::Util::apply_all_roles( $role->meta(), @_ );
33 my $meta = $role->meta();
34 croak "Must specify at least one method" unless @_;
35 $meta->add_required_methods(@_);
40 my $meta = $role->meta();
41 croak "Must specify at least one role" unless @_;
42 $meta->add_excluded_roles(@_);
47 my $meta = $role->meta();
49 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
51 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
52 $meta->add_attribute( $_, %options ) for @$attrs;
57 my $meta = $role->meta();
61 croak "Moose::Role do not currently support "
63 . " references for before method modifiers"
65 $meta->add_before_method_modifier( $_, $code );
71 my $meta = $role->meta();
75 croak "Moose::Role do not currently support "
77 . " references for after method modifiers"
79 $meta->add_after_method_modifier( $_, $code );
85 my $meta = $role->meta();
88 croak "Moose::Role do not currently support "
90 . " references for around method modifiers"
92 $meta->add_around_method_modifier( $_, $code );
96 # see Moose.pm for discussion
98 return unless $Moose::SUPER_BODY;
99 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
104 my $meta = $role->meta();
105 my ( $name, $code ) = @_;
106 $meta->add_override_method_modifier( $name, $code );
110 croak "Moose::Role cannot support 'inner'";
114 croak "Moose::Role cannot support 'augment'";
117 my $exporter = Moose::Exporter->build_import_methods(
119 qw( with requires excludes has before after around override extends super inner augment ),
121 \&Scalar::Util::blessed,
132 my $role = $args{for_class}
134 "Cannot call _init_meta without specifying a for_class";
136 return $METAS{$role} if exists $METAS{$role};
138 # make a subtype for each Moose class
139 role_type $role unless find_type_constraint($role);
142 if ($role->can('meta')) {
143 $meta = $role->meta();
144 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
145 || confess "You already have a &meta function, but it does not return a Moose::Meta::Role";
148 $meta = Moose::Meta::Role->initialize($role);
149 $meta->alias_method('meta' => sub { $meta });
152 return $METAS{$role} = $meta;
164 Moose::Role - The Moose Role
169 use Moose::Role; # automatically turns on strict and warnings
174 my ($self, $other) = @_;
175 !$self->equal($other);
178 # ... then in your classes
181 use Moose; # automatically turns on strict and warnings
186 my ($self, $other) = @_;
187 $self->as_float == $other->as_float;
192 Role support in Moose is pretty solid at this point. However, the best
193 documentation is still the the test suite. It is fairly safe to assume Perl 6
194 style behavior and then either refer to the test suite, or ask questions on
195 #moose if something doesn't quite do what you expect.
197 We are planning writing some more documentation in the near future, but nothing
200 =head1 EXPORTED FUNCTIONS
202 Moose::Role currently supports all of the functions that L<Moose> exports, but
203 differs slightly in how some items are handled (see L<CAVEATS> below for
206 Moose::Role also offers two role-specific keyword exports:
210 =item B<requires (@method_names)>
212 Roles can require that certain methods are implemented by any class which
215 =item B<excludes (@role_names)>
217 Roles can C<exclude> other roles, in effect saying "I can never be combined
218 with these C<@role_names>". This is a feature which should not be used
225 Moose::Role offers a way to remove the keywords it exports, through the
226 C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
227 your code for this to work.
231 Role support has only a few caveats:
237 Roles cannot use the C<extends> keyword; it will throw an exception for now.
238 The same is true of the C<augment> and C<inner> keywords (not sure those
239 really make sense for roles). All other Moose keywords will be I<deferred>
240 so that they can be applied to the consuming class.
244 Role composition does its best to B<not> be order-sensitive when it comes to
245 conflict resolution and requirements detection. However, it is order-sensitive
246 when it comes to method modifiers. All before/around/after modifiers are
247 included whenever a role is composed into a class, and then applied in the order
248 in which the roles are used. This also means that there is no conflict for
249 before/around/after modifiers.
251 In most cases, this will be a non-issue; however, it is something to keep in
252 mind when using method modifiers in a role. You should never assume any
257 The C<requires> keyword currently only works with actual methods. A method
258 modifier (before/around/after and override) will not count as a fufillment
259 of the requirement, and neither will an autogenerated accessor for an attribute.
261 It is likely that attribute accessors will eventually be allowed to fufill those
262 requirements, or we will introduce a C<requires_attr> keyword of some kind
263 instead. This decision has not yet been finalized.
269 All complex software has bugs lurking in it, and this module is no
270 exception. If you find a bug please either email me, or add the bug
275 Stevan Little E<lt>stevan@iinteractive.comE<gt>
277 Christian Hansen E<lt>chansen@cpan.orgE<gt>
279 =head1 COPYRIGHT AND LICENSE
281 Copyright 2006-2008 by Infinity Interactive, Inc.
283 L<http://www.iinteractive.com>
285 This library is free software; you can redistribute it and/or modify
286 it under the same terms as Perl itself.