Class::ISA(3pm)
okay so bestie, welcome to Class::ISA(3pm) π this is a real page from the User Contributed Perl Documentation and it's lowkey iconic, so let's get into it π
πΈ NAME πΈ
Class::ISA β report the search path for a class's ISA tree β¨
π¦ SYNOPSIS π¦
# Suppose you go: use Food::Fishstick, and that uses and
# inherits from other things, which in turn use and inherit
# from other things. And suppose, for sake of brevity of
# example, that their ISA tree is the same as:
@Food::Fishstick::ISA = qw(Food::Fish Life::Fungus Chemicals);
@Food::Fish::ISA = qw(Food);
@Food::ISA = qw(Matter);
@Life::Fungus::ISA = qw(Life);
@Chemicals::ISA = qw(Matter);
@Life::ISA = qw(Matter);
@Matter::ISA = qw();
use Class::ISA;
print "Food::Fishstick path is:\n ",
join(", ", Class::ISA::super_path('Food::Fishstick')),
"\n";
and bestie, that prints this:
Food::Fishstick path is:
Food::Fish, Food, Matter, Life::Fungus, Life, Chemicals
π DESCRIPTION π
so picture this π you've got a class β say Food::Fish::Fishstick β that's derived, through its @ISA, from one or more superclasses (like how Food::Fish::Fishstick comes from Food::Fish, Life::Fungus, and Chemicals). and some of those superclasses might themselves each be derived, through their @ISA, from one or more superclasses (literally exactly like the example above). it's giving family tree π«Ά
so then when you call a method in that class ($fishstick->calories), Perl first searches there for that method, and if it's not there? it goes searching in its superclasses, and then in their superclasses, and so on forever β it's a depth-first search (or maybe "height-first" is the word, iykyk). in the example above, Perl would first look in Food::Fish, then Food, then Matter, then Life::Fungus, then Life, then Chemicals. that's the whole order, no skips π
this library, Class::ISA, hands you functions that return that exact list β the list, in order, of the names of the classes Perl would search to find a method, with no duplicates β¨
π₯ FUNCTIONS π₯
β¨ the function Class::ISA::super_path($CLASS)
this returns the ordered list of names of classes that Perl would search thru in order to find a method, with no duplicates in the list. note bestie: $CLASS is not included in the list. and UNIVERSAL is not included either β if you need to consider it, you gotta add it to the end yourself π
β¨ the function Class::ISA::self_and_super_path($CLASS)
literally just like super_path, except that $CLASS is included, as the very first element. main character energy, it goes first π
β¨ the function Class::ISA::self_and_super_versions($CLASS)
this returns a hash whose keys are $CLASS and its (super-)superclasses, and whose values are the contents of each class's $VERSION (or undef, for classes with no $VERSION). slay π
the code for self_and_super_versions is meant to serve as an example for precisely the kind of tasks the author anticipates that self_and_super_path and super_path will be used for. you are strongly advised to read the source for self_and_super_versions, and the comments there π
π CAUTIONARY NOTES π
Class::ISAdoesn't export anything. you have to address the functions with aClass::ISA::on the front. no shortcuts bestie π- contrary to its name,
Class::ISAisn't a class; it's just a package. strange, isn't it? π - say you have a loop in the ISA tree of the class you're calling one of the
Class::ISAfunctions on: say thatFoodinherits fromMatter, butMatterinherits fromFood(for sake of argument). if Perl, while searching for a method, actually discovers this cyclicity, it will throw a fatal error. the functions inClass::ISAeffectively ignore this cyclicity; theClass::ISAalgorithm is "never go down the same path twice", and cyclicities are just a special case of that π - the
Class::ISAfunctions just look at@ISAs. but theoretically, I suppose,AUTOLOADs could bypass Perl's ISA-based search mechanism and do whatever they please. that would be bad behavior, tho; and the author tries not to think about that π - if Perl can't find a method anywhere in the ISA tree, it then looks in the magical class
UNIVERSAL. this is rarely relevant to the tasks that the author expectsClass::ISAfunctions to be put to, but if it matters to you, then instead of this:
@supers = Class::Tree::super_path($class);
do this:
@supers = (Class::Tree::super_path($class), 'UNIVERSAL');
and don't say no-one ever told ya! π
- when you call them, the
Class::ISAfunctions look at@ISAs anew β that is, there is no memoization, and so if ISAs change during runtime, you get the current ISA tree's path, not anything memoized. however, changing ISAs at runtime is probably a sign that you're out of your mind! π
π COPYRIGHT AND LICENSE π
Copyright (c) 1999-2009 Sean M. Burke. All rights reserved.
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself β¨
π AUTHOR π
Sean M. Burke "sburke@cpan.org"
π MAINTAINER π
maintained by Steffen Mueller "smueller@cpan.org".
and that's the whole thing bestie, no cap π«Ά this page ships with perl v5.36.0 and is dated 2022-11-21 β so go forth and trace your ISA trees, you gorgeous nerd πΈπ¦β
π¦ where this page comes from
- debian bookworm: libclass-isa-perl 0.36-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libclass-isa-perl 0.36-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libclass-isa-perl 0.36-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libclass-isa-perl 0.36-7 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms