| 1 | #!/usr/local/bin/perl
|
|---|
| 2 | # Time-stamp: "2004-12-29 20:01:02 AST" -*-Perl-*-
|
|---|
| 3 |
|
|---|
| 4 | package Class::ISA;
|
|---|
| 5 | require 5;
|
|---|
| 6 | use strict;
|
|---|
| 7 | use vars qw($Debug $VERSION);
|
|---|
| 8 | $VERSION = '0.33';
|
|---|
| 9 | $Debug = 0 unless defined $Debug;
|
|---|
| 10 |
|
|---|
| 11 | =head1 NAME
|
|---|
| 12 |
|
|---|
| 13 | Class::ISA -- report the search path for a class's ISA tree
|
|---|
| 14 |
|
|---|
| 15 | =head1 SYNOPSIS
|
|---|
| 16 |
|
|---|
| 17 | # Suppose you go: use Food::Fishstick, and that uses and
|
|---|
| 18 | # inherits from other things, which in turn use and inherit
|
|---|
| 19 | # from other things. And suppose, for sake of brevity of
|
|---|
| 20 | # example, that their ISA tree is the same as:
|
|---|
| 21 |
|
|---|
| 22 | @Food::Fishstick::ISA = qw(Food::Fish Life::Fungus Chemicals);
|
|---|
| 23 | @Food::Fish::ISA = qw(Food);
|
|---|
| 24 | @Food::ISA = qw(Matter);
|
|---|
| 25 | @Life::Fungus::ISA = qw(Life);
|
|---|
| 26 | @Chemicals::ISA = qw(Matter);
|
|---|
| 27 | @Life::ISA = qw(Matter);
|
|---|
| 28 | @Matter::ISA = qw();
|
|---|
| 29 |
|
|---|
| 30 | use Class::ISA;
|
|---|
| 31 | print "Food::Fishstick path is:\n ",
|
|---|
| 32 | join(", ", Class::ISA::super_path('Food::Fishstick')),
|
|---|
| 33 | "\n";
|
|---|
| 34 |
|
|---|
| 35 | That prints:
|
|---|
| 36 |
|
|---|
| 37 | Food::Fishstick path is:
|
|---|
| 38 | Food::Fish, Food, Matter, Life::Fungus, Life, Chemicals
|
|---|
| 39 |
|
|---|
| 40 | =head1 DESCRIPTION
|
|---|
| 41 |
|
|---|
| 42 | Suppose you have a class (like Food::Fish::Fishstick) that is derived,
|
|---|
| 43 | via its @ISA, from one or more superclasses (as Food::Fish::Fishstick
|
|---|
| 44 | is from Food::Fish, Life::Fungus, and Chemicals), and some of those
|
|---|
| 45 | superclasses may themselves each be derived, via its @ISA, from one or
|
|---|
| 46 | more superclasses (as above).
|
|---|
| 47 |
|
|---|
| 48 | When, then, you call a method in that class ($fishstick->calories),
|
|---|
| 49 | Perl first searches there for that method, but if it's not there, it
|
|---|
| 50 | goes searching in its superclasses, and so on, in a depth-first (or
|
|---|
| 51 | maybe "height-first" is the word) search. In the above example, it'd
|
|---|
| 52 | first look in Food::Fish, then Food, then Matter, then Life::Fungus,
|
|---|
| 53 | then Life, then Chemicals.
|
|---|
| 54 |
|
|---|
| 55 | This library, Class::ISA, provides functions that return that list --
|
|---|
| 56 | the list (in order) of names of classes Perl would search to find a
|
|---|
| 57 | method, with no duplicates.
|
|---|
| 58 |
|
|---|
| 59 | =head1 FUNCTIONS
|
|---|
| 60 |
|
|---|
| 61 | =over
|
|---|
| 62 |
|
|---|
| 63 | =item the function Class::ISA::super_path($CLASS)
|
|---|
| 64 |
|
|---|
| 65 | This returns the ordered list of names of classes that Perl would
|
|---|
| 66 | search thru in order to find a method, with no duplicates in the list.
|
|---|
| 67 | $CLASS is not included in the list. UNIVERSAL is not included -- if
|
|---|
| 68 | you need to consider it, add it to the end.
|
|---|
| 69 |
|
|---|
| 70 |
|
|---|
| 71 | =item the function Class::ISA::self_and_super_path($CLASS)
|
|---|
| 72 |
|
|---|
| 73 | Just like C<super_path>, except that $CLASS is included as the first
|
|---|
| 74 | element.
|
|---|
| 75 |
|
|---|
| 76 | =item the function Class::ISA::self_and_super_versions($CLASS)
|
|---|
| 77 |
|
|---|
| 78 | This returns a hash whose keys are $CLASS and its
|
|---|
| 79 | (super-)superclasses, and whose values are the contents of each
|
|---|
| 80 | class's $VERSION (or undef, for classes with no $VERSION).
|
|---|
| 81 |
|
|---|
| 82 | The code for self_and_super_versions is meant to serve as an example
|
|---|
| 83 | for precisely the kind of tasks I anticipate that self_and_super_path
|
|---|
| 84 | and super_path will be used for. You are strongly advised to read the
|
|---|
| 85 | source for self_and_super_versions, and the comments there.
|
|---|
| 86 |
|
|---|
| 87 | =back
|
|---|
| 88 |
|
|---|
| 89 | =head1 CAUTIONARY NOTES
|
|---|
| 90 |
|
|---|
| 91 | * Class::ISA doesn't export anything. You have to address the
|
|---|
| 92 | functions with a "Class::ISA::" on the front.
|
|---|
| 93 |
|
|---|
| 94 | * Contrary to its name, Class::ISA isn't a class; it's just a package.
|
|---|
| 95 | Strange, isn't it?
|
|---|
| 96 |
|
|---|
| 97 | * Say you have a loop in the ISA tree of the class you're calling one
|
|---|
| 98 | of the Class::ISA functions on: say that Food inherits from Matter,
|
|---|
| 99 | but Matter inherits from Food (for sake of argument). If Perl, while
|
|---|
| 100 | searching for a method, actually discovers this cyclicity, it will
|
|---|
| 101 | throw a fatal error. The functions in Class::ISA effectively ignore
|
|---|
| 102 | this cyclicity; the Class::ISA algorithm is "never go down the same
|
|---|
| 103 | path twice", and cyclicities are just a special case of that.
|
|---|
| 104 |
|
|---|
| 105 | * The Class::ISA functions just look at @ISAs. But theoretically, I
|
|---|
|
|---|