Tải bản đầy đủ (.pdf) (14 trang)

Web Client Programming with Perl-Chapter 7: Graphical Examples with Perl/Tk- P1

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (44.62 KB, 14 trang )

Chapter 7: Graphical Examples with Perl/Tk- P1
The Tk extension to Perl can be used to create a Graphical User Interface
(GUI) to your Perl programs on UNIX. Why would you want to do this?
Several reasons, such as ease of use, or to be able to display HTML nicely.
Instead of just writing a "cool script," you could go as far as writing your
own custom browser.
In this chapter, we show a few examples of Tk-based web clients, which go
beyond the command-line interface that we've been using so far in this
book:[1]
 xword, a dictionary client
 track, a graphical version of the FedEx example shown in Chapter 6.
 webping, an at-a-glance display of the status of multiple web servers
One caveat about Tk, and it's a serious one. At this writing, the Tk module to
Perl (also known as pTk) only runs on UNIX machines with the X Window
System. While the Tk extension to the Tcl language has been successfully
ported to Microsoft Windows, the Perl port is still pending, although it is
rumored to be in the works.
Still, even with its limited availability, we think the ability to give your
programs an easy-to-use graphical interface is important enough to devote a
chapter to it. And who knows--by the time you're reading this, the pTk port
to Windows might already be completed, and this whole paragraph may be
moot.
A Brief Introduction to Tk
Tk was originally developed by John Ousterhout as an extension to his Tcl
language, for providing a graphical user interface for the X Window System.
It was ported to Perl soon afterwards; Nick Ing-Simmons did most of the
work to make it functional as a module with Perl. You can get Tk from any
CPAN archive (
The Tk extension provides an easy way to draw a window, put widgets into
it (such as buttons, check boxes, entry fields, menus, etc.), and have them
perform certain actions based on user input. A simple "Hello World"


program would look like this:
1 #!/usr/bin/perl -w
2 use Tk;
3 my $mw = MainWindow->new;
4 $mw->Button(-text => "Hello World!", -command
=>sub{exit})->pack;
5 MainLoop;
(The line numbers are not part of the actual code; they are just included for
ease in reference.)
When you run it, it would look like Figure 7-1.
Figure 7-1. A simple Tk widget

Pushing the "Hello World" button will exit the program, and your window
will then go away. Line 1 tells the shell to invoke Perl to interpret the rest of
the file, and Line 2 then tells Perl that we need to use the Tk module. Line 3
tells the system that you want it to build you a generic, standard window.
Line 4 creates a button, displays it (using the pack method), and gives the
button something to do when pushed.
Line 5 tells the program to "go do it." MainLoop kicks off the event handler
for the graphical interface. The most important concept to understand with
Perl/Tk is that the program won't do a single thing until it hits the MainLoop
statement. You won't see any graphical output at all until then. We prepare it
by telling it what we want to draw, and what should happen when certain
events happen, such as a mouse click on our button in our "Hello World"
program. The more complex the things you want the GUI to do, the more
complex the code looks for setting it up.
Since the purpose of this chapter is to show some examples using Tk and to
interact with the WWW, we won't be going into much more detail about
what Tk does and why. Some places you might look for help are the
newsgroup comp.lang.perl.tk for Perk/Tk-specific questions, or the Perl/Tk

FAQ at Any search site
will point you to at least 30 web sites as well. And of course the Tk source
includes "pod" documentation: run pod2text on Tk.pm to get started.
Before we continue, there a few odd things you need to know about Perl/Tk:
 => is functionally the same as a comma (,). Using => makes it easier
to detect "pairs" of items in a list.
 Widgets are always built referencing another part of the GUI, if not
the main window (in our examples, $mw), then another widget or
frame. This builds the parent/child hierarchy and allows the packer to
know what to pack where.
 The pack( ) method essentially displays the widget on the screen,
according to any parameters sent to it. Alternately, it could un-display
it as well. If you don't pack( ) a widget, it won't show up.
Now on to some examples.
A Dictionary Client: xword
For our first example, we want to build a simple application that has only a
few types of widgets in it. The xword program will prompt the user for a
word, then use an online dictionary to define it, and return the formatted
results.
When you need a quick word definition, instead of running a web browser
(which can often have a lengthy startup time with all those fancy plug-ins),
surfing to the site via a bookmark, and then entering the word to get your
answer, you can use this simple program that will just prompt for the word
and go look it up without all that extra hassle. Anyone familiar with the
xwebster client for the X Window System will find xword to be vaguely
familiar, but our version doesn't require a local licensed dictionary server;
we use one already existing on the Web. Since the program is so simple, you
can probably just iconify it, and then bring it back up whenever you're
stumped for the spelling or meaning of another word.
So in designing our window, we want a place to enter the word, and a place

to display the results. We also need to be able to exit the program (always a
must). It seems pretty simple, until we remember that the definition
information sent back to us is going to come back in HTML. I really don't
want to have to visually dig through a bunch of HTML codes to find out the
answer I'm looking for, so I want my program to handle that as well when it
displays the answer. We have two options: ignore the HTML codes
completely or find a simple way to parse them and make the output look a
little nicer.
Luckily, the HTML module distributed with LWP will do most of the work
for us. As described in Chapter 5, The LWP Library, the HTML package
contains a function called parse_html(), which takes a string containing
HTML as its argument, and returns a pointer to a data structure with all the
HTML tags and text parsed out and remembered in order. Now we can use
another function called traverse(), which operates on this data structure and
lets us specify what function to call for each piece of information it contains.
Keeping all this in mind, let's look at our program:

×