9 use Sub::Name 'subname';
13 our $VERSION = '0.03';
15 use Moose::Meta::Role;
16 use Moose::Util::TypeConstraints;
19 my ( $CALLER, %METAS );
24 return $METAS{$role} if exists $METAS{$role};
26 # make a subtype for each Moose class
29 => where { $_->does($role) }
30 unless find_type_constraint($role);
33 if ($role->can('meta')) {
34 $meta = $role->meta();
35 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
36 || confess "Whoops, not møøsey enough";
39 $meta = Moose::Meta::Role->new(role_name => $role);
40 $meta->_role_meta->add_method('meta' => sub { $meta })
43 return $METAS{$role} = $meta;
49 my $meta = _find_meta();
50 return subname 'Moose::Role::extends' => sub {
51 confess "Moose::Role does not currently support 'extends'"
55 my $meta = _find_meta();
56 return subname 'Moose::Role::with' => sub {
58 Moose::_load_all_classes(@roles);
59 ($_->can('meta') && $_->meta->isa('Moose::Meta::Role'))
60 || confess "You can only consume roles, $_ is not a Moose role"
62 if (scalar @roles == 1) {
63 $roles[0]->meta->apply($meta);
66 Moose::Meta::Role->combine(
67 map { $_->meta } @roles
73 my $meta = _find_meta();
74 return subname 'Moose::Role::requires' => sub {
75 $meta->add_required_methods(@_);
79 my $meta = _find_meta();
80 return subname 'Moose::Role::excludes' => sub {
81 $meta->add_excluded_roles(@_);
85 my $meta = _find_meta();
86 return subname 'Moose::Role::has' => sub {
87 my ($name, %options) = @_;
88 $meta->add_attribute($name, %options)
92 my $meta = _find_meta();
93 return subname 'Moose::Role::before' => sub {
95 $meta->add_before_method_modifier($_, $code) for @_;
99 my $meta = _find_meta();
100 return subname 'Moose::Role::after' => sub {
102 $meta->add_after_method_modifier($_, $code) for @_;
106 my $meta = _find_meta();
107 return subname 'Moose::Role::around' => sub {
109 $meta->add_around_method_modifier($_, $code) for @_;
113 my $meta = _find_meta();
114 return subname 'Moose::Role::super' => sub {};
117 my $meta = _find_meta();
118 return subname 'Moose::Role::override' => sub {
119 my ($name, $code) = @_;
120 $meta->add_override_method_modifier($name, $code);
124 my $meta = _find_meta();
125 return subname 'Moose::Role::inner' => sub {
126 confess "Moose::Role does not currently support 'inner'";
130 my $meta = _find_meta();
131 return subname 'Moose::Role::augment' => sub {
132 confess "Moose::Role does not currently support 'augment'";
136 return \&Carp::confess;
139 return \&Scalar::Util::blessed;
143 my $exporter = Sub::Exporter::build_exporter({
144 exports => \%exports,
153 # we should never export to main
154 return if $CALLER eq 'main';
169 Moose::Role - The Moose Role
181 my ($self, $other) = @_;
182 !$self->equal($other);
185 # ... then in your classes
195 my ($self, $other) = @_;
196 $self->as_float == $other->as_float;
201 This is currently a very early release of Perl 6 style Roles for
202 Moose, it is still incomplete, but getting much closer. If you are
203 interested in helping move this feature along, please come to
204 #moose on irc.perl.org and we can talk.
208 Currently, the role support has a few of caveats. They are as follows:
214 At this time classes I<cannot> correctly consume more than one role. The
215 role composition process, and it's conflict detection has not been added
216 yet. While this should be considered a major feature, it can easily be
217 worked around, and in many cases, is not needed at all.
219 A class can actually consume multiple roles, they are just applied one
220 after another in the order you ask for them. This is incorrect behavior,
221 the roles should be merged first, and conflicts determined, etc. However,
222 if your roles do not have any conflicts, then things will work just
223 fine. This actually tends to be quite sufficient for basic roles.
227 Roles cannot use the C<extends> keyword, it will throw an exception for now.
228 The same is true of the C<augment> and C<inner> keywords (not sure those
229 really make sense for roles). All other Moose keywords will be I<deferred>
230 so that they can be applied to the consuming class.
236 All complex software has bugs lurking in it, and this module is no
237 exception. If you find a bug please either email me, or add the bug
242 Stevan Little E<lt>stevan@iinteractive.comE<gt>
244 Christian Hansen E<lt>chansen@cpan.orgE<gt>
246 =head1 COPYRIGHT AND LICENSE
248 Copyright 2006 by Infinity Interactive, Inc.
250 L<http://www.iinteractive.com>
252 This library is free software; you can redistribute it and/or modify
253 it under the same terms as Perl itself.