16.1. Inheritance
One of the most unusual, and least robust, aspects of Perl's OO mechanism is that each class keeps its inheritance hierarchy information in an ordinary package variable: @ISA. Apart from bringing along all the problems of package variables (see Chapter 5), this approach also means that Perl class hierarchies are typically set up by run-time assignments: package Superman; our @ISA = qw( Avian Agrarian Alien ); instead of by compile-time declarations. That arrangement can lead to very obscure compile-time bugs when objects are created and used before the run-time components of their class's code have been executed (for example, in a BEGIN block). So always define a class's hierarchy declaratively at compile time, using the standard use base pragma: package Superman; use base qw( Avian Agrarian Alien ); This ensures that the inheritance relationship is set up as early as possible, and also ensures that the necessary modules (e.g., Avian.pm, Agrarian.pm, Alien.pm) are automatically loaded for you. Better still, this approach discourages messing about with class hierarchies at run time, by reassigning @ISA. The temptation to modify @ISA at run time is usually a sign that your class might be better implemented as a factory, a façade, or with some other meta-object technique. |