release 0.20
[gitmo/Class-MOP.git] / examples / C3MethodDispatchOrder.pod
1
2 package # hide from PAUSE 
3     C3MethodDispatchOrder;
4     
5 use strict;
6 use warnings;
7
8 use Carp 'confess';
9 use Algorithm::C3;
10
11 our $VERSION = '0.02';
12
13 use base 'Class::MOP::Class';
14
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);
20     }
21 };
22
23 C3MethodDispatchOrder->meta->add_around_method_modifier('initialize' => sub {
24         my $cont = shift;
25     my $meta = $cont->(@_);
26     $meta->add_method('AUTOLOAD' => sub {
27         my $meta = $_[0]->meta;
28         my $method_name;
29         {
30             no strict 'refs';
31             my $label = ${$meta->name . '::AUTOLOAD'};
32             $method_name = (split /\:\:/ => $label)[-1];
33         }
34         my $method = $_find_method->($meta, $method_name);
35         (defined $method) || confess "Method ($method_name) not found";
36         goto &$method;
37     });
38     $meta->add_method('can' => sub {
39         $_find_method->($_[0]->meta, $_[1]);
40     });
41         return $meta;
42 });
43
44 sub superclasses {
45     my $self = shift;
46     no strict 'refs';
47     if (@_) {
48         my @supers = @_;
49         @{$self->get_package_variable('@SUPERS')} = @supers;
50     }
51     @{$self->get_package_variable('@SUPERS')};        
52 }
53
54 sub class_precedence_list {
55     my $self = shift;
56     return map {
57         $_->name;
58     } Algorithm::C3::merge($self, sub {
59         my $class = shift;
60         map { $_->meta } $class->superclasses;
61     });
62 }
63
64 1;
65
66 __END__
67
68 =pod
69
70 =head1 NAME
71
72 C3MethodDispatchOrder - An example attribute metaclass for changing to C3 method dispatch order
73
74 =head1 SYNOPSIS
75   
76   # a classic diamond inheritence graph 
77   #
78   #    <A>
79   #   /   \
80   # <B>   <C>
81   #   \   /
82   #    <D>
83   
84   package A;
85   use metaclass 'C3MethodDispatchOrder';
86   
87   sub hello { return "Hello from A" }
88   
89   package B;
90   use metaclass 'C3MethodDispatchOrder';
91   B->meta->superclasses('A');
92   
93   package C;
94   use metaclass 'C3MethodDispatchOrder';
95   C->meta->superclasses('A');
96   
97   sub hello { return "Hello from C" }
98   
99   package D;
100   use metaclass 'C3MethodDispatchOrder';
101   D->meta->superclasses('B', 'C');
102   
103   print join ", " => D->meta->class_precedence_list; # prints C3 order D, B, C, A
104   
105   # later in other code ...
106   
107   print D->hello; # print 'Hello from C' instead of the normal 'Hello from A' 
108   
109 =head1 DESCRIPTION
110
111 This is an example of how you could change the method dispatch order of a 
112 class using L<Class::MOP>. Using the L<Algorithm::C3> module, this repleces 
113 the normal depth-first left-to-right perl dispatch order with the C3 method 
114 dispatch order (see the L<Algorithm::C3> or L<Class::C3> docs for more 
115 information about this).
116
117 This example could be used as a template for other method dispatch orders 
118 as well, all that is required is to write a the C<class_precedence_list> method 
119 which will return a linearized list of classes to dispatch along. 
120
121 =head1 AUTHOR
122
123 Stevan Little E<lt>stevan@iinteractive.comE<gt>
124
125 =head1 COPYRIGHT AND LICENSE
126
127 Copyright 2006 by Infinity Interactive, Inc.
128
129 L<http://www.iinteractive.com>
130
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.
133
134 =cut