And then the Windows failed - and then I could not see to see -
- Emily Dickinson "I heard a Fly buzz - when I died"
Everything we use has a user interface: VCRs, computers, telephones, even books. Our programs have user interfaces: do we have to supply arguments on the command line? Can we drag and drop files into the program? Do we have to press
This chapter won't discuss designing user interfaces: entire bookshelves are filled with books written on the subject. Instead, we'll focus on implementing user interfaces - parsing command-line arguments, reading a character at a time, writing anywhere on the screen, and writing a graphical user interface.
The simplest user interface is what we'll call line mode interfaces. Line mode programs normally read lines at a time and write characters or entire lines. Filters like grep and utilities like mail exemplify this type of interface. We don't really talk much about this type of interface in this chapter, because so much of the rest of the book does.
A more complex interface is what we'll call full-screen mode . Programs like vi , elm , and lynx have full-screen interfaces. They read single characters at a time and can write to any character position on the screen. We address this type of interface in Recipes Recipe 15.4 , Recipe 15.6 , Recipe 15.9 , Recipe 15.10 , and Recipe 15.11 .
The final class of interface is the GUI (graphical user interface). Programs with GUIs can address individual pixels, not just characters. GUIs often follow a windowing metaphor, in which a program creates windows that appear on the user's display device. The windows are filled with widgets, things like scrollbars to drag or buttons to click. Netscape Navigator provides a full graphical user interface, as does your window manager. Perl can use many GUI toolkits, but here we'll cover the Tk toolkit, since it's the most well-known and portable. See Recipes Recipe 15.14 , Recipe 15.15 , and Recipe 15.19 .
A program's user interface is different from the environment you run it in. Your environment determines the type of program you can run. If you're logged in through a terminal capable of full-screen I/O, you can run line mode applications but not GUI programs. Let's look briefly at the environments.
Some environments only handle programs that have a bare line mode interface. This includes executing programs with backticks, over rsh , or from cron . Their simple interface allows them to be combined creatively and powerfully as reusable components in larger scripts. Line mode programs are wonderful for automation, because they don't rely on a keyboard or screen. They rely on only STDIN and STDOUT - if that. These are often the most portable programs because they use nothing but the basic I/O supported by virtually all systems.
The typical login session, where you use a terminal with a screen and keyboard, permits both line mode and full-screen interfaces. Here the program with the full-screen interface talks to the terminal driver and has intimate knowledge of how to make the terminal write to various positions on the screen. To automate such a program you need to create a pseudo-terminal for the program to talk to, as shown in Recipe 15.13 .
Finally, some window systems let you run line mode and full-screen programs as well as programs that use a GUI. For instance, you can run grep (line-mode programs) from within vi (a full-screen program) from an xterm window (a GUI program running in a window system environment). GUI programs are difficult to automate unless they provide an alternative interface through remote procedure calls.
Toolkits exist for programming in full-screen and GUI environments. These toolkits ( curses for full-screen programs; Tk for GUI programs) increase the portability of your programs by abstracting out system-specific details. A curses program can run on virtually any kind of terminal without the user worrying about which particular escape sequences they need to use. Tk programs will run unmodified on Unix and Windows systems - providing you don't use operating-system specific functions.
There are other ways to interact with a user, most notably through the Web. We cover the Web in Chapters Chapter 19, CGI Programming and Chapter 20, Web Automation , so we make no further mention of it here.
Copyright © 2002 O'Reilly & Associates. All rights reserved.