This is exciting stuff. You can wrap up all your existing Perl-coded applications with skinny GUI scripts to make them into similarly interactive and "user-friendly" programs. In fact, you can easily rewrite utilities you have now so that they can continue to operate in a normal "batch" mode when given appropriate command-line arguments, but they can branch and construct a GUI window otherwise:Listing 3. Select between command-line and GUI operation
Remember: it's just a few extra lines of the Perl (or Python, or ...)
code you already know to give these results. What's more, because Perl
acts as "glue" to external applications, these same techniques work to
wrap legacy Fortran or C applications that you don't want to touch. Just
wrap them up in a bit of Perl with backticks or
you're in business.
Perl/Tk scales well, too. Substantial Perl/Tk applications are in everyday use around the world, including a biochemical simulator, geographic mapping programs, a flight management system, and much more. It's difficult to convey the feel of large-scale Perl/Tk programming. The examples above demonstrate how straightforward it is to "GUIfy" with a few extra lines of Perl. For a fuller sense of Perl/Tk programming "in the large," follow the references in Resources below, and study for yourself the source code of more complex programs.
One program I haven't written yet, except in a crude prototype, is a graphical version of Zlatanov's genetic algorithm
modeler (read about it in his developerWorks article listed in Resources). I leave a
more satisfying version as an exercise to the reader. My own experiments
suggested it'll make a showcase for the approach this column recommends:
its batch-mode operation will really come alive when visualized on the
If you tried Perl/Tk a few years ago, and were unimpressed, it's time to give it another chance. The Perl/Tk processor has improved a great deal lately. It's far easier for Perl/Tk beginners to install binaries for popular platforms, Windows portability is much better, and the latest releases simply exhibit higher quality. Perhaps even more important, more books on Perl/Tk are available. July's second edition of O'Reilly's Perl in a Nutshell includes a valuable Perl/Tk reference. Best of all, O'Reilly kicked off 2002 with publication of Mastering Perl/Tk by Nancy Walsh and Stephen Lidie. If you plan to work for more than a couple of hours with Perk/Tk, Mastering Perl/Tk is the one reference you need to buy.
GUI work can seem mysterious to systems programmers and administrators. It looks like a specialized pursuit that's rather removed from the kinds of development we normally do in "Server clinic."
It doesn't have to be that way, though. Modern GUI toolkits like Perl/Tk offer good performance and functionality, along with the productivity and ease-of-learning this column most often emphasizes. Take an hour or two this month and give Perl/Tk a chance to help you wrap up your programs more attractively.
- Check out the other installments of Server clinic.
- The Perl/Tk FAQ aims to answer all common questions about the Perl/Tk GUI toolkit.
Ted Zlatanov's article "Cultured Perl: Automating UNIX system administration with Perl" describes
cfengineand details the use of Perl to clear the process table (developerWorks, July 2001).
Ted Zlatanov's article "Cultured Perl: Genetic algorithms, the next generation" uses Perl for its implementations (developerWorks, October 2002).
Perl in a Nutshell (O'Reilly & Associates; 2002) includes a compact reference to Perl/Tk.
Mastering Perl/Tk (O'Reilly & Associates; 2002) is simply the best Perl/Tk book.
- Cameron's personal notes on GUI toolkits compare Tk, Motif, Qt, GTK+, and other popular toolkits.
- Other related developerWorks content includes:
- "Tips for beginners using Python's GUI library" (developerWorks, December 2000)
- "Graphics made easy using the GNOME canvas" (developerWorks, January 2000)
- "GUI building with GTK+" (developerWorks, July 1999)
- "Easy GUI programming with EasyGTK" (developerWorks, December 2000)
- "Create native, cross-platform GUI applications" (developerWorks, April 2002)
- Learn to build a client-server application for DB2 using extensions to Perl and Python.
- Find more Linux resources in the developerWorks Linux zone.