Mega-Widget Implementation Details

Once again, briefly, here’s the basic structure of a Perl/Tk mega-widget, but this time using a derived NavListbox widget (described in Section 14.5) as the model:

 1   package Tk::NavListbox;
 2 
 3   use vars qw($VERSION);
 4   $VERSION = '1.0';
 5 
 6   use Tk::widgets qw(Listbox Dialog);
 7   use base qw(Tk::Derived Tk::Listbox);
 8   use strict;
 9 
10   Construct Tk::Widget 'NavListbox';
11 
12   sub ClassInit {}
13   sub Populate {}
14 
15   1;

Line 1 declares the widget’s class name.

Lines 3 and 4 show another way of specifying the module’s version number.

Line 6 concisely declares the widgets used by the module.

Line 7 is the signature line of a derived mega-widget, because the base class list starts with Tk::Derived and includes another Tk widget class. Tk::Derived provides all the option specification and configuration, and method delegation support methods. A composite mega-widget would list a single base class, either Tk::Toplevel or Tk::Frame. As Figure 14-4 shows, composite mega-widgets need not include Tk::Derived in their @ISA array, because Tk::Derived is a base class of Tk::Frame.

Line 10, also written as Tk::Widget->Construct('NavListbox'), creates a constructor named NavListbox in the class Tk::Widget. When the user types:

            $nlb = $mw->NavListbox;

Perl eventually finds Tk::Widget::NavListbox via MainWindow’s @ISA array. This constructor, like all Perl/Tk widget constructors, then calls Tk::Widget::new (described next) to actually create the widget.

Lines 12 ...

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.