7 use Scalar::Util 'blessed';
9 use Sub::Name 'subname';
14 our $VERSION = '0.07';
15 our $AUTHORITY = 'cpan:STEVAN';
20 use Moose::Meta::Role;
21 use Moose::Util::TypeConstraints;
24 my ( $CALLER, %METAS );
29 return $METAS{$role} if exists $METAS{$role};
31 # make a subtype for each Moose class
34 => where { $_->does($role) }
35 => optimize_as { blessed($_[0]) && $_[0]->can('does') && $_[0]->does($role) }
36 unless find_type_constraint($role);
39 if ($role->can('meta')) {
40 $meta = $role->meta();
41 (blessed($meta) && $meta->isa('Moose::Meta::Role'))
42 || confess "You already have a &meta function, but it does not return a Moose::Meta::Role";
45 $meta = Moose::Meta::Role->initialize($role);
46 $meta->alias_method('meta' => sub { $meta });
49 return $METAS{$role} = $meta;
55 my $meta = _find_meta();
56 return subname 'Moose::Role::extends' => sub {
57 confess "Moose::Role does not currently support 'extends'"
61 my $meta = _find_meta();
62 return subname 'Moose::Role::with' => sub (@) {
63 Moose::Util::apply_all_roles($meta, @_)
67 my $meta = _find_meta();
68 return subname 'Moose::Role::requires' => sub (@) {
69 confess "Must specify at least one method" unless @_;
70 $meta->add_required_methods(@_);
74 my $meta = _find_meta();
75 return subname 'Moose::Role::excludes' => sub (@) {
76 confess "Must specify at least one role" unless @_;
77 $meta->add_excluded_roles(@_);
81 my $meta = _find_meta();
82 return subname 'Moose::Role::has' => sub ($;%) {
83 my ($name, %options) = @_;
84 $meta->add_attribute($name, %options)
88 my $meta = _find_meta();
89 return subname 'Moose::Role::before' => sub (@&) {
91 $meta->add_before_method_modifier($_, $code) for @_;
95 my $meta = _find_meta();
96 return subname 'Moose::Role::after' => sub (@&) {
98 $meta->add_after_method_modifier($_, $code) for @_;
102 my $meta = _find_meta();
103 return subname 'Moose::Role::around' => sub (@&) {
105 $meta->add_around_method_modifier($_, $code) for @_;
111 $Moose::SUPER_SLOT{$CALLER} = \*{"${CALLER}::super"};
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 cannot support 'inner'";
130 my $meta = _find_meta();
131 return subname 'Moose::Role::augment' => sub {
132 confess "Moose::Role cannot support 'augment'";
136 return \&Carp::confess;
139 return \&Scalar::Util::blessed;
143 my $exporter = Sub::Exporter::build_exporter({
144 exports => \%exports,
152 ref $_[1] && defined $_[1]->{into} ? $_[1]->{into}
154 && defined $_[1]->{into_level} ? caller( $_[1]->{into_level} )
161 # we should never export to main
162 return if $CALLER eq 'main';
177 Moose::Role - The Moose Role
182 use Moose::Role; # automatically turns on strict and warnings
187 my ($self, $other) = @_;
188 !$self->equal($other);
191 # ... then in your classes
194 use Moose; # automatically turns on strict and warnings
199 my ($self, $other) = @_;
200 $self->as_float == $other->as_float;
205 Role support in Moose is pretty solid at this point. However, the best
206 documentation is still the the test suite. It is fairly safe to assume Perl 6
207 style behavior and then either refer to the test suite, or ask questions on
208 #moose if something doesn't quite do what you expect.
210 We are planning writing some more documentation in the near future, but nothing
213 =head1 EXPORTED FUNCTIONS
215 Moose::Role currently supports all of the functions that L<Moose> exports, but
216 differs slightly in how some items are handled (see L<CAVEATS> below for
219 Moose::Role also offers two role-specific keyword exports:
223 =item B<requires (@method_names)>
225 Roles can require that certain methods are implemented by any class which
228 =item B<excludes (@role_names)>
230 Roles can C<exclude> other roles, in effect saying "I can never be combined
231 with these C<@role_names>". This is a feature which should not be used
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.