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