# Keep this < 1.00, so people can tell the fake
# mro.pm from the real one
-our $VERSION = '0.03';
+our $VERSION = '0.12';
BEGIN {
# Alias our private functions over to
if($] < 5.009_005) {
$mro::VERSION # to fool Module::Install when generating META.yml
= $VERSION;
- $INC{'mro.pm'} = 'Faked by MRO::Compat';
+ $INC{'mro.pm'} = __FILE__;
*mro::import = \&__import;
*mro::get_linear_isa = \&__get_linear_isa;
*mro::set_mro = \&__set_mro;
=head1 SYNOPSIS
+ package PPP; use base qw/Exporter/;
+ package X; use base qw/PPP/;
+ package Y; use base qw/PPP/;
+ package Z; use base qw/PPP/;
+
package FooClass; use base qw/X Y Z/;
- package X; use base qw/ZZZ/;
- package Y; use base qw/ZZZ/;
- package Z; use base qw/ZZZ/;
package main;
use MRO::Compat;
my $linear = mro::get_linear_isa('FooClass');
print join(q{, }, @$linear);
- # Prints: "FooClass, X, ZZZ, Y, Z"
+ # Prints: FooClass, X, PPP, Exporter, Y, Z
=head1 DESCRIPTION
This module provides those interfaces for
earlier versions of Perl (back to 5.6.0 anyways).
-It is a harmless no-op to use this module on 5.9.5+. If
-you're writing a piece of software that would like to use
+It is a harmless no-op to use this module on 5.9.5+. That
+is to say, code which properly uses L<MRO::Compat> will work
+unmodified on both older Perls and 5.9.5+.
+
+If you're writing a piece of software that would like to use
the parts of 5.9.5+'s mro:: interfaces that are supported
here, and you want compatibility with older Perls, this
is the module for you.
-Some parts of this interface will work better with
-L<Class::C3::XS> installed, but it's not a requirement.
+Some parts of this code will work better and/or faster with
+L<Class::C3::XS> installed (which is an optional prereq
+of L<Class::C3>, which is in turn a prereq of this
+package), but it's not a requirement.
This module never exports any functions. All calls must
be fully qualified with the C<mro::> prefix.
look out for. The main docs in 5.9.5's L<mro> are the real
interface docs, and contain a lot of other useful information.
-=head1 VERSION 0.02
-
-This is the first release of this new module, and on top of that,
-the Perl 5.9.5 it seeks to provide compatibility with isn't even
-out yet.
-
-If you're going to use/depend on this, please keep abreast of
-possible interface changes in the next few versions. Once Perl
-5.9.5 is out the door the interfaces should stabilize on whatever
-5.9.5 has to offer. In the meantime, don't be surprised if
-L<MRO::Compat> and 5.9.5's interfaces aren't perfectly in sync
-at all times.
-
=head1 Functions
=head2 mro::get_linear_isa($classname[, $type])
-Returns an arrayref which is the linearized MRO of the given class.
+Returns an arrayref which is the linearized "ISA" 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 a single ordered list of all of the
+The linearized ISA of a class is a single ordered list of all of the
classes that would be visited in the process of resolving a method
on the given class, starting with itself. It does not include any
duplicate entries.
sub __get_linear_isa {
my ($classname, $type) = @_;
- die "mro::get_mro requires a classname" if !$classname;
+ die "mro::get_mro requires a classname" if !defined $classname;
$type ||= __get_mro($classname);
if($type eq 'dfs') {
sub __set_mro {
my ($classname, $type) = @_;
- if(!$classname || !$type) {
+
+ if(!defined $classname || !$type) {
die q{Usage: mro::set_mro($classname, $type)};
}
+
if($type eq 'c3') {
eval "package $classname; use Class::C3";
die $@ if $@;
}
- if($type ne 'dfs') {
- die q{Invalid mro type "$type"};
+ elsif($type eq 'dfs') {
+ # In the dfs case, check whether we need to undo C3
+ if(defined $Class::C3::MRO{$classname}) {
+ Class::C3::_remove_method_dispatch_table($classname);
+ }
+ delete $Class::C3::MRO{$classname};
}
-
- # In the dfs case, check whether we need to undo C3
- if(defined $Class::C3::MRO{$classname}) {
- Class::C3::_remove_method_dispatch_table($classname);
+ else {
+ die qq{Invalid mro type "$type"};
}
- delete $Class::C3::MRO{$classname};
return;
}
sub __get_mro {
my $classname = shift;
- die "mro::get_mro requires a classname" if !$classname;
+ die "mro::get_mro requires a classname" if !defined $classname;
return 'c3' if exists $Class::C3::MRO{$classname};
return 'dfs';
}
=head2 mro::get_isarev($classname)
Returns an arrayref of classes who are subclasses of the
-given classname. In other words, classes who we exist,
-however indirectly, in the @ISA inheritancy hierarchy of.
+given classname. In other words, classes in whose @ISA
+hierarchy we appear, no matter how indirectly.
This is much slower on pre-5.9.5 Perls with MRO::Compat
than it is on 5.9.5+, as it has to search the entire
my $search = shift;
my $pfx;
my $isa;
- if($search) {
+ if(defined $search) {
$isa = \@{"$search\::ISA"};
$pfx = "$search\::";
}
push(@retval, $search) if scalar(@$isa);
foreach my $cand (keys %{"$search\::"}) {
- if($cand =~ /::$/) {
- $cand =~ s/::$//;
+ if($cand =~ s/::$//) {
next if $cand eq $search; # skip self-reference (main?)
push(@retval, @{__get_all_pkgs_with_isas($pfx . $cand)});
}
sub __get_isarev {
my $classname = shift;
- die "mro::get_isarev requires a classname" if !$classname;
+ die "mro::get_isarev requires a classname" if !defined $classname;
__get_isarev_recurse($classname, __get_all_pkgs_with_isas(), 0);
}
sub __is_universal {
my $classname = shift;
- die "mro::is_universal requires a classname" if !$classname;
+ die "mro::is_universal requires a classname" if !defined $classname;
my $lin = __get_linear_isa('UNIVERSAL');
foreach (@$lin) {
sub __invalidate_all_method_caches {
# Super secret mystery code :)
- @fedcba98::ISA = @fedcba98::ISA;
+ @f845a9c1ac41be33::ISA = @f845a9c1ac41be33::ISA;
return;
}
sub __method_changed_in {
my $classname = shift;
- die "mro::method_changed_in requires a classname" if !$classname;
+ die "mro::method_changed_in requires a classname" if !defined $classname;
__invalidate_all_method_caches();
}
=cut
-my $__pkg_gen = 2;
-sub __get_pkg_gen_pp {
- my $classname = shift;
- die "mro::get_pkg_gen requires a classname" if !$classname;
- return $__pkg_gen++;
+{
+ my $__pkg_gen = 2;
+ sub __get_pkg_gen_pp {
+ my $classname = shift;
+ die "mro::get_pkg_gen requires a classname" if !defined $classname;
+ return $__pkg_gen++;
+ }
}
sub __get_pkg_gen_c3xs {
my $classname = shift;
- die "mro::get_pkg_gen requires a classname" if !$classname;
+ die "mro::get_pkg_gen requires a classname" if !defined $classname;
return Class::C3::XS::_plsubgen();
}
to L<Class::C3>.
It does not remove the need for you to call
-L<Class::C3>'s C<initialize()>, C<reinitialize()>, and/or
-C<uninitialize()> at the appropriate times
-as documented in the L<Class::C3> docs.
-
-Because L<MRO::Compat> has L<Class::C3> as a pre-requisite,
-and requires it at C<use> time, you can blindly call
-those functions in code that uses L<MRO::Compat>.
-Under 5.9.5+ with L<MRO::Compat>, your calls to those
-functions will become a no-op and everything will work fine.
+C<Class::C3::initialize()>, C<Class::C3::reinitialize()>, and/or
+C<Class::C3::uninitialize()> at the appropriate times
+as documented in the L<Class::C3> docs. These three functions
+are always provided by L<MRO::Compat>, either via L<Class::C3>
+itself on older Perls, or directly as no-ops on 5.9.5+.
=head1 SEE ALSO
=head1 COPYRIGHT AND LICENSE
-Copyright 2007 Brandon L. Black E<lt>blblack@gmail.comE<gt>
+Copyright 2007-2008 Brandon L. Black E<lt>blblack@gmail.comE<gt>
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.