O'Reilly logo

Mastering Perl/Tk by Nancy Walsh, Stephen Lidie

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Perl/Tk Class Hierarchy

Mega-widgets are hierarchical in nature. Base classes are combined to create new classes of ever greater functionality and sophistication.

The following statement creates a Label widget as a child of the MainWindow, $mw, and stores a reference to it in the Perl variable $l:

my $l = $mw->Label;

In object-oriented lingo, it instantiates (makes an instance of) an object of class Tk::Label. If we don’t know an object’s class, we can determine it using the ref function:

print "l = $l, class = ", ref $l, "\n";

l = Tk::Label=HASH(0x822b3d0), class = Tk::Label

We see that in reality, $l is a reference to a hash that has been blessed into the package Tk::Label. What we don’t know is where this class exists in the overall Tk class hierarchy. This useful bit of information not only tells us the path Perl follows when looking up object methods but also a widget’s class relationship with other widgets.

We can write a program that uses an HList widget (fully described in Chapter 18) to graph a depth-first traversal of any widget’s @ISA array. The program, isa, accepts a Tk class name as input via an Entry widget, then recursively calls isa_tree. This subroutine adds the class name to the HList tree, loads the module file so the symbol table is available, determines the module’s base classes by evaluating the new module’s @ISA array, and calls itself recursively as required.

use Tk; use Tk::widgets qw/HList/; use subs qw/isa_tree/; use strict; my $mw = MainWindow->new; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required