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'";
27 Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ );
31 my $meta = Moose::Meta::Role->initialize(shift);
32 croak "Must specify at least one method" unless @_;
33 $meta->add_required_methods(@_);
37 my $meta = Moose::Meta::Role->initialize(shift);
38 croak "Must specify at least one role" unless @_;
39 $meta->add_excluded_roles(@_);
43 my $meta = Moose::Meta::Role->initialize(shift);
45 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
47 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
48 $meta->add_attribute( $_, %options ) for @$attrs;
52 my $meta = Moose::Meta::Role->initialize(shift);
56 croak "Moose::Role do not currently support "
58 . " references for before method modifiers"
60 $meta->add_before_method_modifier( $_, $code );
65 my $meta = Moose::Meta::Role->initialize(shift);
69 croak "Moose::Role do not currently support "
71 . " references for after method modifiers"
73 $meta->add_after_method_modifier( $_, $code );
78 my $meta = Moose::Meta::Role->initialize(shift);
81 croak "Moose::Role do not currently support "
83 . " references for around method modifiers"
85 $meta->add_around_method_modifier( $_, $code );
89 # see Moose.pm for discussion
91 return unless $Moose::SUPER_BODY;
92 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
96 my $meta = Moose::Meta::Role->initialize(shift);
97 my ( $name, $code ) = @_;
98 $meta->add_override_method_modifier( $name, $code );
102 croak "Moose::Role cannot support 'inner'";
106 croak "Moose::Role cannot support 'augment'";
109 my $exporter = Moose::Exporter->setup_import_methods(
111 qw( with requires excludes has before after around override make_immutable )
114 qw( extends super inner augment ),
116 \&Scalar::Util::blessed,
127 my $role = $args{for_class}
129 "Cannot call init_meta without specifying a for_class";
131 return $METAS{$role} if exists $METAS{$role};
133 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
135 # make a subtype for each Moose class
136 role_type $role unless find_type_constraint($role);
139 if ($role->can('meta')) {
140 $meta = $role->meta();
141 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
142 || confess "You already have a &meta function, but it does not return a Moose::Meta::Role";
145 $meta = $metaclass->initialize($role);
146 $meta->alias_method('meta' => sub { $meta });
149 return $METAS{$role} = $meta;
161 Moose::Role - The Moose Role
166 use Moose::Role; # automatically turns on strict and warnings
171 my ($self, $other) = @_;
172 !$self->equal($other);
175 # ... then in your classes
178 use Moose; # automatically turns on strict and warnings
183 my ($self, $other) = @_;
184 $self->as_float == $other->as_float;
189 Role support in Moose is pretty solid at this point. However, the best
190 documentation is still the the test suite. It is fairly safe to assume Perl 6
191 style behavior and then either refer to the test suite, or ask questions on
192 #moose if something doesn't quite do what you expect.
194 We are planning writing some more documentation in the near future, but nothing
197 =head1 EXPORTED FUNCTIONS
199 Moose::Role currently supports all of the functions that L<Moose> exports, but
200 differs slightly in how some items are handled (see L<CAVEATS> below for
203 Moose::Role also offers two role-specific keyword exports:
207 =item B<requires (@method_names)>
209 Roles can require that certain methods are implemented by any class which
212 =item B<excludes (@role_names)>
214 Roles can C<exclude> other roles, in effect saying "I can never be combined
215 with these C<@role_names>". This is a feature which should not be used
222 Moose::Role offers a way to remove the keywords it exports, through the
223 C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
224 your code for this to work.
226 =head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >>
228 The C<init_meta> method sets up the metaclass object for the role
229 specified by C<for_class>. It also injects a a C<meta> accessor into
230 the role so you can get at this object.
232 The default metaclass is L<Moose::Meta::Role>. You can specify an
233 alternate metaclass with the C<metaclass> parameter.
237 Role support has only a few caveats:
243 Roles cannot use the C<extends> keyword; it will throw an exception for now.
244 The same is true of the C<augment> and C<inner> keywords (not sure those
245 really make sense for roles). All other Moose keywords will be I<deferred>
246 so that they can be applied to the consuming class.
250 Role composition does its best to B<not> be order-sensitive when it comes to
251 conflict resolution and requirements detection. However, it is order-sensitive
252 when it comes to method modifiers. All before/around/after modifiers are
253 included whenever a role is composed into a class, and then applied in the order
254 in which the roles are used. This also means that there is no conflict for
255 before/around/after modifiers.
257 In most cases, this will be a non-issue; however, it is something to keep in
258 mind when using method modifiers in a role. You should never assume any
263 The C<requires> keyword currently only works with actual methods. A method
264 modifier (before/around/after and override) will not count as a fufillment
265 of the requirement, and neither will an autogenerated accessor for an attribute.
267 It is likely that attribute accessors will eventually be allowed to fufill those
268 requirements, or we will introduce a C<requires_attr> keyword of some kind
269 instead. This decision has not yet been finalized.
275 All complex software has bugs lurking in it, and this module is no
276 exception. If you find a bug please either email me, or add the bug
281 Stevan Little E<lt>stevan@iinteractive.comE<gt>
283 Christian Hansen E<lt>chansen@cpan.orgE<gt>
285 =head1 COPYRIGHT AND LICENSE
287 Copyright 2006-2008 by Infinity Interactive, Inc.
289 L<http://www.iinteractive.com>
291 This library is free software; you can redistribute it and/or modify
292 it under the same terms as Perl itself.