7 use Scalar::Util 'blessed';
13 our $VERSION = '0.62_01';
14 $VERSION = eval $VERSION;
15 our $AUTHORITY = 'cpan:STEVAN';
20 use Moose::Meta::Role;
21 use Moose::Util::TypeConstraints;
24 croak "Roles do not currently support 'extends'";
28 Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ );
32 my $meta = Moose::Meta::Role->initialize(shift);
33 croak "Must specify at least one method" unless @_;
34 $meta->add_required_methods(@_);
38 my $meta = Moose::Meta::Role->initialize(shift);
39 croak "Must specify at least one role" unless @_;
40 $meta->add_excluded_roles(@_);
44 my $meta = Moose::Meta::Role->initialize(shift);
46 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
48 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
49 $meta->add_attribute( $_, %options ) for @$attrs;
53 my $meta = Moose::Meta::Role->initialize(shift);
57 croak "Roles do not currently support "
59 . " references for before method modifiers"
61 $meta->add_before_method_modifier( $_, $code );
66 my $meta = Moose::Meta::Role->initialize(shift);
70 croak "Roles do not currently support "
72 . " references for after method modifiers"
74 $meta->add_after_method_modifier( $_, $code );
79 my $meta = Moose::Meta::Role->initialize(shift);
82 croak "Roles do not currently support "
84 . " references for around method modifiers"
86 $meta->add_around_method_modifier( $_, $code );
90 # see Moose.pm for discussion
92 return unless $Moose::SUPER_BODY;
93 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
97 my $meta = Moose::Meta::Role->initialize(shift);
98 my ( $name, $code ) = @_;
99 $meta->add_override_method_modifier( $name, $code );
103 croak "Roles cannot support 'inner'";
107 croak "Roles cannot support 'augment'";
110 my $exporter = Moose::Exporter->setup_import_methods(
112 qw( with requires excludes has before after around override make_immutable )
115 qw( extends super inner augment ),
117 \&Scalar::Util::blessed,
125 my $role = $args{for_class}
126 or Moose->throw_error("Cannot call init_meta without specifying a for_class");
128 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
130 # make a subtype for each Moose class
131 role_type $role unless find_type_constraint($role);
133 # FIXME copy from Moose.pm
135 if ($role->can('meta')) {
136 $meta = $role->meta();
137 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
138 || Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role");
141 $meta = $metaclass->initialize($role);
145 # re-initialize so it inherits properly
146 $metaclass->initialize( ref($_[0]) || $_[0] );
162 Moose::Role - The Moose Role
167 use Moose::Role; # automatically turns on strict and warnings
172 my ($self, $other) = @_;
173 !$self->equal($other);
176 # ... then in your classes
179 use Moose; # automatically turns on strict and warnings
184 my ($self, $other) = @_;
185 $self->as_float == $other->as_float;
190 Role support in Moose is pretty solid at this point. However, the best
191 documentation is still the the test suite. It is fairly safe to assume Perl 6
192 style behavior and then either refer to the test suite, or ask questions on
193 #moose if something doesn't quite do what you expect.
195 We are planning writing some more documentation in the near future, but nothing
198 =head1 EXPORTED FUNCTIONS
200 Moose::Role currently supports all of the functions that L<Moose> exports, but
201 differs slightly in how some items are handled (see L<CAVEATS> below for
204 Moose::Role also offers two role-specific keyword exports:
208 =item B<requires (@method_names)>
210 Roles can require that certain methods are implemented by any class which
213 =item B<excludes (@role_names)>
215 Roles can C<exclude> other roles, in effect saying "I can never be combined
216 with these C<@role_names>". This is a feature which should not be used
223 Moose::Role offers a way to remove the keywords it exports, through the
224 C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
225 your code for this to work.
227 =head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >>
229 The C<init_meta> method sets up the metaclass object for the role
230 specified by C<for_class>. It also injects a a C<meta> accessor into
231 the role so you can get at this object.
233 The default metaclass is L<Moose::Meta::Role>. You can specify an
234 alternate metaclass with the C<metaclass> parameter.
238 Role support has only a few caveats:
244 Roles cannot use the C<extends> keyword; it will throw an exception for now.
245 The same is true of the C<augment> and C<inner> keywords (not sure those
246 really make sense for roles). All other Moose keywords will be I<deferred>
247 so that they can be applied to the consuming class.
251 Role composition does its best to B<not> be order-sensitive when it comes to
252 conflict resolution and requirements detection. However, it is order-sensitive
253 when it comes to method modifiers. All before/around/after modifiers are
254 included whenever a role is composed into a class, and then applied in the order
255 in which the roles are used. This also means that there is no conflict for
256 before/around/after modifiers.
258 In most cases, this will be a non-issue; however, it is something to keep in
259 mind when using method modifiers in a role. You should never assume any
264 The C<requires> keyword currently only works with actual methods. A method
265 modifier (before/around/after and override) will not count as a fufillment
266 of the requirement, and neither will an autogenerated accessor for an attribute.
268 It is likely that attribute accessors will eventually be allowed to fufill those
269 requirements, or we will introduce a C<requires_attr> keyword of some kind
270 instead. This decision has not yet been finalized.
276 All complex software has bugs lurking in it, and this module is no
277 exception. If you find a bug please either email me, or add the bug
282 Stevan Little E<lt>stevan@iinteractive.comE<gt>
284 Christian Hansen E<lt>chansen@cpan.orgE<gt>
286 =head1 COPYRIGHT AND LICENSE
288 Copyright 2006-2008 by Infinity Interactive, Inc.
290 L<http://www.iinteractive.com>
292 This library is free software; you can redistribute it and/or modify
293 it under the same terms as Perl itself.