7 use Scalar::Util 'blessed';
13 our $VERSION = '0.73_02';
14 $VERSION = eval $VERSION;
15 our $AUTHORITY = 'cpan:STEVAN';
21 use Moose::Meta::Role;
22 use Moose::Util::TypeConstraints;
25 croak "Roles do not currently support 'extends'";
29 Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ );
33 my $meta = Moose::Meta::Role->initialize(shift);
34 croak "Must specify at least one method" unless @_;
35 $meta->add_required_methods(@_);
39 my $meta = Moose::Meta::Role->initialize(shift);
40 croak "Must specify at least one role" unless @_;
41 $meta->add_excluded_roles(@_);
45 my $meta = Moose::Meta::Role->initialize(shift);
47 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
49 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
50 $meta->add_attribute( $_, %options ) for @$attrs;
54 my $meta = Moose::Meta::Role->initialize(shift);
58 croak "Roles do not currently support "
60 . " references for before method modifiers"
62 $meta->add_before_method_modifier( $_, $code );
67 my $meta = Moose::Meta::Role->initialize(shift);
71 croak "Roles do not currently support "
73 . " references for after method modifiers"
75 $meta->add_after_method_modifier( $_, $code );
80 my $meta = Moose::Meta::Role->initialize(shift);
83 croak "Roles do not currently support "
85 . " references for around method modifiers"
87 $meta->add_around_method_modifier( $_, $code );
91 # see Moose.pm for discussion
93 return unless $Moose::SUPER_BODY;
94 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
98 my $meta = Moose::Meta::Role->initialize(shift);
99 my ( $name, $code ) = @_;
100 $meta->add_override_method_modifier( $name, $code );
104 croak "Roles cannot support 'inner'";
108 croak "Roles cannot support 'augment'";
111 Moose::Exporter->setup_import_methods(
113 qw( with requires excludes has before after around override make_immutable )
116 qw( extends super inner augment ),
118 \&Scalar::Util::blessed,
126 my $role = $args{for_class};
130 Moose->throw_error("Cannot call init_meta without specifying a for_class");
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);
138 # FIXME copy from Moose.pm
140 if ($role->can('meta')) {
141 $meta = $role->meta();
143 unless ( blessed($meta) && $meta->isa('Moose::Meta::Role') ) {
145 Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role");
149 $meta = $metaclass->initialize($role);
153 # re-initialize so it inherits properly
154 $metaclass->initialize( ref($_[0]) || $_[0] );
170 Moose::Role - The Moose Role
175 use Moose::Role; # automatically turns on strict and warnings
180 my ($self, $other) = @_;
181 !$self->equal($other);
184 # ... then in your classes
187 use Moose; # automatically turns on strict and warnings
192 my ($self, $other) = @_;
193 $self->as_float == $other->as_float;
198 Role support in Moose is pretty solid at this point. However, the best
199 documentation is still the the test suite. It is fairly safe to assume Perl 6
200 style behavior and then either refer to the test suite, or ask questions on
201 #moose if something doesn't quite do what you expect.
203 We are planning writing some more documentation in the near future, but nothing
206 =head1 EXPORTED FUNCTIONS
208 Moose::Role currently supports all of the functions that L<Moose> exports, but
209 differs slightly in how some items are handled (see L<CAVEATS> below for
212 Moose::Role also offers two role-specific keyword exports:
216 =item B<requires (@method_names)>
218 Roles can require that certain methods are implemented by any class which
221 =item B<excludes (@role_names)>
223 Roles can C<exclude> other roles, in effect saying "I can never be combined
224 with these C<@role_names>". This is a feature which should not be used
231 Moose::Role offers a way to remove the keywords it exports, through the
232 C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
233 your code for this to work.
235 =head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >>
237 The C<init_meta> method sets up the metaclass object for the role
238 specified by C<for_class>. It also injects a a C<meta> accessor into
239 the role so you can get at this object.
241 The default metaclass is L<Moose::Meta::Role>. You can specify an
242 alternate metaclass with the C<metaclass> parameter.
246 Role support has only a few caveats:
252 Roles cannot use the C<extends> keyword; it will throw an exception for now.
253 The same is true of the C<augment> and C<inner> keywords (not sure those
254 really make sense for roles). All other Moose keywords will be I<deferred>
255 so that they can be applied to the consuming class.
259 Role composition does its best to B<not> be order-sensitive when it comes to
260 conflict resolution and requirements detection. However, it is order-sensitive
261 when it comes to method modifiers. All before/around/after modifiers are
262 included whenever a role is composed into a class, and then applied in the order
263 in which the roles are used. This also means that there is no conflict for
264 before/around/after modifiers.
266 In most cases, this will be a non-issue; however, it is something to keep in
267 mind when using method modifiers in a role. You should never assume any
274 All complex software has bugs lurking in it, and this module is no
275 exception. If you find a bug please either email me, or add the bug
280 Stevan Little E<lt>stevan@iinteractive.comE<gt>
282 Christian Hansen E<lt>chansen@cpan.orgE<gt>
284 =head1 COPYRIGHT AND LICENSE
286 Copyright 2006-2009 by Infinity Interactive, Inc.
288 L<http://www.iinteractive.com>
290 This library is free software; you can redistribute it and/or modify
291 it under the same terms as Perl itself.