This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Text::ParseWords 3.27
[perl5.git] / lib / mro.pm
index 5b02ab3..d4be79a 100644 (file)
@@ -9,14 +9,33 @@ package mro;
 use strict;
 use warnings;
 
-# mro.pm versions < 1.00 reserved for possible CPAN mro dist
-#  (for partial back-compat to 5.[68].x)
+# mro.pm versions < 1.00 reserved for MRO::Compat
+#  for partial back-compat to 5.[68].x
 our $VERSION = '1.00';
 
 sub import {
     mro::set_mro(scalar(caller), $_[1]) if $_[1];
 }
 
+package # hide me from PAUSE
+    next;
+
+sub can { mro::_nextcan($_[0], 0) }
+
+sub method {
+    my $method = mro::_nextcan($_[0], 1);
+    goto &$method;
+}
+
+package # hide me from PAUSE
+    maybe::next;
+
+sub method {
+    my $method = mro::_nextcan($_[0], 0);
+    goto &$method if defined $method;
+    return;
+}
+
 1;
 
 __END__
@@ -27,6 +46,8 @@ mro - Method Resolution Order
 
 =head1 SYNOPSIS
 
+  use mro; # enables next::method and friends globally
+
   use mro 'dfs'; # enable DFS MRO for this class (Perl default)
   use mro 'c3'; # enable C3 MRO for this class
 
@@ -35,13 +56,20 @@ mro - Method Resolution Order
 The "mro" namespace provides several utilities for dealing
 with method resolution order and method caching in general.
 
+These interfaces are only available in Perl 5.9.5 and higher.
+See L<MRO::Compat> on CPAN for a mostly forwards compatible
+implementation for older Perls.
+
 =head1 OVERVIEW
 
 It's possible to change the MRO of a given class either by using C<use
 mro> as shown in the synopsis, or by using the L</mro::set_mro> function
-below.  The functions do not require loading the C<mro> module, as they
-are actually provided by the core perl interpreter.  The C<use mro> syntax
-is just syntactic sugar for setting the current package's MRO.
+below.  The functions in the mro namespace do not require loading the
+C<mro> module, as they are actually provided by the core perl interpreter.
+
+The special methods C<next::method>, C<next::can>, and
+C<maybe::next::method> are not available until this C<mro> module
+has been loaded via C<use> or C<require>.
 
 =head1 The C3 MRO
 
@@ -89,6 +117,13 @@ Returns an arrayref which is the linearized MRO of the given class.
 Uses whichever MRO is currently in effect for that class by default,
 or the given MRO (either C<c3> or C<dfs> if specified as C<$type>).
 
+The linearized MRO of a class is an ordered array of all of the
+classes one would search when resolving a method on that class,
+starting with the class itself.
+
+If the requested class doesn't yet exist, this function will still
+succeed, and return C<[ $classname ]>
+
 Note that C<UNIVERSAL> (and any members of C<UNIVERSAL>'s MRO) are not
 part of the MRO of a class, even though all classes implicitly inherit
 methods from C<UNIVERSAL> and its parents.
@@ -105,7 +140,7 @@ Returns the MRO of the given class (either C<c3> or C<dfs>).
 =head2 mro::get_isarev($classname)
 
 Gets the C<mro_isarev> for this class, returned as an
-array of class names.  These are every class that "isa"
+arrayref of class names.  These are every class that "isa"
 the given class name, even if the isa relationship is
 indirect.  This is used internally by the MRO code to
 keep track of method/MRO cache invalidations.
@@ -141,29 +176,49 @@ For similar reasons to C<isarev> above, this flag is
 permanent.  Once it is set, it does not go away, even
 if the class in question really isn't universal anymore.
 
-=head2 mro::get_global_sub_generation()
-
-Returns the current value of the internal perl variable
-C<PL_sub_generation>.
-
 =head2 mro::invalidate_all_method_caches()
 
 Increments C<PL_sub_generation>, which invalidates method
 caching in all packages.
 
-=head2 mro::get_sub_generation($classname)
-
-Returns the current value of a given package's C<sub_generation>.
-This is only incremented when necessary for that package.
-
-If one is trying to determine whether significant (method/cache-affecting)
-changes have occured for a given stash since you last checked, you should
-check both this and the global one above.
-
 =head2 mro::method_changed_in($classname)
 
 Invalidates the method cache of any classes dependent on the
-given class.
+given class.  This is not normally necessary.  The only
+known case where pure perl code can confuse the method
+cache is when you manually install a new constant
+subroutine by using a readonly scalar value, like the
+internals of L<constant> do.  If you find another case,
+please report it so we can either fix it or document
+the exception here.
+
+=head2 mro::get_pkg_gen($classname)
+
+Returns an integer which is incremented every time a
+real local method in the package C<$classname> changes,
+or the local C<@ISA> of C<$classname> is modified.
+
+This is intended for authors of modules which do lots
+of class introspection, as it allows them to very quickly
+check if anything important about the local properties
+of a given class have changed since the last time they
+looked.  It does not increment on method/C<@ISA>
+changes in superclasses.
+
+It's still up to you to seek out the actual changes,
+and there might not actually be any.  Perhaps all
+of the changes since you last checked cancelled each
+other out and left the package in the state it was in
+before.
+
+This integer normally starts off at a value of C<1>
+when a package stash is instantiated.  Calling it
+on packages whose stashes do not exist at all will
+return C<0>.  If a package stash is completely
+deleted (not a normal occurence, but it can happen
+if someone does something like C<undef %PkgName::>),
+the number will be reset to either C<0> or C<1>,
+depending on how completely package was wiped out.
 
 =head2 next::method
 
@@ -255,7 +310,7 @@ exist.
 
 In simple cases, it is equivalent to:
 
-   $self->next::method(@_) if $self->next_can;
+   $self->next::method(@_) if $self->next::can;
 
 But there are some cases where only this solution
 works (like C<goto &maybe::next::method>);