C3MethodDispatchOrder - An example attribute metaclass for changing to C3 method dispatch order
# a classic diamond inheritence graph # # <A> # / \ # <B> <C> # \ / # <D> package A; use metaclass 'C3MethodDispatchOrder'; sub hello { return "Hello from A" } package B; use metaclass 'C3MethodDispatchOrder'; B->meta->superclasses('A'); package C; use metaclass 'C3MethodDispatchOrder'; C->meta->superclasses('A'); sub hello { return "Hello from C" } package D; use metaclass 'C3MethodDispatchOrder'; D->meta->superclasses('B', 'C'); print join ", " => D->meta->class_precedence_list; # prints C3 order D, B, C, A # later in other code ... print D->hello; # print 'Hello from C' instead of the normal 'Hello from A'
This is an example of how you could change the method dispatch order of a class using Class::MOP. Using the Algorithm::C3 module, this repleces the normal depth-first left-to-right perl dispatch order with the C3 method dispatch order (see the Algorithm::C3 or Class::C3 docs for more information about this).
This example could be used as a template for other method dispatch orders as well, all that is required is to write a the class_precedence_list method which will return a linearized list of classes to dispatch along.
class_precedence_list
Stevan Little <stevan@iinteractive.com>
Yuval Kogman <nothingmuch@woobling.com>
Copyright 2006-2008 by Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Moose, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Moose
CPAN shell
perl -MCPAN -e shell install Moose
For more information on module installation, please visit the detailed CPAN module installation guide.