⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch15_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:43:04Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch14_12.htm"TITLE="14.11. Program: ggh&nbsp- Grep Netscape Global History"><LINKREL="next"HREF="ch15_02.htm"TITLE="15.1. Parsing Program Arguments"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_12.htm"TITLE="14.11. Program: ggh&nbsp- Grep Netscape Global History"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.11. Program: ggh&nbsp- Grep Netscape Global History"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_02.htm"TITLE="15.1. Parsing Program Arguments"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.1. Parsing Program Arguments"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch15-54291">15. User Interfaces</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch15-17450"TITLE="15.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch15_02.htm"TITLE="15.1. Parsing Program Arguments">Parsing Program Arguments</A><BR><ACLASS="sect1"HREF="ch15_03.htm"TITLE="15.2. Testing Whether a Program Is Running Interactively">Testing Whether a Program Is Running Interactively</A><BR><ACLASS="sect1"HREF="ch15_04.htm"TITLE="15.3. Clearing the Screen">Clearing the Screen</A><BR><ACLASS="sect1"HREF="ch15_05.htm"TITLE="15.4. Determining Terminal or Window Size">Determining Terminal or Window Size</A><BR><ACLASS="sect1"HREF="ch15_06.htm"TITLE="15.5. Changing Text Color">Changing Text Color</A><BR><ACLASS="sect1"HREF="ch15_07.htm"TITLE="15.6. Reading from the Keyboard">Reading from the Keyboard</A><BR><ACLASS="sect1"HREF="ch15_08.htm"TITLE="15.7. Ringing the Terminal Bell">Ringing the Terminal Bell</A><BR><ACLASS="sect1"HREF="ch15_09.htm"TITLE="15.8. Using POSIX termios">Using POSIX termios</A><BR><ACLASS="sect1"HREF="ch15_10.htm"TITLE="15.9. Checking for Waiting Input">Checking for Waiting Input</A><BR><ACLASS="sect1"HREF="ch15_11.htm"TITLE="15.10. Reading Passwords">Reading Passwords</A><BR><ACLASS="sect1"HREF="ch15_12.htm"TITLE="15.11. Editing Input">Editing Input</A><BR><ACLASS="sect1"HREF="ch15_13.htm"TITLE="15.12. Managing the Screen">Managing the Screen</A><BR><ACLASS="sect1"HREF="ch15_14.htm"TITLE="15.13. Controlling Another Program with Expect">Controlling Another Program with Expect</A><BR><ACLASS="sect1"HREF="ch15_15.htm"TITLE="15.14. Creating Menus with Tk">Creating Menus with Tk</A><BR><ACLASS="sect1"HREF="ch15_16.htm"TITLE="15.15. Creating Dialog Boxes with Tk">Creating Dialog Boxes with Tk</A><BR><ACLASS="sect1"HREF="ch15_17.htm"TITLE="15.16. Responding to Tk Resize Events">Responding to Tk Resize Events</A><BR><ACLASS="sect1"HREF="ch15_18.htm"TITLE="15.17. Removing the DOS Shell Window with Windows Perl/Tk">Removing the DOS Shell Window with Windows Perl/Tk</A><BR><ACLASS="sect1"HREF="ch15_19.htm"TITLE="15.18. Program: Small termcap program">Program: Small termcap program</A><BR><ACLASS="sect1"HREF="ch15_20.htm"TITLE="15.19. Program: tkshufflepod">Program: tkshufflepod</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>And then the Windows failed&nbsp;- and then I could not see to see&nbsp;- </I></P><PCLASS="attribution"ALIGN="right">-&nbsp;Emily Dickinson <CITECLASS="citetitle">"I heard a Fly buzz&nbsp;- when I died"</CITE></P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-17450">15.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch15-idx-1000004994-0"></A>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 <CODECLASS="literal">Enter</CODE> after every response we make, or can the program read a single keystroke at a time?</P><PCLASS="para">This chapter won't discuss <EMCLASS="emphasis">designing</EM> user interfaces: entire bookshelves are filled with books written on the subject. Instead, we'll focus on <EMCLASS="emphasis">implementing</EM> user interfaces&nbsp;- parsing command-line arguments, reading a character at a time, writing anywhere on the screen, and writing a graphical user interface.</P><PCLASS="para">The simplest user interface is what we'll call <EMCLASS="emphasis">line mode</EM><ACLASS="indexterm"NAME="ch15-idx-1000005018-0"></A> interfaces. Line mode programs normally read lines at a time and write characters or entire lines. Filters like <EMCLASS="emphasis">grep</EM> and utilities like <EMCLASS="emphasis">mail</EM> 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.</P><PCLASS="para">A more complex interface is what we'll call <EMCLASS="emphasis">full-screen mode</EM><ACLASS="indexterm"NAME="ch15-idx-1000005019-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005019-1"></A>. Programs like <EMCLASS="emphasis">vi</EM>, <EMCLASS="emphasis">elm</EM>, and <EMCLASS="emphasis">lynx</EM> 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 <ACLASS="xref"HREF="ch15_05.htm"TITLE="Determining Terminal or Window Size">Recipe 15.4</A>, <ACLASS="xref"HREF="ch15_07.htm"TITLE="Reading from the Keyboard">Recipe 15.6</A>, <ACLASS="xref"HREF="ch15_10.htm"TITLE="Checking for Waiting Input">Recipe 15.9</A>, <ACLASS="xref"HREF="ch15_11.htm"TITLE="Reading Passwords">Recipe 15.10</A>, and <ACLASS="xref"HREF="ch15_12.htm"TITLE="Editing Input">Recipe 15.11</A>.</P><PCLASS="para">The final class of interface is the <ACLASS="indexterm"NAME="ch15-idx-1000005020-0"></A>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 <ACLASS="xref"HREF="ch15_15.htm"TITLE="Creating Menus with Tk">Recipe 15.14</A>, <ACLASS="xref"HREF="ch15_16.htm"TITLE="Creating Dialog Boxes with Tk">Recipe 15.15</A>, and <ACLASS="xref"HREF="ch15_20.htm"TITLE="Program: tkshufflepod">Recipe 15.19</A>.</P><PCLASS="para">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.</P><PCLASS="para">Some environments only handle programs that have a bare line mode interface. This includes executing programs with backticks, over <EMCLASS="emphasis">rsh</EM>, or from <EMCLASS="emphasis">cron</EM>. 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 &nbsp;-  if that. These are often the most portable programs because they use nothing but the basic I/O supported by virtually all systems.</P><PCLASS="para">The typical <ACLASS="indexterm"NAME="ch15-idx-1000005021-0"></A>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 <ACLASS="xref"HREF="ch15_14.htm"TITLE="Controlling Another Program with Expect">Recipe 15.13</A>.</P><PCLASS="para">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 <EMCLASS="emphasis">grep</EM> (line-mode programs) from within <EMCLASS="emphasis">vi</EM> (a full-screen program) from an <EMCLASS="emphasis">xterm</EM> 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.</P><PCLASS="para">Toolkits exist for programming in full-screen and GUI environments. These toolkits (<EMCLASS="emphasis">curses</EM><ACLASS="indexterm"NAME="ch15-idx-1000005022-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005022-1"></A> 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 &nbsp;-  providing you don't use operating-system specific functions.</P><PCLASS="para">There are other ways to interact with a user, most notably through the Web. We cover the Web in Chapters <ACLASS="xref"HREF="ch19_01.htm"TITLE="CGI Programming">Chapter 19, <CITECLASS="chapter">CGI Programming</CITE></A> and <ACLASS="xref"HREF="ch20_01.htm"TITLE="Web Automation">Chapter 20, <CITECLASS="chapter">Web Automation</CITE></A>, so we make no further mention of it here.</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_12.htm"TITLE="14.11. Program: ggh&nbsp- Grep Netscape Global History"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.11. Program: ggh&nbsp- Grep Netscape Global History"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_02.htm"TITLE="15.1. Parsing Program Arguments"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.1. Parsing Program Arguments"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.11. Program: ggh&nbsp- Grep Netscape Global History</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">15.1. Parsing Program Arguments</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -