The Perl Journal December, 2003
Mastering Perl/Tk
Steve Lidie and Nancy Walsh
O'Reilly & Associates, 2003
766 pp., $44.95
ISBN 1-56592-716-8
For a while, it seemed that we didn't really need a cross-platform GUI. The Macintosh was here, and Windows was there, and UNIX was its own weird self. Now, with Mac OS X gaining ground, especially among the Perl world, and the Panther release including X11, it may be time for Tk to fill the need. Mastering Perl/Tk, by Steve Lidie and Nancy Walsh, is a fine place to start. It's a replacement, update, and expansion of Walsh's earlier effort, Learning Perl/Tk.
Chapter 1 takes an approach that I'd like to see more common in computer books that introduce a new subculture to the reader. It's sort of a crash course on what may be surprisingly different when writing Perl/Tk programs, such as: Indentation styles, how to debug a GUI program, naming conventions, and the proper way to end a program. It's good to know the methods to the madness before starting the journey.
After the introductions, half the book is devoted to the panoply of Perl/Tk widgets: text entry fields, scrollbars, frames, listboxes, menus, and so on. The progression of widgets is logically ordered and builds on previous chapters. Each widget is presented with a list of options available, including the default in bold. There's also a later chapter on the Tk Interface Extension widgets, a set of widgets that comes with the Tk module itself including TList, Tree, DirTree, HList, and TListindispensable for any sort of hierarchy or filesystem navigation applications.
The chapter on geometry management explains each of the four different geometry managers (pack, place, grid, and form) in depth, discussing the differences between each, and when each is appropriate to use. The geometry manager is responsible for placing the widgets in a window, and can be a daunting learning experience to the Perl/Tk programmer, but the illustrations and examples are clear and instructive.
It's not surprising that Perl/Tk has a variety of different graphic capabilities, from custom cursors, to defining bitmaps in code, to tiling images on canvases, and more. I was disappointed, then, that the entire graphics chapter was only 33 pages long. The 3D progress bar was cool, but I would have liked to have seen more examples of Tk being more than text.
Things start to get really interesting in the second half of the book, making the book much more than a rehash of the online docs. It starts off with a chapter on creating your own widgets in Perl. Every step of the process is included, from the idea to creating the framework of required subroutines, to filling in the code, to packaging and releasing your widget for CPAN distribution.
Unfortunately, I was disappointed to see the section on releasing and packaging your widget use the outdated test.pl single test program instead of a t directory, and the use of the Test module instead of the newer, more flexible Test::More.
Later in the book, there's a chapter on creating widgets in C. The sample widget is very simple, only drawing a square, but it's just as well. The intricacies of C widget creation are apparently far more torturous than in Perl, and the discussion of interfacing Perl and C is long enough as it is. As with the chapter on Perl widget creation, each step is explained, including having a completed distribution. For most readers, custom Perl widgets will be the way to go, but for those interfacing with existing third-party libraries, C extensions may be necessary.
The rest of the chapters are a grab bag, showing ways of using Perl/Tk with parts of the system that don't specifically relate to Tk. They're great for showing Tk as an actual application framework. The chapter on IPC shows parallel processing of the value of pi, the LWP chapter shows a simple program to fetch comics from web sites, and so on.
The appendices are a disappointmentalmost an afterthought. They make up 18 percent of the book's page count and don't add anything very usable. Appendix A is a two-page treatment of installing both Perl and Perl/Tk on UNIX and Windows. This isn't worth the mention for one of the most notoriously hard-to-compile modules.
Appendix B is a listing of options and default values for each of the widgets. Unfortunately, it's entirely redundant to the package docs, printed in landscape, and there's a meaningless column called "Current value" populated with values like ARRAY(0x87d8b14). I would have preferred an aggregation of the clear, useful option listings from the chapters. Just because it's an appendix doesn't mean that it has to change the format of the information from earlier in the book.
Finally, Appendix C is a printout of all the code mentioned in the book proper. I hope that this doesn't signal a return to the bad old days of the computer book publishing boom when book thickness was everything, superfluous code listings padded the page count, and CD-ROMs contained archives of what was freely and easily available online.
Far more useful appendix content might have included a graphical map of the Perl/Tk widget class hierarchy, since every Perl/Tk widget is derived from from the Tk base class and shares commonalities with other widgets. There's even a program in the chapter on building your own widgets that shows an example of a single widget's inheritance tree, so I'm baffled that there's not one in the appendix.
The book is put together well, and has the excellent O'Reilly layout and style. There are copious screenshots and figures throughout, making it clear how the widget will look onscreen. The figures are appropriately sized, unlike many books that, for example, print entire screens to illustrate one listbox. It was nice to see screens from Windows mixed with X11, each with a slightly different widget look.
Despite the criticisms, Mastering Perl/Tk is a fine book that I recommend for anyone interested in graphics programming with Perl. I only wonder how much of its $44.95 cover price could have been shaved off if not for the superfluous appendixes.
TPJ