this error message changed
[gitmo/Moose.git] / lib / Moose / Role.pm
CommitLineData
e185c027 1package Moose::Role;
d1e17c7f 2use strict;
3use warnings;
e185c027 4
e65dccbc 5use Scalar::Util 'blessed';
f4f808de 6use Carp 'croak';
e185c027 7
2d562421 8use Sub::Exporter;
9
d7d8a8c7 10use Moose ();
11use Moose::Util ();
e65dccbc 12
c36b393c 13use Moose::Exporter;
e185c027 14use Moose::Meta::Role;
7eaef7ad 15use Moose::Util::TypeConstraints;
e185c027 16
e606ae5f 17sub extends {
887e3bf0 18 croak "Roles do not support 'extends' (you can use 'with' to specialize a role)";
e606ae5f 19}
20
21sub with {
d5447d26 22 Moose::Util::apply_all_roles( shift, @_ );
e606ae5f 23}
24
25sub requires {
d5447d26 26 my $meta = shift;
e606ae5f 27 croak "Must specify at least one method" unless @_;
28 $meta->add_required_methods(@_);
29}
30
31sub excludes {
d5447d26 32 my $meta = shift;
e606ae5f 33 croak "Must specify at least one role" unless @_;
34 $meta->add_excluded_roles(@_);
35}
2d562421 36
e606ae5f 37sub has {
d5447d26 38 my $meta = shift;
e606ae5f 39 my $name = shift;
40 croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1;
833b56a7 41 my %options = ( definition_context => Moose::Util::_caller_info(), @_ );
e606ae5f 42 my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ];
43 $meta->add_attribute( $_, %options ) for @$attrs;
44}
2d562421 45
2e3c7aa0 46sub _add_method_modifier {
47 my $type = shift;
d5447d26 48 my $meta = shift;
d9add270 49
50 if ( ref($_[0]) eq 'Regexp' ) {
51 croak "Roles do not currently support regex "
52 . " references for $type method modifiers";
e606ae5f 53 }
d9add270 54
55 Moose::Util::add_method_modifier($meta, $type, \@_);
e606ae5f 56}
2d562421 57
2e3c7aa0 58sub before { _add_method_modifier('before', @_) }
2d562421 59
2e3c7aa0 60sub after { _add_method_modifier('after', @_) }
fb1e11d5 61
2e3c7aa0 62sub around { _add_method_modifier('around', @_) }
fb1e11d5 63
e606ae5f 64# see Moose.pm for discussion
65sub super {
66 return unless $Moose::SUPER_BODY;
67 $Moose::SUPER_BODY->(@Moose::SUPER_ARGS);
68}
69
70sub override {
d5447d26 71 my $meta = shift;
e606ae5f 72 my ( $name, $code ) = @_;
73 $meta->add_override_method_modifier( $name, $code );
74}
75
76sub inner {
b8945921 77 croak "Roles cannot support 'inner'";
e606ae5f 78}
79
80sub augment {
b8945921 81 croak "Roles cannot support 'augment'";
e606ae5f 82}
83
c36b393c 84Moose::Exporter->setup_import_methods(
d5447d26 85 with_meta => [
d4783454 86 qw( with requires excludes has before after around override )
e606ae5f 87 ],
88 as_is => [
89 qw( extends super inner augment ),
90 \&Carp::confess,
91 \&Scalar::Util::blessed,
92 ],
93);
94
95sub init_meta {
96 shift;
97 my %args = @_;
98
70ea9161 99 my $role = $args{for_class};
100
101 unless ($role) {
102 require Moose;
103 Moose->throw_error("Cannot call init_meta without specifying a for_class");
104 }
e606ae5f 105
106 my $metaclass = $args{metaclass} || "Moose::Meta::Role";
2937ed18 107 my $meta_name = exists $args{meta_name} ? $args{meta_name} : 'meta';
e606ae5f 108
3eb29da9 109 Moose->throw_error("The Metaclass $metaclass must be loaded. (Perhaps you forgot to 'use $metaclass'?)")
110 unless Class::MOP::is_class_loaded($metaclass);
111
ed544690 112 Moose->throw_error("The Metaclass $metaclass must be a subclass of Moose::Meta::Role.")
113 unless $metaclass->isa('Moose::Meta::Role');
114
115 # make a subtype for each Moose role
e606ae5f 116 role_type $role unless find_type_constraint($role);
117
e606ae5f 118 my $meta;
ed544690 119 if ( $meta = Class::MOP::get_metaclass_by_name($role) ) {
120 unless ( $meta->isa("Moose::Meta::Role") ) {
121 my $error_message = "$role already has a metaclass, but it does not inherit $metaclass ($meta).";
122 if ( $meta->isa('Moose::Meta::Class') ) {
123 Moose->throw_error($error_message . ' You cannot make the same thing a role and a class. Remove either Moose or Moose::Role.');
124 } else {
125 Moose->throw_error($error_message);
126 }
70ea9161 127 }
e606ae5f 128 }
129 else {
130 $meta = $metaclass->initialize($role);
ed544690 131 }
e606ae5f 132
2937ed18 133 if (defined $meta_name) {
ed544690 134 # also check for inherited non moose 'meta' method?
2937ed18 135 my $existing = $meta->get_method($meta_name);
d65bfd76 136 if ($existing && !$existing->isa('Class::MOP::Method::Meta')) {
df100ac2 137 Carp::cluck "Moose::Role is overwriting an existing method named "
2937ed18 138 . "$meta_name in role $role with a method "
139 . "which returns the class's metaclass. If this is "
140 . "actually what you want, you should remove the "
141 . "existing method, otherwise, you should rename or "
142 . "disable this generated method using the "
143 . "'-meta_name' option to 'use Moose::Role'.";
d65bfd76 144 }
2937ed18 145 $meta->_add_meta_method($meta_name);
d31f9614 146 }
e606ae5f 147
148 return $meta;
e185c027 149}
150
1511;
152
ad46f524 153# ABSTRACT: The Moose Role
154
e185c027 155__END__
156
157=pod
158
76d37e5a 159=head1 SYNOPSIS
160
161 package Eq;
85424612 162 use Moose::Role; # automatically turns on strict and warnings
fb1e11d5 163
e46edf94 164 requires 'equal';
fb1e11d5 165
166 sub no_equal {
76d37e5a 167 my ($self, $other) = @_;
168 !$self->equal($other);
169 }
fb1e11d5 170
76d37e5a 171 # ... then in your classes
fb1e11d5 172
76d37e5a 173 package Currency;
85424612 174 use Moose; # automatically turns on strict and warnings
fb1e11d5 175
76d37e5a 176 with 'Eq';
fb1e11d5 177
76d37e5a 178 sub equal {
179 my ($self, $other) = @_;
bdabd620 180 $self->as_float == $other->as_float;
76d37e5a 181 }
182
36fa7a87 183 # ... and also
184
185 package Comparator;
186 use Moose;
187
188 has compare_to => (
189 is => 'ro',
190 does => 'Eq',
191 handles => 'Eq',
192 );
193
194 # ... which allows
195
196 my $currency1 = Currency->new(...);
197 my $currency2 = Currency->new(...);
198 Comparator->new(compare_to => $currency1)->equal($currency2);
199
e185c027 200=head1 DESCRIPTION
201
46b73973 202The concept of roles is documented in L<Moose::Manual::Roles>. This document
69161799 203serves as API documentation.
76d37e5a 204
2c0cbef7 205=head1 EXPORTED FUNCTIONS
206
85424612 207Moose::Role currently supports all of the functions that L<Moose> exports, but
cec39889 208differs slightly in how some items are handled (see L</CAVEATS> below for
85424612 209details).
76d37e5a 210
85424612 211Moose::Role also offers two role-specific keyword exports:
e185c027 212
213=over 4
214
2c0cbef7 215=item B<requires (@method_names)>
76d37e5a 216
fb1e11d5 217Roles can require that certain methods are implemented by any class which
85424612 218C<does> the role.
9e93dd19 219
92fcea04 220Note that attribute accessors also count as methods for the purposes
221of satisfying the requirements of a role.
222
2c0cbef7 223=item B<excludes (@role_names)>
224
9e93dd19 225Roles can C<exclude> other roles, in effect saying "I can never be combined
fb1e11d5 226with these C<@role_names>". This is a feature which should not be used
85424612 227lightly.
9e93dd19 228
2c0cbef7 229=back
230
d31f9614 231=head2 B<unimport>
232
233Moose::Role offers a way to remove the keywords it exports, through the
234C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of
235your code for this to work.
236
c1381000 237=head1 METACLASS
238
0f9d92ef 239When you use Moose::Role, you can specify traits which will be applied to your
240role metaclass:
c1381000 241
242 use Moose::Role -traits => 'My::Trait';
243
244This is very similar to the attribute traits feature. When you do
245this, your class's C<meta> object will have the specified traits
8a8856de 246applied to it. See L<Moose/Metaclass and Trait Name Resolution> for more
247details.
c1381000 248
b9cb323b 249=head1 APPLYING ROLES
250
251In addition to being applied to a class using the 'with' syntax (see
252L<Moose::Manual::Roles>) and using the L<Moose::Util> 'apply_all_roles'
253method, roles may also be applied to an instance of a class using
254L<Moose::Util> 'apply_all_roles' or the role's metaclass:
255
256 MyApp::Test::SomeRole->meta->apply( $instance );
257
258Doing this creates a new, mutable, anonymous subclass, applies the role to that,
259and reblesses. In a debugger, for example, you will see class names of the
0f9d92ef 260form C< Moose::Meta::Class::__ANON__::SERIAL::6 >, which means that doing a
261'ref' on your instance may not return what you expect. See L<Moose::Object> for
262'DOES'.
b9cb323b 263
0f9d92ef 264Additional params may be added to the new instance by providing
265'rebless_params'. See L<Moose::Meta::Role::Application::ToInstance>.
b9cb323b 266
2c0cbef7 267=head1 CAVEATS
268
85424612 269Role support has only a few caveats:
2c0cbef7 270
271=over 4
76d37e5a 272
76d37e5a 273=item *
274
fb1e11d5 275Roles cannot use the C<extends> keyword; it will throw an exception for now.
276The same is true of the C<augment> and C<inner> keywords (not sure those
277really make sense for roles). All other Moose keywords will be I<deferred>
85424612 278so that they can be applied to the consuming class.
76d37e5a 279
fb1e11d5 280=item *
2c0cbef7 281
85424612 282Role composition does its best to B<not> be order-sensitive when it comes to
283conflict resolution and requirements detection. However, it is order-sensitive
284when it comes to method modifiers. All before/around/after modifiers are
285included whenever a role is composed into a class, and then applied in the order
286in which the roles are used. This also means that there is no conflict for
287before/around/after modifiers.
2c0cbef7 288
85424612 289In most cases, this will be a non-issue; however, it is something to keep in
290mind when using method modifiers in a role. You should never assume any
2c0cbef7 291ordering.
292
e185c027 293=back
294
295=head1 BUGS
296
d4048ef3 297See L<Moose/BUGS> for details on reporting bugs.
e185c027 298
68117c45 299=cut