updating copyright dates
[gitmo/Moose.git] / lib / Moose / Spec / Role.pod
CommitLineData
24a8fe99 1
2=pod
3
4=head1 NAME
5
6Moose::Spec::Role - Formal spec for Role behavior
7
8=head1 DESCRIPTION
9
10=head2 Components of a Role
11
12=over 4
13
14=item Excluded Roles
15
16=item Attributes
17
18=item Methods
19
20=item Required Methods
21
22=item Overriden Methods
23
24The C<override> and C<super> keywords are allowed in roles, but
25thier behavior is different from that of it's class counterparts.
26The C<super> in a class refers directly to that class's superclass,
27while the C<super> in a role is deferred and only has meaning once
28the role is composed into a class. Once that composition occurs,
29C<super> then refers to that class's superclass.
30
31It is key to remember that roles do not have hierarchy, so they
32can never have a I<super> role.
33
34=item Method Modifiers
35
36=back
37
38=head2 Role to Role Composition Rules
39
40When a role is added to another role (using the C<with>
41keyword) the two roles are composed symmetrically. The
42product of the composition is a third composite role.
43
44=over 4
45
46=item Attributes
47
48Attributes with the same name will conflict and are considered
49a un-recoverable error. No other aspect of the attribute is
50examained, it is enough that just the attribute names conflict.
51
52The reason for such early and harsh conflicts with attributes
53is because there is so much room for variance between two
54attributes that the problem quickly explodes and rules get
55very complex. It is my opinion that this complexity is not
56worth the trouble.
57
58=item Methods
59
60Methods with the same name will conflict, but no error is
61thrown, instead the method name is added to the list of
62I<required> methods for the new composite role.
63
64To look at this in terms of set theory, each role can be
65said to have a set of methods. The symmetric difference of
66these two sets is the new set of methods for the composite
67role, while the intersection of these two sets are the
68conflicts. This can be illustrated like so:
69
70 Role A has method set { a, b, c }
71 Role B has method set { c, d, e }
72
73 The composite role (A,B) has
74 method set { a, b, d, e }
75 conflict set { c }
76
77=item Overriden methods
78
79An overriden method can conflict in one of two ways.
80
81The first way is with another overriden method of the same
82name, and this is considered an un-recoverable error. This
83is an obvious error since you cannot override a method twice
84in the same class.
85
86The second way for conflict is for an overriden method and a
87regular method to have the same name. This is also an un-recoverable
88error since there is no way to combine these two, nor is it
89okay for both items to be composed into a single class at some
90point.
91
92The use of override in roles can be tricky, but if used
93carefully they can be a very powerful tool.
94
95=item Method Modifiers (before, around, after)
96
97Method modifiers are the only place where the ordering of
98role composition matters. This is due to the nature of
99method modifiers themselves.
100
101Since a method can have multiple method modifiers, these
102are just collected in order to be later applied to the
103class in that same order.
104
105In general, great care should be taken in using method
106modifiers in roles. The order sensitivity can possibly
107lead to subtle and difficult to find bugs if they are
108overused. As with all good things in life, moderation
109is the key.
110
111=back
112
113=head1 SEE ALSO
114
115=over 4
116
117=item Traits
118
119Roles are based on Traits, which originated in the Smalltalk
120community.
121
122=over 4
123
124=item L<http://www.iam.unibe.ch/~scg/Research/Traits/>
125
126This is the main site for the original Traits papers.
127
128=item L<Class::Trait>
129
130I created this implementation of traits several years ago,
131after reading the papers linked above. (This module is now
132maintatined by Ovid and I am no longer involved with it).
133
134=back
135
136=item Roles
137
138Since they are relatively new, and the Moose implementation
139is probably the most mature out there, roles don't have much
140to link to. However, here is some bits worth looking at (mostly
141related to Perl 6)
142
143=over 4
144
145=item L<http://www.oreillynet.com/onlamp/blog/2006/08/roles_composable_units_of_obje.html>
146
147This is chromatic's take on roles, which is worth reading since
148he was/is one of the big proponents of them.
149
150=item L<http://svn.perl.org/perl6/doc/trunk/design/syn/S12.pod>
151
152This is Synopsis 12, which is all about the Perl 6 Object System.
153Which, of course, includes roles.
154
155=back
156
157=back
158
159=head1 AUTHOR
160
161Stevan Little E<lt>stevan@iinteractive.comE<gt>
162
163=head1 COPYRIGHT AND LICENSE
164
778db3ac 165Copyright 2007-2008 by Infinity Interactive, Inc.
24a8fe99 166
167L<http://www.iinteractive.com>
168
169This library is free software; you can redistribute it and/or modify
170it under the same terms as Perl itself.
171
172=cut
173