7 use Scalar::Util 'blessed';
9 use Sub::Name 'subname';
13 our $VERSION = '0.06';
17 use Moose::Meta::Role;
18 use Moose::Util::TypeConstraints;
21 my ( $CALLER, %METAS );
26 return $METAS{$role} if exists $METAS{$role};
28 # make a subtype for each Moose class
31 => where { $_->does($role) }
32 => optimize_as { blessed($_[0]) && $_[0]->can('does') && $_[0]->does($role) }
33 unless find_type_constraint($role);
36 if ($role->can('meta')) {
37 $meta = $role->meta();
38 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
39 || confess "You already have a &meta function, but it does not return a Moose::Meta::Role";
42 $meta = Moose::Meta::Role->initialize($role);
43 $meta->Moose::Meta::Class::add_method('meta' => sub { $meta })
46 return $METAS{$role} = $meta;
52 my $meta = _find_meta();
53 return subname 'Moose::Role::extends' => sub {
54 confess "Moose::Role does not currently support 'extends'"
58 my $meta = _find_meta();
59 return subname 'Moose::Role::with' => sub (@) {
61 confess "Must specify at least one role" unless @roles;
62 Class::MOP::load_class($_) for @roles;
63 ($_->can('meta') && $_->meta->isa('Moose::Meta::Role'))
64 || confess "You can only consume roles, $_ is not a Moose role"
66 if (scalar @roles == 1) {
67 $roles[0]->meta->apply($meta);
70 Moose::Meta::Role->combine(
71 map { $_->meta } @roles
77 my $meta = _find_meta();
78 return subname 'Moose::Role::requires' => sub (@) {
79 confess "Must specify at least one method" unless @_;
80 $meta->add_required_methods(@_);
84 my $meta = _find_meta();
85 return subname 'Moose::Role::excludes' => sub (@) {
86 confess "Must specify at least one role" unless @_;
87 $meta->add_excluded_roles(@_);
91 my $meta = _find_meta();
92 return subname 'Moose::Role::has' => sub ($;%) {
93 my ($name, %options) = @_;
94 $meta->add_attribute($name, %options)
98 my $meta = _find_meta();
99 return subname 'Moose::Role::before' => sub (@&) {
101 $meta->add_before_method_modifier($_, $code) for @_;
105 my $meta = _find_meta();
106 return subname 'Moose::Role::after' => sub (@&) {
108 $meta->add_after_method_modifier($_, $code) for @_;
112 my $meta = _find_meta();
113 return subname 'Moose::Role::around' => sub (@&) {
115 $meta->add_around_method_modifier($_, $code) for @_;
119 my $meta = _find_meta();
120 return subname 'Moose::Role::super' => sub {};
123 my $meta = _find_meta();
124 return subname 'Moose::Role::override' => sub ($&) {
125 my ($name, $code) = @_;
126 $meta->add_override_method_modifier($name, $code);
130 my $meta = _find_meta();
131 return subname 'Moose::Role::inner' => sub {
132 confess "Moose::Role cannot support 'inner'";
136 my $meta = _find_meta();
137 return subname 'Moose::Role::augment' => sub {
138 confess "Moose::Role cannot support 'augment'";
142 return \&Carp::confess;
145 return \&Scalar::Util::blessed;
149 my $exporter = Sub::Exporter::build_exporter({
150 exports => \%exports,
162 # we should never export to main
163 return if $CALLER eq 'main';
178 Moose::Role - The Moose Role
190 my ($self, $other) = @_;
191 !$self->equal($other);
194 # ... then in your classes
204 my ($self, $other) = @_;
205 $self->as_float == $other->as_float;
210 Role support in Moose is coming along quite well. It's best documentation
211 is still the the test suite, but it is fairly safe to assume Perl 6 style
212 behavior, and then either refer to the test suite, or ask questions on
213 #moose if something doesn't quite do what you expect. More complete
214 documentation is planned and will be included with the next official
215 (non-developer) release.
217 =head1 EXPORTED FUNCTIONS
219 Currently Moose::Role supports all of the functions that L<Moose> exports,
220 but differs slightly in how some items are handled (see L<CAVEATS> below
223 Moose::Role also offers two role specific keyword exports:
227 =item B<requires (@method_names)>
229 Roles can require that certain methods are implemented by any class which
232 =item B<excludes (@role_names)>
234 Roles can C<exclude> other roles, in effect saying "I can never be combined
235 with these C<@role_names>". This is a feature which should not be used
242 The role support now has only a few caveats. They are as follows:
248 Roles cannot use the C<extends> keyword, it will throw an exception for now.
249 The same is true of the C<augment> and C<inner> keywords (not sure those
250 really make sense for roles). All other Moose keywords will be I<deferred>
251 so that they can be applied to the consuming class.
255 Role composition does it's best to B<not> be order sensitive when it comes
256 to conflict resolution and requirements detection. However, it is order
257 sensitive when it comes to method modifiers. All before/around/after modifiers
258 are included whenever a role is composed into a class, and then are applied
259 in the order the roles are used. This too means that there is no conflict for
260 before/around/after modifiers as well.
262 In most cases, this will be a non issue, however it is something to keep in
263 mind when using method modifiers in a role. You should never assume any
268 The C<requires> keyword currently only works with actual methods. A method
269 modifier (before/around/after and override) will not count as a fufillment
270 of the requirement, and neither will an autogenerated accessor for an attribute.
272 It is likely that the attribute accessors will eventually be allowed to fufill
273 those requirements, either that or we will introduce a C<requires_attr> keyword
274 of some kind instead. This descision has not yet been finalized.
280 All complex software has bugs lurking in it, and this module is no
281 exception. If you find a bug please either email me, or add the bug
286 Stevan Little E<lt>stevan@iinteractive.comE<gt>
288 Christian Hansen E<lt>chansen@cpan.orgE<gt>
290 =head1 COPYRIGHT AND LICENSE
292 Copyright 2006, 2007 by Infinity Interactive, Inc.
294 L<http://www.iinteractive.com>
296 This library is free software; you can redistribute it and/or modify
297 it under the same terms as Perl itself.