From Code to Community: Sponsoring The Perl and Raku Conference 2025 Learn more

NAME

C3MethodDispatchOrder - An example attribute metaclass for changing to C3 method dispatch order

SYNOPSIS

# 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'

DESCRIPTION

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.

AUTHORS

Stevan Little <stevan@iinteractive.com>

Yuval Kogman <nothingmuch@woobling.com>

COPYRIGHT AND LICENSE

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.