home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeMastering Perl/TkSearch this book

Chapter 1. Hello, Perl/Tk

Tk is a module that gives you the ability to create graphical interfaces with Perl. Most Perl programs are written with command-line interfaces, which can be cumbersome and intimidating to end users. Perl/Tk lets you communicate with buttons, menus, dialog boxes, scrolled text areas, and so on—all the features you need to develop simple or sophisticated GUI applications.

Why use a graphical interface? In the course of your programming experience, you've probably come across situations in which a text-based interface was insufficient for your needs, if not downright awkward. Certain applications can run with no input, but others, such as installation scripts, require the user to feed information to them constantly. They ask such questions as: Do you want to install this file? Can I overwrite this DLL? Do you want to create this directory? Do you want the help files?

A graphical user interface (GUI) adds a little flair and professionalism to an application. Here are some examples of good uses for a GUI:

  • A mini web client that connects to a dictionary server

  • An application that displays a map in a scrollable window

  • A program that interfaces with a database and displays query results in several widgets, with labels to describe the data

  • A mail reader that interfaces with your inbox and can also send out mail messages

A GUI can also be helpful when your boss just says "make it easy to use!," which usually means either adding a wrapper around a script or an interface that makes it easy for users to understand the decisions they have to make.

But don't take this to mean that you should start adding GUIs to all your Perl scripts. There are times when it would be overkill to add a GUI to a script. If all you are doing is reading one file, munging a bit with no user input, and generating another file, a GUI would be silly and unnecessary. GUIs work best when you require a lot of decisions and input from the user, such as in the installation scenario mentioned earlier.

1.1. Perl/Tk Concepts

Perl/Tk programs are written in an object-oriented (OO) style, but you don't need previous Perl object-oriented programming experience to code in Perl/Tk. You'll pick it up easily enough after seeing the first few examples. In a nutshell, Perl/Tk widgets (such as Buttons and Listboxes) are objects that have methods we invoke to control them. Besides widgets, Perl/Tk has images, which are also objects, and fonts, which can be objects or simple strings.

A Perl/Tk program is composed of a hierarchy of widgets. At the top of the hierarchy is the MainWindow, the parent widget for all other widgets in the application. The MainWindow widget acts as a container, within which we arrange child widgets using a geometry manager. The widget hierarchy is important for several reasons. Among other things, it's used by geometry managers to control the screen layout and the menu system to arrange menu items.

Each different widget belongs to a class. A widget's class defines its initial appearance and behavior, but individual widgets of the same class can be customized. As an example, you might create two Buttons that have different textual labels but are otherwise identical. Sometimes you'll read about instantiating a widget. This is simply OO-speak for creating a widget (a widget instance). The class constructor is responsible for creating widget instances.

The class also defines a widget's initial behavior by creating bindings. A binding associates an event such as a button press with a callback, which is a subroutine that handles the event. You can add additional bindings (indeed, even change and remove them) to alter a widget's standard behavior. Callbacks have several formats, but we mostly use simple references to Perl subroutines.

You'll learn all about these topics as you continue reading.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.