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($role);
59 $role->meta->apply($meta);
63 my $meta = _find_meta();
64 return subname 'Moose::Role::requires' => sub {
65 $meta->add_required_methods(@_);
69 my $meta = _find_meta();
70 return subname 'Moose::Role::has' => sub {
71 my ($name, %options) = @_;
72 $meta->add_attribute($name, %options)
76 my $meta = _find_meta();
77 return subname 'Moose::Role::before' => sub {
79 $meta->add_before_method_modifier($_, $code) for @_;
83 my $meta = _find_meta();
84 return subname 'Moose::Role::after' => sub {
86 $meta->add_after_method_modifier($_, $code) for @_;
90 my $meta = _find_meta();
91 return subname 'Moose::Role::around' => sub {
93 $meta->add_around_method_modifier($_, $code) for @_;
97 my $meta = _find_meta();
98 return subname 'Moose::Role::super' => sub {};
101 my $meta = _find_meta();
102 return subname 'Moose::Role::override' => sub {
103 my ($name, $code) = @_;
104 $meta->add_override_method_modifier($name, $code);
108 my $meta = _find_meta();
109 return subname 'Moose::Role::inner' => sub {
110 confess "Moose::Role does not currently support 'inner'";
114 my $meta = _find_meta();
115 return subname 'Moose::Role::augment' => sub {
116 confess "Moose::Role does not currently support 'augment'";
120 return \&Carp::confess;
123 return \&Scalar::Util::blessed;
127 my $exporter = Sub::Exporter::build_exporter({
128 exports => \%exports,
137 # we should never export to main
138 return if $CALLER eq 'main';
153 Moose::Role - The Moose Role
165 my ($self, $other) = @_;
166 !$self->equal($other);
169 # ... then in your classes
179 my ($self, $other) = @_;
180 $self->as_float == $other->as_float;
185 This is currently a very early release of Perl 6 style Roles for
186 Moose, it is still incomplete, but getting much closer. If you are
187 interested in helping move this feature along, please come to
188 #moose on irc.perl.org and we can talk.
192 Currently, the role support has a few of caveats. They are as follows:
198 At this time classes I<cannot> correctly consume more than one role. The
199 role composition process, and it's conflict detection has not been added
200 yet. While this should be considered a major feature, it can easily be
201 worked around, and in many cases, is not needed at all.
203 A class can actually consume multiple roles, they are just applied one
204 after another in the order you ask for them. This is incorrect behavior,
205 the roles should be merged first, and conflicts determined, etc. However,
206 if your roles do not have any conflicts, then things will work just
207 fine. This actually tends to be quite sufficient for basic roles.
211 Roles cannot use the C<extends> keyword, it will throw an exception for now.
212 The same is true of the C<augment> and C<inner> keywords (not sure those
213 really make sense for roles). All other Moose keywords will be I<deferred>
214 so that they can be applied to the consuming class.
220 All complex software has bugs lurking in it, and this module is no
221 exception. If you find a bug please either email me, or add the bug
226 Stevan Little E<lt>stevan@iinteractive.comE<gt>
230 =head1 COPYRIGHT AND LICENSE
232 Copyright 2006 by Infinity Interactive, Inc.
234 L<http://www.iinteractive.com>
236 This library is free software; you can redistribute it and/or modify
237 it under the same terms as Perl itself.