Bumping version to 0.29
[gitmo/Class-C3.git] / lib / Class / C3 / next.pm
CommitLineData
e86d671c 1package # hide me from PAUSE
97d51548 2 next;
e86d671c 3
4use strict;
5use warnings;
663e8dcc 6no warnings 'redefine'; # for 00load.t w/ core support
e86d671c 7
8use Scalar::Util 'blessed';
9
6398c848 10our $VERSION = '0.29';
e86d671c 11
12our %METHOD_CACHE;
13
14sub method {
15 my $self = $_[0];
16 my $class = blessed($self) || $self;
17 my $indirect = caller() =~ /^(?:next|maybe::next)$/;
18 my $level = $indirect ? 2 : 1;
97d51548 19
e86d671c 20 my ($method_caller, $label, @label);
21 while ($method_caller = (caller($level++))[3]) {
22 @label = (split '::', $method_caller);
23 $label = pop @label;
24 last unless
25 $label eq '(eval)' ||
26 $label eq '__ANON__';
27 }
28
29 my $method;
30
97d51548 31 my $caller = join '::' => @label;
32
e86d671c 33 $method = $METHOD_CACHE{"$class|$caller|$label"} ||= do {
97d51548 34
e86d671c 35 my @MRO = Class::C3::calculateMRO($class);
97d51548 36
e86d671c 37 my $current;
38 while ($current = shift @MRO) {
39 last if $caller eq $current;
40 }
97d51548 41
e86d671c 42 no strict 'refs';
43 my $found;
44 foreach my $class (@MRO) {
97d51548 45 next if (defined $Class::C3::MRO{$class} &&
46 defined $Class::C3::MRO{$class}{methods}{$label});
e86d671c 47 last if (defined ($found = *{$class . '::' . $label}{CODE}));
48 }
97d51548 49
e86d671c 50 $found;
51 };
52
53 return $method if $indirect;
54
55 die "No next::method '$label' found for $self" if !$method;
56
57 goto &{$method};
58}
59
60sub can { method($_[0]) }
61
62package # hide me from PAUSE
97d51548 63 maybe::next;
e86d671c 64
65use strict;
66use warnings;
663e8dcc 67no warnings 'redefine'; # for 00load.t w/ core support
e86d671c 68
6398c848 69our $VERSION = '0.29';
e86d671c 70
71sub method { (next::method($_[0]) || return)->(@_) }
72
731;
8d45f443 74
75__END__
76
77=pod
78
79=head1 NAME
80
81Class::C3::next - Pure-perl next::method and friends
82
83=head1 DESCRIPTION
84
85This module is used internally by L<Class::C3> when
0382e71f 86necessary, and shouldn't be used (or required in
8d45f443 87distribution dependencies) directly. It
88defines C<next::method>, C<next::can>, and
89C<maybe::next::method> in pure perl.
90
91=head1 AUTHOR
92
93Stevan Little, E<lt>stevan@iinteractive.comE<gt>
94
95Brandon L. Black, E<lt>blblack@gmail.comE<gt>
96
97=head1 COPYRIGHT AND LICENSE
98
99Copyright 2005, 2006 by Infinity Interactive, Inc.
100
101L<http://www.iinteractive.com>
102
103This library is free software; you can redistribute it and/or modify
97d51548 104it under the same terms as Perl itself.
8d45f443 105
106=cut