source: trunk/essentials/dev-lang/perl/lib/Class/ISA.pm@ 3184

Last change on this file since 3184 was 3181, checked in by bird, 19 years ago

perl 5.8.8

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