2 package # hide from PAUSE
11 our $VERSION = '0.03';
13 use base 'Class::MOP::Class';
15 my $_find_method = sub {
16 my ($class, $method) = @_;
17 foreach my $super ($class->class_precedence_list) {
18 return $super->meta->get_method($method)
19 if $super->meta->has_method($method);
23 C3MethodDispatchOrder->meta->add_around_method_modifier('initialize' => sub {
25 my $meta = $cont->(@_);
27 # we need to look at $AUTOLOAD in the package where the coderef belongs
28 # if subname works, then it'll be where this AUTOLOAD method was installed
29 # otherwise, it'll be $C3MethodDispatchOrder::AUTOLOAD. get_code_info
30 # tells us where AUTOLOAD will look
33 my ($package) = Class::MOP::get_code_info($autoload);
34 my $label = ${ $package->meta->get_package_symbol('$AUTOLOAD') };
35 my $method_name = (split /\:\:/ => $label)[-1];
36 my $method = $_find_method->($_[0]->meta, $method_name);
37 (defined $method) || confess "Method ($method_name) not found";
41 $meta->add_method('AUTOLOAD' => $autoload)
42 unless $meta->has_method('AUTOLOAD');
44 $meta->add_method('can' => sub {
45 $_find_method->($_[0]->meta, $_[1]);
46 }) unless $meta->has_method('can');
54 $self->add_package_symbol('@SUPERS' => [])
55 unless $self->has_package_symbol('@SUPERS');
59 @{$self->get_package_symbol('@SUPERS')} = @supers;
61 @{$self->get_package_symbol('@SUPERS')};
64 sub class_precedence_list {
68 } Algorithm::C3::merge($self, sub {
70 map { $_->meta } $class->superclasses;
82 C3MethodDispatchOrder - An example attribute metaclass for changing to C3 method dispatch order
86 # a classic diamond inheritence graph
95 use metaclass 'C3MethodDispatchOrder';
97 sub hello { return "Hello from A" }
100 use metaclass 'C3MethodDispatchOrder';
101 B->meta->superclasses('A');
104 use metaclass 'C3MethodDispatchOrder';
105 C->meta->superclasses('A');
107 sub hello { return "Hello from C" }
110 use metaclass 'C3MethodDispatchOrder';
111 D->meta->superclasses('B', 'C');
113 print join ", " => D->meta->class_precedence_list; # prints C3 order D, B, C, A
115 # later in other code ...
117 print D->hello; # print 'Hello from C' instead of the normal 'Hello from A'
121 This is an example of how you could change the method dispatch order of a
122 class using L<Class::MOP>. Using the L<Algorithm::C3> module, this repleces
123 the normal depth-first left-to-right perl dispatch order with the C3 method
124 dispatch order (see the L<Algorithm::C3> or L<Class::C3> docs for more
125 information about this).
127 This example could be used as a template for other method dispatch orders
128 as well, all that is required is to write a the C<class_precedence_list> method
129 which will return a linearized list of classes to dispatch along.
133 Stevan Little E<lt>stevan@iinteractive.comE<gt>
135 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
137 =head1 COPYRIGHT AND LICENSE
139 Copyright 2006-2008 by Infinity Interactive, Inc.
141 L<http://www.iinteractive.com>
143 This library is free software; you can redistribute it and/or modify
144 it under the same terms as Perl itself.