7 use Scalar::Util 'blessed';
9 use Sub::Name 'subname';
13 our $VERSION = '0.05';
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 unless find_type_constraint($role);
35 if ($role->can('meta')) {
36 $meta = $role->meta();
37 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
38 || confess "Whoops, not møøsey enough";
41 $meta = Moose::Meta::Role->new(role_name => $role);
42 $meta->_role_meta->add_method('meta' => sub { $meta })
45 return $METAS{$role} = $meta;
51 my $meta = _find_meta();
52 return subname 'Moose::Role::extends' => sub {
53 confess "Moose::Role does not currently support 'extends'"
57 my $meta = _find_meta();
58 return subname 'Moose::Role::with' => sub (@) {
60 confess "Must specify at least one role" unless @roles;
61 Moose::_load_all_classes(@roles);
62 ($_->can('meta') && $_->meta->isa('Moose::Meta::Role'))
63 || confess "You can only consume roles, $_ is not a Moose role"
65 if (scalar @roles == 1) {
66 $roles[0]->meta->apply($meta);
69 Moose::Meta::Role->combine(
70 map { $_->meta } @roles
76 my $meta = _find_meta();
77 return subname 'Moose::Role::requires' => sub (@) {
78 confess "Must specify at least one method" unless @_;
79 $meta->add_required_methods(@_);
83 my $meta = _find_meta();
84 return subname 'Moose::Role::excludes' => sub (@) {
85 confess "Must specify at least one role" unless @_;
86 $meta->add_excluded_roles(@_);
90 my $meta = _find_meta();
91 return subname 'Moose::Role::has' => sub ($;%) {
92 my ($name, %options) = @_;
93 $meta->add_attribute($name, %options)
97 my $meta = _find_meta();
98 return subname 'Moose::Role::before' => sub (@&) {
99 confess "Moose::Role does not currently support 'before'";
101 $meta->add_before_method_modifier($_, $code) for @_;
105 my $meta = _find_meta();
106 return subname 'Moose::Role::after' => sub (@&) {
107 confess "Moose::Role does not currently support 'after'";
109 $meta->add_after_method_modifier($_, $code) for @_;
113 my $meta = _find_meta();
114 return subname 'Moose::Role::around' => sub (@&) {
115 confess "Moose::Role does not currently support 'around'";
117 $meta->add_around_method_modifier($_, $code) for @_;
121 my $meta = _find_meta();
122 return subname 'Moose::Role::super' => sub {
123 confess "Moose::Role cannot support 'super'";
127 my $meta = _find_meta();
128 return subname 'Moose::Role::override' => sub ($&) {
129 confess "Moose::Role cannot support 'override'";
133 my $meta = _find_meta();
134 return subname 'Moose::Role::inner' => sub {
135 confess "Moose::Role cannot support 'inner'";
139 my $meta = _find_meta();
140 return subname 'Moose::Role::augment' => sub {
141 confess "Moose::Role cannot support 'augment'";
145 return \&Carp::confess;
148 return \&Scalar::Util::blessed;
152 my $exporter = Sub::Exporter::build_exporter({
153 exports => \%exports,
165 # we should never export to main
166 return if $CALLER eq 'main';
181 Moose::Role - The Moose Role
193 my ($self, $other) = @_;
194 !$self->equal($other);
197 # ... then in your classes
207 my ($self, $other) = @_;
208 $self->as_float == $other->as_float;
213 Role support in Moose is coming along quite well. It's best documentation
214 is still the the test suite, but it is fairly safe to assume Perl 6 style
215 behavior, and then either refer to the test suite, or ask questions on
216 #moose if something doesn't quite do what you expect. More complete
217 documentation is planned and will be included with the next official
218 (non-developer) release.
220 =head1 EXPORTED FUNCTIONS
222 Currently Moose::Role supports all of the functions that L<Moose> exports,
223 but differs slightly in how some items are handled (see L<CAVEATS> below
226 Moose::Role also offers two role specific keyword exports:
230 =item B<requires (@method_names)>
232 Roles can require that certain methods are implemented by any class which
235 =item B<excludes (@role_names)>
237 Roles can C<exclude> other roles, in effect saying "I can never be combined
238 with these C<@role_names>". This is a feature which should not be used
245 The role support now has only a few caveats. They are as follows:
251 Roles cannot use the C<extends> keyword, it will throw an exception for now.
252 The same is true of the C<augment> and C<inner> keywords (not sure those
253 really make sense for roles). All other Moose keywords will be I<deferred>
254 so that they can be applied to the consuming class.
258 Role composition does it's best to B<not> be order sensitive when it comes
259 to conflict resolution and requirements detection. However, it is order
260 sensitive when it comes to method modifiers. All before/around/after modifiers
261 are included whenever a role is composed into a class, and then are applied
262 in the order the roles are used. This too means that there is no conflict for
263 before/around/after modifiers as well.
265 In most cases, this will be a non issue, however it is something to keep in
266 mind when using method modifiers in a role. You should never assume any
271 The C<requires> keyword currently only works with actual methods. A method
272 modifier (before/around/after and override) will not count as a fufillment
273 of the requirement, and neither will an autogenerated accessor for an attribute.
275 It is likely that the attribute accessors will eventually be allowed to fufill
276 those requirements, either that or we will introduce a C<requires_attr> keyword
277 of some kind instead. This descision has not yet been finalized.
283 All complex software has bugs lurking in it, and this module is no
284 exception. If you find a bug please either email me, or add the bug
289 Stevan Little E<lt>stevan@iinteractive.comE<gt>
291 Christian Hansen E<lt>chansen@cpan.orgE<gt>
293 =head1 COPYRIGHT AND LICENSE
295 Copyright 2006 by Infinity Interactive, Inc.
297 L<http://www.iinteractive.com>
299 This library is free software; you can redistribute it and/or modify
300 it under the same terms as Perl itself.