Commit | Line | Data |
e185c027 |
1 | |
2 | package Moose::Role; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | |
e65dccbc |
7 | use Scalar::Util 'blessed'; |
f4f808de |
8 | use Carp 'croak'; |
e185c027 |
9 | |
2d562421 |
10 | use Sub::Exporter; |
11 | |
75d2da34 |
12 | our $VERSION = '0.77'; |
e606ae5f |
13 | $VERSION = eval $VERSION; |
d44714be |
14 | our $AUTHORITY = 'cpan:STEVAN'; |
e185c027 |
15 | |
d7d8a8c7 |
16 | use Moose (); |
17 | use Moose::Util (); |
e65dccbc |
18 | |
c36b393c |
19 | use Moose::Exporter; |
e185c027 |
20 | use Moose::Meta::Role; |
7eaef7ad |
21 | use Moose::Util::TypeConstraints; |
e185c027 |
22 | |
e606ae5f |
23 | sub extends { |
24 | croak "Roles do not currently support 'extends'"; |
25 | } |
26 | |
27 | sub with { |
28 | Moose::Util::apply_all_roles( Moose::Meta::Role->initialize(shift), @_ ); |
29 | } |
30 | |
31 | sub requires { |
32 | my $meta = Moose::Meta::Role->initialize(shift); |
33 | croak "Must specify at least one method" unless @_; |
34 | $meta->add_required_methods(@_); |
35 | } |
36 | |
37 | sub excludes { |
38 | my $meta = Moose::Meta::Role->initialize(shift); |
39 | croak "Must specify at least one role" unless @_; |
40 | $meta->add_excluded_roles(@_); |
41 | } |
2d562421 |
42 | |
e606ae5f |
43 | sub has { |
44 | my $meta = Moose::Meta::Role->initialize(shift); |
45 | my $name = shift; |
46 | croak 'Usage: has \'name\' => ( key => value, ... )' if @_ == 1; |
47 | my %options = @_; |
48 | my $attrs = ( ref($name) eq 'ARRAY' ) ? $name : [ ($name) ]; |
49 | $meta->add_attribute( $_, %options ) for @$attrs; |
50 | } |
2d562421 |
51 | |
e606ae5f |
52 | sub before { |
53 | my $meta = Moose::Meta::Role->initialize(shift); |
54 | my $code = pop @_; |
fb1e11d5 |
55 | |
e606ae5f |
56 | for (@_) { |
b8945921 |
57 | croak "Roles do not currently support " |
e606ae5f |
58 | . ref($_) |
59 | . " references for before method modifiers" |
60 | if ref $_; |
61 | $meta->add_before_method_modifier( $_, $code ); |
62 | } |
63 | } |
2d562421 |
64 | |
e606ae5f |
65 | sub after { |
66 | my $meta = Moose::Meta::Role->initialize(shift); |
2d562421 |
67 | |
e606ae5f |
68 | my $code = pop @_; |
69 | for (@_) { |
b8945921 |
70 | croak "Roles do not currently support " |
e606ae5f |
71 | . ref($_) |
72 | . " references for after method modifiers" |
73 | if ref $_; |
74 | $meta->add_after_method_modifier( $_, $code ); |
2d562421 |
75 | } |
e606ae5f |
76 | } |
fb1e11d5 |
77 | |
e606ae5f |
78 | sub around { |
79 | my $meta = Moose::Meta::Role->initialize(shift); |
80 | my $code = pop @_; |
81 | for (@_) { |
b8945921 |
82 | croak "Roles do not currently support " |
e606ae5f |
83 | . ref($_) |
84 | . " references for around method modifiers" |
85 | if ref $_; |
86 | $meta->add_around_method_modifier( $_, $code ); |
87 | } |
88 | } |
fb1e11d5 |
89 | |
e606ae5f |
90 | # see Moose.pm for discussion |
91 | sub super { |
92 | return unless $Moose::SUPER_BODY; |
93 | $Moose::SUPER_BODY->(@Moose::SUPER_ARGS); |
94 | } |
95 | |
96 | sub override { |
97 | my $meta = Moose::Meta::Role->initialize(shift); |
98 | my ( $name, $code ) = @_; |
99 | $meta->add_override_method_modifier( $name, $code ); |
100 | } |
101 | |
102 | sub inner { |
b8945921 |
103 | croak "Roles cannot support 'inner'"; |
e606ae5f |
104 | } |
105 | |
106 | sub augment { |
b8945921 |
107 | croak "Roles cannot support 'augment'"; |
e606ae5f |
108 | } |
109 | |
c36b393c |
110 | Moose::Exporter->setup_import_methods( |
e606ae5f |
111 | with_caller => [ |
112 | qw( with requires excludes has before after around override make_immutable ) |
113 | ], |
114 | as_is => [ |
115 | qw( extends super inner augment ), |
116 | \&Carp::confess, |
117 | \&Scalar::Util::blessed, |
118 | ], |
119 | ); |
120 | |
121 | sub init_meta { |
122 | shift; |
123 | my %args = @_; |
124 | |
70ea9161 |
125 | my $role = $args{for_class}; |
126 | |
127 | unless ($role) { |
128 | require Moose; |
129 | Moose->throw_error("Cannot call init_meta without specifying a for_class"); |
130 | } |
e606ae5f |
131 | |
132 | my $metaclass = $args{metaclass} || "Moose::Meta::Role"; |
133 | |
134 | # make a subtype for each Moose class |
135 | role_type $role unless find_type_constraint($role); |
136 | |
137 | # FIXME copy from Moose.pm |
138 | my $meta; |
139 | if ($role->can('meta')) { |
140 | $meta = $role->meta(); |
70ea9161 |
141 | |
142 | unless ( blessed($meta) && $meta->isa('Moose::Meta::Role') ) { |
143 | require Moose; |
144 | Moose->throw_error("You already have a &meta function, but it does not return a Moose::Meta::Role"); |
145 | } |
e606ae5f |
146 | } |
147 | else { |
148 | $meta = $metaclass->initialize($role); |
149 | |
150 | $meta->add_method( |
151 | 'meta' => sub { |
152 | # re-initialize so it inherits properly |
153 | $metaclass->initialize( ref($_[0]) || $_[0] ); |
d31f9614 |
154 | } |
e606ae5f |
155 | ); |
d31f9614 |
156 | } |
e606ae5f |
157 | |
158 | return $meta; |
e185c027 |
159 | } |
160 | |
161 | 1; |
162 | |
163 | __END__ |
164 | |
165 | =pod |
166 | |
167 | =head1 NAME |
168 | |
169 | Moose::Role - The Moose Role |
170 | |
76d37e5a |
171 | =head1 SYNOPSIS |
172 | |
173 | package Eq; |
85424612 |
174 | use Moose::Role; # automatically turns on strict and warnings |
fb1e11d5 |
175 | |
e46edf94 |
176 | requires 'equal'; |
fb1e11d5 |
177 | |
178 | sub no_equal { |
76d37e5a |
179 | my ($self, $other) = @_; |
180 | !$self->equal($other); |
181 | } |
fb1e11d5 |
182 | |
76d37e5a |
183 | # ... then in your classes |
fb1e11d5 |
184 | |
76d37e5a |
185 | package Currency; |
85424612 |
186 | use Moose; # automatically turns on strict and warnings |
fb1e11d5 |
187 | |
76d37e5a |
188 | with 'Eq'; |
fb1e11d5 |
189 | |
76d37e5a |
190 | sub equal { |
191 | my ($self, $other) = @_; |
bdabd620 |
192 | $self->as_float == $other->as_float; |
76d37e5a |
193 | } |
194 | |
e185c027 |
195 | =head1 DESCRIPTION |
196 | |
85424612 |
197 | Role support in Moose is pretty solid at this point. However, the best |
198 | documentation is still the the test suite. It is fairly safe to assume Perl 6 |
199 | style behavior and then either refer to the test suite, or ask questions on |
200 | #moose if something doesn't quite do what you expect. |
d44714be |
201 | |
85424612 |
202 | We are planning writing some more documentation in the near future, but nothing |
203 | is ready yet, sorry. |
76d37e5a |
204 | |
2c0cbef7 |
205 | =head1 EXPORTED FUNCTIONS |
206 | |
85424612 |
207 | Moose::Role currently supports all of the functions that L<Moose> exports, but |
208 | differs slightly in how some items are handled (see L<CAVEATS> below for |
209 | details). |
76d37e5a |
210 | |
85424612 |
211 | Moose::Role also offers two role-specific keyword exports: |
e185c027 |
212 | |
213 | =over 4 |
214 | |
2c0cbef7 |
215 | =item B<requires (@method_names)> |
76d37e5a |
216 | |
fb1e11d5 |
217 | Roles can require that certain methods are implemented by any class which |
85424612 |
218 | C<does> the role. |
9e93dd19 |
219 | |
2c0cbef7 |
220 | =item B<excludes (@role_names)> |
221 | |
9e93dd19 |
222 | Roles can C<exclude> other roles, in effect saying "I can never be combined |
fb1e11d5 |
223 | with these C<@role_names>". This is a feature which should not be used |
85424612 |
224 | lightly. |
9e93dd19 |
225 | |
2c0cbef7 |
226 | =back |
227 | |
d31f9614 |
228 | =head2 B<unimport> |
229 | |
230 | Moose::Role offers a way to remove the keywords it exports, through the |
231 | C<unimport> method. You simply have to say C<no Moose::Role> at the bottom of |
232 | your code for this to work. |
233 | |
e606ae5f |
234 | =head2 B<< Moose::Role->init_meta(for_class => $role, metaclass => $metaclass) >> |
235 | |
236 | The C<init_meta> method sets up the metaclass object for the role |
237 | specified by C<for_class>. It also injects a a C<meta> accessor into |
238 | the role so you can get at this object. |
239 | |
240 | The default metaclass is L<Moose::Meta::Role>. You can specify an |
241 | alternate metaclass with the C<metaclass> parameter. |
242 | |
c1381000 |
243 | =head1 METACLASS |
244 | |
245 | When you use Moose::Role, you can specify which metaclass to use: |
246 | |
247 | use Moose::Role -metaclass => 'My::Meta::Role'; |
248 | |
249 | You can also specify traits which will be applied to your role metaclass: |
250 | |
251 | use Moose::Role -traits => 'My::Trait'; |
252 | |
253 | This is very similar to the attribute traits feature. When you do |
254 | this, your class's C<meta> object will have the specified traits |
255 | applied to it. See L<Moose/TRAIT NAME RESOLUTION> for more details. |
256 | |
2c0cbef7 |
257 | =head1 CAVEATS |
258 | |
85424612 |
259 | Role support has only a few caveats: |
2c0cbef7 |
260 | |
261 | =over 4 |
76d37e5a |
262 | |
76d37e5a |
263 | =item * |
264 | |
fb1e11d5 |
265 | Roles cannot use the C<extends> keyword; it will throw an exception for now. |
266 | The same is true of the C<augment> and C<inner> keywords (not sure those |
267 | really make sense for roles). All other Moose keywords will be I<deferred> |
85424612 |
268 | so that they can be applied to the consuming class. |
76d37e5a |
269 | |
fb1e11d5 |
270 | =item * |
2c0cbef7 |
271 | |
85424612 |
272 | Role composition does its best to B<not> be order-sensitive when it comes to |
273 | conflict resolution and requirements detection. However, it is order-sensitive |
274 | when it comes to method modifiers. All before/around/after modifiers are |
275 | included whenever a role is composed into a class, and then applied in the order |
276 | in which the roles are used. This also means that there is no conflict for |
277 | before/around/after modifiers. |
2c0cbef7 |
278 | |
85424612 |
279 | In most cases, this will be a non-issue; however, it is something to keep in |
280 | mind when using method modifiers in a role. You should never assume any |
2c0cbef7 |
281 | ordering. |
282 | |
e185c027 |
283 | =back |
284 | |
285 | =head1 BUGS |
286 | |
fb1e11d5 |
287 | All complex software has bugs lurking in it, and this module is no |
e185c027 |
288 | exception. If you find a bug please either email me, or add the bug |
289 | to cpan-RT. |
290 | |
291 | =head1 AUTHOR |
292 | |
293 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
294 | |
db1ab48d |
295 | Christian Hansen E<lt>chansen@cpan.orgE<gt> |
98aae381 |
296 | |
e185c027 |
297 | =head1 COPYRIGHT AND LICENSE |
298 | |
2840a3b2 |
299 | Copyright 2006-2009 by Infinity Interactive, Inc. |
e185c027 |
300 | |
301 | L<http://www.iinteractive.com> |
302 | |
303 | This library is free software; you can redistribute it and/or modify |
fb1e11d5 |
304 | it under the same terms as Perl itself. |
e185c027 |
305 | |
68117c45 |
306 | =cut |