10 eval { require Class::C3::XS };
12 eval { require Class::C3::PurePerl };
14 die 'Could not load Class::C3::XS or Class::C3::PurePerl!';
27 Class::C3 - A pragma to use the C3 method resolution order algortihm
33 sub hello { 'A::hello' }
43 sub hello { 'C::hello' }
49 # Classic Diamond MI pattern
58 # initializez the C3 module
59 # (formerly called in INIT)
60 Class::C3::initialize();
62 print join ', ' => Class::C3::calculateMRO('Diamond_D') # prints D, B, C, A
64 print D->hello() # prints 'C::hello' instead of the standard p5 'A::hello'
66 D->can('hello')->(); # can() also works correctly
67 UNIVERSAL::can('D', 'hello'); # as does UNIVERSAL::can()
71 This is pragma to change Perl 5's standard method resolution order from depth-first left-to-right
72 (a.k.a - pre-order) to the more sophisticated C3 method resolution order.
76 C3 is the name of an algorithm which aims to provide a sane method resolution order under multiple
77 inheritence. It was first introduced in the langauge Dylan (see links in the L<SEE ALSO> section),
78 and then later adopted as the prefered MRO (Method Resolution Order) for the new-style classes in
79 Python 2.3. Most recently it has been adopted as the 'canonical' MRO for Perl 6 classes, and the
80 default MRO for Parrot objects as well.
82 =head2 How does C3 work.
84 C3 works by always preserving local precendence ordering. This essentially means that no class will
85 appear before any of it's subclasses. Take the classic diamond inheritence pattern for instance:
93 The standard Perl 5 MRO would be (D, B, A, C). The result being that B<A> appears before B<C>, even
94 though B<C> is the subclass of B<A>. The C3 MRO algorithm however, produces the following MRO
95 (D, B, C, A), which does not have this same issue.
97 This example is fairly trival, for more complex examples and a deeper explaination, see the links in
98 the L<SEE ALSO> section.
100 =head2 How does this module work?
102 This module uses a technique similar to Perl 5's method caching. When C<Class::C3::initialize> is
103 called, this module calculates the MRO of all the classes which called C<use Class::C3>. It then
104 gathers information from the symbol tables of each of those classes, and builds a set of method
105 aliases for the correct dispatch ordering. Once all these C3-based method tables are created, it
106 then adds the method aliases into the local classes symbol table.
108 The end result is actually classes with pre-cached method dispatch. However, this caching does not
109 do well if you start changing your C<@ISA> or messing with class symbol tables, so you should consider
110 your classes to be effectively closed. See the L<CAVEATS> section for more details.
112 =head1 OPTIONAL LOWERCASE PRAGMA
114 This release also includes an optional module B<c3> in the F<opt/> folder. I did not include this in
115 the regular install since lowercase module names are considered I<"bad"> by some people. However I
116 think that code looks much nicer like this:
126 But hey, it's your choice, thats why it is optional.
132 =item B<calculateMRO ($class)>
134 Given a C<$class> this will return an array of class names in the proper C3 method resolution order.
138 This B<must be called> to initalize the C3 method dispatch tables, this module B<will not work> if
139 you do not do this. It is advised to do this as soon as possible B<after> loading any classes which
140 use C3. Here is a quick code example:
144 # ... Foo methods here
149 # ... Bar methods here
153 Class::C3::initialize(); # now it is safe to use Foo and Bar
155 This function used to be called automatically for you in the INIT phase of the perl compiler, but
156 that lead to warnings if this module was required at runtime. After discussion with my user base
157 (the L<DBIx::Class> folks), we decided that calling this in INIT was more of an annoyance than a
158 convience. I apologize to anyone this causes problems for (although i would very suprised if I had
159 any other users other than the L<DBIx::Class> folks). The simplest solution of course is to define
160 your own INIT method which calls this function.
164 If C<initialize> detects that C<initialize> has already been executed, it will L</uninitialize> and
165 clear the MRO cache first.
167 =item B<uninitialize>
169 Calling this function results in the removal of all cached methods, and the restoration of the old Perl 5
170 style dispatch order (depth-first, left-to-right).
172 =item B<reinitialize>
174 This is an alias for L</initialize> above.
178 =head1 METHOD REDISPATCHING
180 It is always useful to be able to re-dispatch your method call to the "next most applicable method". This
181 module provides a pseudo package along the lines of C<SUPER::> or C<NEXT::> which will re-dispatch the
182 method along the C3 linearization. This is best show with an examples.
184 # a classic diamond MI pattern ...
198 sub foo { 'B::foo => ' . (shift)->next::method() }
203 sub foo { 'C::foo => ' . (shift)->next::method() }
208 sub foo { 'D::foo => ' . (shift)->next::method() }
210 print D->foo; # prints out "D::foo => B::foo => C::foo => A::foo"
212 A few things to note. First, we do not require you to add on the method name to the C<next::method>
213 call (this is unlike C<NEXT::> and C<SUPER::> which do require that). This helps to enforce the rule
214 that you cannot dispatch to a method of a different name (this is how C<NEXT::> behaves as well).
216 The next thing to keep in mind is that you will need to pass all arguments to C<next::method> it can
217 not automatically use the current C<@_>.
219 If C<next::method> cannot find a next method to re-dispatch the call to, it will throw an exception.
220 You can use C<next::can> to see if C<next::method> will succeed before you call it like so:
222 $self->next::method(@_) if $self->next::can;
224 Additionally, you can use C<maybe::next::method> as a shortcut to only call the next method if it exists.
225 The previous example could be simply written as:
227 $self->maybe::next::method(@_);
229 There are some caveats about using C<next::method>, see below for those.
233 This module used to be labeled as I<experimental>, however it has now been pretty heavily tested by
234 the good folks over at L<DBIx::Class> and I am confident this module is perfectly usable for
235 whatever your needs might be.
237 But there are still caveats, so here goes ...
241 =item Use of C<SUPER::>.
243 The idea of C<SUPER::> under multiple inheritence is ambigious, and generally not recomended anyway.
244 However, it's use in conjuntion with this module is very much not recommended, and in fact very
245 discouraged. The recommended approach is to instead use the supplied C<next::method> feature, see
246 more details on it's usage above.
248 =item Changing C<@ISA>.
250 It is the author's opinion that changing C<@ISA> at runtime is pure insanity anyway. However, people
251 do it, so I must caveat. Any changes to the C<@ISA> will not be reflected in the MRO calculated by this
252 module, and therefor probably won't even show up. If you do this, you will need to call C<reinitialize>
253 in order to recalulate B<all> method dispatch tables. See the C<reinitialize> documentation and an example
254 in F<t/20_reinitialize.t> for more information.
256 =item Adding/deleting methods from class symbol tables.
258 This module calculates the MRO for each requested class by interogatting the symbol tables of said classes.
259 So any symbol table manipulation which takes place after our INIT phase is run will not be reflected in
260 the calculated MRO. Just as with changing the C<@ISA>, you will need to call C<reinitialize> for any
261 changes you make to take effect.
263 =item Calling C<next::method> from methods defined outside the class
265 There is an edge case when using C<next::method> from within a subroutine which was created in a different
266 module than the one it is called from. It sounds complicated, but it really isn't. Here is an example which
267 will not work correctly:
269 *Foo::foo = sub { (shift)->next::method(@_) };
271 The problem exists because the anonymous subroutine being assigned to the glob C<*Foo::foo> will show up
272 in the call stack as being called C<__ANON__> and not C<foo> as you might expect. Since C<next::method>
273 uses C<caller> to find the name of the method it was called in, it will fail in this case.
275 But fear not, there is a simple solution. The module C<Sub::Name> will reach into the perl internals and
276 assign a name to an anonymous subroutine for you. Simply do this:
278 use Sub::Name 'subname';
279 *Foo::foo = subname 'Foo::foo' => sub { (shift)->next::method(@_) };
281 and things will Just Work. Of course this is not always possible to do, but to be honest, I just can't
282 manage to find a workaround for it, so until someone gives me a working patch this will be a known
283 limitation of this module.
289 I use B<Devel::Cover> to test the code coverage of my tests, below is the B<Devel::Cover> report on this
292 ---------------------------- ------ ------ ------ ------ ------ ------ ------
293 File stmt bran cond sub pod time total
294 ---------------------------- ------ ------ ------ ------ ------ ------ ------
295 Class/C3.pm 98.3 84.4 80.0 96.2 100.0 98.4 94.4
296 ---------------------------- ------ ------ ------ ------ ------ ------ ------
297 Total 98.3 84.4 80.0 96.2 100.0 98.4 94.4
298 ---------------------------- ------ ------ ------ ------ ------ ------ ------
302 =head2 The original Dylan paper
306 =item L<http://www.webcom.com/haahr/dylan/linearization-oopsla96.html>
310 =head2 The prototype Perl 6 Object Model uses C3
314 =item L<http://svn.openfoundry.org/pugs/perl5/Perl6-MetaModel/>
318 =head2 Parrot now uses C3
322 =item L<http://aspn.activestate.com/ASPN/Mail/Message/perl6-internals/2746631>
324 =item L<http://use.perl.org/~autrijus/journal/25768>
328 =head2 Python 2.3 MRO related links
332 =item L<http://www.python.org/2.3/mro.html>
334 =item L<http://www.python.org/2.2.2/descrintro.html#mro>
338 =head2 C3 for TinyCLOS
342 =item L<http://www.call-with-current-continuation.org/eggs/c3.html>
346 =head1 ACKNOWLEGEMENTS
350 =item Thanks to Matt S. Trout for using this module in his module L<DBIx::Class>
351 and finding many bugs and providing fixes.
353 =item Thanks to Justin Guenther for making C<next::method> more robust by handling
354 calls inside C<eval> and anon-subs.
356 =item Thanks to Robert Norris for adding support for C<next::can> and
357 C<maybe::next::method>.
363 Stevan Little, E<lt>stevan@iinteractive.comE<gt>
365 Brandon L. Black, E<lt>blblack@gmail.comE<gt>
367 =head1 COPYRIGHT AND LICENSE
369 Copyright 2005, 2006 by Infinity Interactive, Inc.
371 L<http://www.iinteractive.com>
373 This library is free software; you can redistribute it and/or modify
374 it under the same terms as Perl itself.