1N/A#!/usr/local/bin/perl
1N/A# Time-stamp: "2000-05-13 20:03:22 MDT" -*-Perl-*-
1N/A
1N/Apackage Class::ISA;
1N/Arequire 5;
1N/Ause strict;
1N/Ause vars qw($Debug $VERSION);
1N/A$VERSION = 0.32;
1N/A$Debug = 0 unless defined $Debug;
1N/A
1N/A=head1 NAME
1N/A
1N/AClass::ISA -- report the search path for a class's ISA tree
1N/A
1N/A=head1 SYNOPSIS
1N/A
1N/A # Suppose you go: use Food::Fishstick, and that uses and
1N/A # inherits from other things, which in turn use and inherit
1N/A # from other things. And suppose, for sake of brevity of
1N/A # example, that their ISA tree is the same as:
1N/A
1N/A @Food::Fishstick::ISA = qw(Food::Fish Life::Fungus Chemicals);
1N/A @Food::Fish::ISA = qw(Food);
1N/A @Food::ISA = qw(Matter);
1N/A @Life::Fungus::ISA = qw(Life);
1N/A @Chemicals::ISA = qw(Matter);
1N/A @Life::ISA = qw(Matter);
1N/A @Matter::ISA = qw();
1N/A
1N/A use Class::ISA;
1N/A print "Food::Fishstick path is:\n ",
1N/A join(", ", Class::ISA::super_path('Food::Fishstick')),
1N/A "\n";
1N/A
1N/AThat prints:
1N/A
1N/A Food::Fishstick path is:
1N/A Food::Fish, Food, Matter, Life::Fungus, Life, Chemicals
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/ASuppose you have a class (like Food::Fish::Fishstick) that is derived,
1N/Avia its @ISA, from one or more superclasses (as Food::Fish::Fishstick
1N/Ais from Food::Fish, Life::Fungus, and Chemicals), and some of those
1N/Asuperclasses may themselves each be derived, via its @ISA, from one or
1N/Amore superclasses (as above).
1N/A
1N/AWhen, then, you call a method in that class ($fishstick->calories),
1N/APerl first searches there for that method, but if it's not there, it
1N/Agoes searching in its superclasses, and so on, in a depth-first (or
1N/Amaybe "height-first" is the word) search. In the above example, it'd
1N/Afirst look in Food::Fish, then Food, then Matter, then Life::Fungus,
1N/Athen Life, then Chemicals.
1N/A
1N/AThis library, Class::ISA, provides functions that return that list --
1N/Athe list (in order) of names of classes Perl would search to find a
1N/Amethod, with no duplicates.
1N/A
1N/A=head1 FUNCTIONS
1N/A
1N/A=over
1N/A
1N/A=item the function Class::ISA::super_path($CLASS)
1N/A
1N/AThis returns the ordered list of names of classes that Perl would
1N/Asearch thru in order to find a method, with no duplicates in the list.
1N/A$CLASS is not included in the list. UNIVERSAL is not included -- if
1N/Ayou need to consider it, add it to the end.
1N/A
1N/A
1N/A=item the function Class::ISA::self_and_super_path($CLASS)
1N/A
1N/AJust like C<super_path>, except that $CLASS is included as the first
1N/Aelement.
1N/A
1N/A=item the function Class::ISA::self_and_super_versions($CLASS)
1N/A
1N/AThis returns a hash whose keys are $CLASS and its
1N/A(super-)superclasses, and whose values are the contents of each
1N/Aclass's $VERSION (or undef, for classes with no $VERSION).
1N/A
1N/AThe code for self_and_super_versions is meant to serve as an example
1N/Afor precisely the kind of tasks I anticipate that self_and_super_path
1N/Aand super_path will be used for. You are strongly advised to read the
1N/Asource for self_and_super_versions, and the comments there.
1N/A
1N/A=back
1N/A
1N/A=head1 CAUTIONARY NOTES
1N/A
1N/A* Class::ISA doesn't export anything. You have to address the
1N/Afunctions with a "Class::ISA::" on the front.
1N/A
1N/A* Contrary to its name, Class::ISA isn't a class; it's just a package.
1N/AStrange, isn't it?
1N/A
1N/A* Say you have a loop in the ISA tree of the class you're calling one
1N/Aof the Class::ISA functions on: say that Food inherits from Matter,
1N/Abut Matter inherits from Food (for sake of argument). If Perl, while
1N/Asearching for a method, actually discovers this cyclicity, it will
1N/Athrow a fatal error. The functions in Class::ISA effectively ignore
1N/Athis cyclicity; the Class::ISA algorithm is "never go down the same
1N/Apath twice", and cyclicities are just a special case of that.
1N/A
1N/A* The Class::ISA functions just look at @ISAs. But theoretically, I
1N/Asuppose, AUTOLOADs could bypass Perl's ISA-based search mechanism and
1N/Ado whatever they please. That would be bad behavior, tho; and I try
1N/Anot to think about that.
1N/A
1N/A* If Perl can't find a method anywhere in the ISA tree, it then looks
1N/Ain the magical class UNIVERSAL. This is rarely relevant to the tasks
1N/Athat I expect Class::ISA functions to be put to, but if it matters to
1N/Ayou, then instead of this:
1N/A
1N/A @supers = Class::Tree::super_path($class);
1N/A
1N/Ado this:
1N/A
1N/A @supers = (Class::Tree::super_path($class), 'UNIVERSAL');
1N/A
1N/AAnd don't say no-one ever told ya!
1N/A
1N/A* When you call them, the Class::ISA functions look at @ISAs anew --
1N/Athat is, there is no memoization, and so if ISAs change during
1N/Aruntime, you get the current ISA tree's path, not anything memoized.
1N/AHowever, changing ISAs at runtime is probably a sign that you're out
1N/Aof your mind!
1N/A
1N/A=head1 COPYRIGHT
1N/A
1N/ACopyright (c) 1999, 2000 Sean M. Burke. All rights reserved.
1N/A
1N/AThis library is free software; you can redistribute it and/or modify
1N/Ait under the same terms as Perl itself.
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ASean M. Burke C<sburke@cpan.org>
1N/A
1N/A=cut
1N/A
1N/A###########################################################################
1N/A
1N/Asub self_and_super_versions {
1N/A no strict 'refs';
1N/A map {
1N/A $_ => (defined(${"$_\::VERSION"}) ? ${"$_\::VERSION"} : undef)
1N/A } self_and_super_path($_[0])
1N/A}
1N/A
1N/A# Also consider magic like:
1N/A# no strict 'refs';
1N/A# my %class2SomeHashr =
1N/A# map { defined(%{"$_\::SomeHash"}) ? ($_ => \%{"$_\::SomeHash"}) : () }
1N/A# Class::ISA::self_and_super_path($class);
1N/A# to get a hash of refs to all the defined (and non-empty) hashes in
1N/A# $class and its superclasses.
1N/A#
1N/A# Or even consider this incantation for doing something like hash-data
1N/A# inheritance:
1N/A# no strict 'refs';
1N/A# %union_hash =
1N/A# map { defined(%{"$_\::SomeHash"}) ? %{"$_\::SomeHash"}) : () }
1N/A# reverse(Class::ISA::self_and_super_path($class));
1N/A# Consider that reverse() is necessary because with
1N/A# %foo = ('a', 'wun', 'b', 'tiw', 'a', 'foist');
1N/A# $foo{'a'} is 'foist', not 'wun'.
1N/A
1N/A###########################################################################
1N/Asub super_path {
1N/A my @ret = &self_and_super_path(@_);
1N/A shift @ret if @ret;
1N/A return @ret;
1N/A}
1N/A
1N/A#--------------------------------------------------------------------------
1N/Asub self_and_super_path {
1N/A # Assumption: searching is depth-first.
1N/A # Assumption: '' (empty string) can't be a class package name.
1N/A # Note: 'UNIVERSAL' is not given any special treatment.
1N/A return () unless @_;
1N/A
1N/A my @out = ();
1N/A
1N/A my @in_stack = ($_[0]);
1N/A my %seen = ($_[0] => 1);
1N/A
1N/A my $current;
1N/A while(@in_stack) {
1N/A next unless defined($current = shift @in_stack) && length($current);
1N/A print "At $current\n" if $Debug;
1N/A push @out, $current;
1N/A no strict 'refs';
1N/A unshift @in_stack,
1N/A map
1N/A { my $c = $_; # copy, to avoid being destructive
1N/A substr($c,0,2) = "main::" if substr($c,0,2) eq '::';
1N/A # Canonize the :: -> main::, ::foo -> main::foo thing.
1N/A # Should I ever canonize the Foo'Bar = Foo::Bar thing?
1N/A $seen{$c}++ ? () : $c;
1N/A }
1N/A @{"$current\::ISA"}
1N/A ;
1N/A # I.e., if this class has any parents (at least, ones I've never seen
1N/A # before), push them, in order, onto the stack of classes I need to
1N/A # explore.
1N/A }
1N/A
1N/A return @out;
1N/A}
1N/A#--------------------------------------------------------------------------
1N/A1;
1N/A
1N/A__END__