1# mro.pm 2# 3# Copyright (c) 2007 Brandon L Black 4# Copyright (c) 2008,2009 Larry Wall and others 5# 6# You may distribute under the terms of either the GNU General Public 7# License or the Artistic License, as specified in the README file. 8# 9package mro; 10use strict; 11use warnings; 12 13# mro.pm versions < 1.00 reserved for MRO::Compat 14# for partial back-compat to 5.[68].x 15our $VERSION = '1.29'; 16 17require XSLoader; 18XSLoader::load('mro'); 19 20sub import { 21 mro::set_mro(scalar(caller), $_[1]) if $_[1]; 22} 23 24package # hide me from PAUSE 25 next; 26 27sub can { mro::_nextcan($_[0], 0) } 28 29sub method { 30 my $method = mro::_nextcan($_[0], 1); 31 goto &$method; 32} 33 34package # hide me from PAUSE 35 maybe::next; 36 37sub method { 38 my $method = mro::_nextcan($_[0], 0); 39 goto &$method if defined $method; 40 return; 41} 42 431; 44 45__END__ 46 47=head1 NAME 48 49mro - Method Resolution Order 50 51=head1 SYNOPSIS 52 53 use mro; # enables next::method and friends globally 54 55 use mro 'dfs'; # enable DFS MRO for this class (Perl default) 56 use mro 'c3'; # enable C3 MRO for this class 57 58=head1 DESCRIPTION 59 60The "mro" namespace provides several utilities for dealing 61with method resolution order and method caching in general. 62 63These interfaces are only available in Perl 5.9.5 and higher. 64See L<MRO::Compat> on CPAN for a mostly forwards compatible 65implementation for older Perls. 66 67=head1 OVERVIEW 68 69It's possible to change the MRO of a given class either by using C<use 70mro> as shown in the synopsis, or by using the L</mro::set_mro> function 71below. 72 73The special methods C<next::method>, C<next::can>, and 74C<maybe::next::method> are not available until this C<mro> module 75has been loaded via C<use> or C<require>. 76 77=head1 The C3 MRO 78 79In addition to the traditional Perl default MRO (depth first 80search, called C<DFS> here), Perl now offers the C3 MRO as 81well. Perl's support for C3 is based on the work done in 82Stevan Little's module L<Class::C3>, and most of the C3-related 83documentation here is ripped directly from there. 84 85=head2 What is C3? 86 87C3 is the name of an algorithm which aims to provide a sane method 88resolution order under multiple inheritance. It was first introduced in 89the language Dylan (see links in the L</"SEE ALSO"> section), and then 90later adopted as the preferred MRO (Method Resolution Order) for the 91new-style classes in Python 2.3. Most recently it has been adopted as the 92"canonical" MRO for Raku classes. 93 94=head2 How does C3 work 95 96C3 works by always preserving local precedence ordering. This essentially 97means that no class will appear before any of its subclasses. Take, for 98instance, the classic diamond inheritance pattern: 99 100 <A> 101 / \ 102 <B> <C> 103 \ / 104 <D> 105 106The standard Perl 5 MRO would be (D, B, A, C). The result being that B<A> 107appears before B<C>, even though B<C> is the subclass of B<A>. The C3 MRO 108algorithm however, produces the following order: (D, B, C, A), which does 109not have this issue. 110 111This example is fairly trivial; for more complex cases and a deeper 112explanation, see the links in the L</"SEE ALSO"> section. 113 114=head1 Functions 115 116=head2 mro::get_linear_isa($classname[, $type]) 117 118Returns an arrayref which is the linearized MRO of the given class. 119Uses whichever MRO is currently in effect for that class by default, 120or the given MRO (either C<c3> or C<dfs> if specified as C<$type>). 121 122The linearized MRO of a class is an ordered array of all of the 123classes one would search when resolving a method on that class, 124starting with the class itself. 125 126If the requested class doesn't yet exist, this function will still 127succeed, and return C<[ $classname ]> 128 129Note that C<UNIVERSAL> (and any members of C<UNIVERSAL>'s MRO) are not 130part of the MRO of a class, even though all classes implicitly inherit 131methods from C<UNIVERSAL> and its parents. 132 133=head2 mro::set_mro ($classname, $type) 134 135Sets the MRO of the given class to the C<$type> argument (either 136C<c3> or C<dfs>). 137 138=head2 mro::get_mro($classname) 139 140Returns the MRO of the given class (either C<c3> or C<dfs>). 141 142=head2 mro::get_isarev($classname) 143 144Gets the C<mro_isarev> for this class, returned as an 145arrayref of class names. These are every class that "isa" 146the given class name, even if the isa relationship is 147indirect. This is used internally by the MRO code to 148keep track of method/MRO cache invalidations. 149 150As with C<mro::get_linear_isa> above, C<UNIVERSAL> is special. 151C<UNIVERSAL> (and parents') isarev lists do not include 152every class in existence, even though all classes are 153effectively descendants for method inheritance purposes. 154 155=head2 mro::is_universal($classname) 156 157Returns a boolean status indicating whether or not 158the given classname is either C<UNIVERSAL> itself, 159or one of C<UNIVERSAL>'s parents by C<@ISA> inheritance. 160 161Any class for which this function returns true is 162"universal" in the sense that all classes potentially 163inherit methods from it. 164 165=head2 mro::invalidate_all_method_caches() 166 167Increments C<PL_sub_generation>, which invalidates method 168caching in all packages. 169 170=head2 mro::method_changed_in($classname) 171 172Invalidates the method cache of any classes dependent on the 173given class. This is not normally necessary. The only 174known case where pure perl code can confuse the method 175cache is when you manually install a new constant 176subroutine by using a readonly scalar value, like the 177internals of L<constant> do. If you find another case, 178please report it so we can either fix it or document 179the exception here. 180 181=head2 mro::get_pkg_gen($classname) 182 183Returns an integer which is incremented every time a 184real local method in the package C<$classname> changes, 185or the local C<@ISA> of C<$classname> is modified. 186 187This is intended for authors of modules which do lots 188of class introspection, as it allows them to very quickly 189check if anything important about the local properties 190of a given class have changed since the last time they 191looked. It does not increment on method/C<@ISA> 192changes in superclasses. 193 194It's still up to you to seek out the actual changes, 195and there might not actually be any. Perhaps all 196of the changes since you last checked cancelled each 197other out and left the package in the state it was in 198before. 199 200This integer normally starts off at a value of C<1> 201when a package stash is instantiated. Calling it 202on packages whose stashes do not exist at all will 203return C<0>. If a package stash is completely 204deleted (not a normal occurrence, but it can happen 205if someone does something like C<undef %PkgName::>), 206the number will be reset to either C<0> or C<1>, 207depending on how completely the package was wiped out. 208 209=head2 next::method 210 211This is somewhat like C<SUPER>, but it uses the C3 method 212resolution order to get better consistency in multiple 213inheritance situations. Note that while inheritance in 214general follows whichever MRO is in effect for the 215given class, C<next::method> only uses the C3 MRO. 216 217One generally uses it like so: 218 219 sub some_method { 220 my $self = shift; 221 my $superclass_answer = $self->next::method(@_); 222 return $superclass_answer + 1; 223 } 224 225Note that you don't (re-)specify the method name. 226It forces you to always use the same method name 227as the method you started in. 228 229It can be called on an object or a class, of course. 230 231The way it resolves which actual method to call is: 232 233=over 4 234 235=item 1 236 237First, it determines the linearized C3 MRO of 238the object or class it is being called on. 239 240=item 2 241 242Then, it determines the class and method name 243of the context it was invoked from. 244 245=item 3 246 247Finally, it searches down the C3 MRO list until 248it reaches the contextually enclosing class, then 249searches further down the MRO list for the next 250method with the same name as the contextually 251enclosing method. 252 253=back 254 255Failure to find a next method will result in an 256exception being thrown (see below for alternatives). 257 258This is substantially different than the behavior 259of C<SUPER> under complex multiple inheritance. 260(This becomes obvious when one realizes that the 261common superclasses in the C3 linearizations of 262a given class and one of its parents will not 263always be ordered the same for both.) 264 265B<Caveat>: Calling C<next::method> from methods defined outside the class: 266 267There is an edge case when using C<next::method> from within a subroutine 268which was created in a different module than the one it is called from. It 269sounds complicated, but it really isn't. Here is an example which will not 270work correctly: 271 272 *Foo::foo = sub { (shift)->next::method(@_) }; 273 274The problem exists because the anonymous subroutine being assigned to the 275C<*Foo::foo> glob will show up in the call stack as being called 276C<__ANON__> and not C<foo> as you might expect. Since C<next::method> uses 277C<caller> to find the name of the method it was called in, it will fail in 278this case. 279 280But fear not, there's a simple solution. The module C<Sub::Name> will 281reach into the perl internals and assign a name to an anonymous subroutine 282for you. Simply do this: 283 284 use Sub::Name 'subname'; 285 *Foo::foo = subname 'Foo::foo' => sub { (shift)->next::method(@_) }; 286 287and things will Just Work. 288 289=head2 next::can 290 291This is similar to C<next::method>, but just returns either a code 292reference or C<undef> to indicate that no further methods of this name 293exist. 294 295=head2 maybe::next::method 296 297In simple cases, it is equivalent to: 298 299 $self->next::method(@_) if $self->next::can; 300 301But there are some cases where only this solution 302works (like C<goto &maybe::next::method>); 303 304=head1 SEE ALSO 305 306=head2 The original Dylan paper 307 308=over 4 309 310=item L<http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.19.3910&rep=rep1&type=pdf> 311 312=back 313 314=head2 Python 2.3 MRO 315 316=over 4 317 318=item L<https://www.python.org/download/releases/2.3/mro/> 319 320=back 321 322=head2 Class::C3 323 324=over 4 325 326=item L<Class::C3> 327 328=back 329 330=head1 AUTHOR 331 332Brandon L. Black, E<lt>blblack@gmail.comE<gt> 333 334Based on Stevan Little's L<Class::C3> 335 336=cut 337