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

📄 ch15_13.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 15.12. Managing the Screen (Perl Cookbook)</TITLE><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:24Z"><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="ch15_01.htm"TITLE="15. User Interfaces"><LINKREL="prev"HREF="ch15_12.htm"TITLE="15.11. Editing Input"><LINKREL="next"HREF="ch15_14.htm"TITLE="15.13. Controlling Another Program with Expect"></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="ch15_12.htm"TITLE="15.11. Editing Input"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.11. Editing Input"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch15_01.htm"TITLE="15. User Interfaces"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch15_14.htm"TITLE="15.13. Controlling Another Program with Expect"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.13. Controlling Another Program with Expect"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch15-35549">15.12. Managing the Screen</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1107">Problem<ACLASS="indexterm"NAME="ch15-idx-1000005132-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005132-1"></A></A></H3><PCLASS="para">You want to control the screen layout or highlighting, detect when special keys are pressed, or present full-screen menus, but you don't want to think about what kind of display device the user has.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1113">Solution</A></H3><PCLASS="para">Use the <ACLASS="indexterm"NAME="ch15-idx-1000005141-0"></A>Curses module from CPAN, which makes use of your native <EMCLASS="emphasis">curses </EM>(3) library.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1119">Description</A></H3><PCLASS="para">The <EMCLASS="emphasis">curses</EM> library provides easy access to the full screen display in an efficient and device-independent fashion. (By display, we mean any cursor-addressable monitor.) With Curses, you write high-level code to put data on the logical display, building it up character by character or string by string. When you want output to show up, call the <CODECLASS="literal">refresh</CODE> function. The library generates output consisting only of the changes on the virtual display since the last call to <CODECLASS="literal">refresh</CODE>. This is particularly appreciated on a slow connection.</P><PCLASS="para">The example program in <ACLASS="xref"HREF="ch15_13.htm#ch15-35516"TITLE="rep">Example 15.5</A>, called <EMCLASS="emphasis">rep</EM><ACLASS="indexterm"NAME="ch15-idx-1000005142-0"></A>, demonstrates this. Call it with arguments of the program to run, like any of these:</P><PRECLASS="programlisting">% rep ps aux% rep netstat% rep -2.5 lpq</PRE><PCLASS="para">The <EMCLASS="emphasis">rep</EM> script will repeatedly call the listed command, printing its output to the screen, updating only what has changed since the previous run. This is most effective when the changes between runs are small. It maintains the current date in reverse video at the bottom-right corner of your screen.</P><PCLASS="para">By default, <EMCLASS="emphasis">rep</EM> waits 10 seconds before rerunning the command. You can change this delay period by calling it an optional number of seconds (which can be a decimal number) as shown above when calling <EMCLASS="emphasis">lpq</EM>. You may also hit any key during the pause for it to run the command right then.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch15-35516">Example 15.5: rep</A></H4><PRECLASS="programlisting">#!/usr/bin/perl -w# rep - screen repeat commanduse strict;use Curses;my $timeout = 10;if (@ARGV &amp;&amp; $ARGV[0] =~ /^-(\d+\.?\d*)$/) {     $timeout = $1;     shift; } die &quot;usage: $0 [ -timeout ] cmd args\n&quot; unless @ARGV;    initscr();          # start screennoecho();           cbreak(); nodelay(1);         # so getch() is non-blocking$SIG{INT} = sub { done(&quot;Ouch!&quot;) };sub done { endwin(); print &quot;@_\n&quot;; exit; }while (1) {    while ((my $key = getch()) ne ERR) {    # maybe multiple keys        done(&quot;See ya&quot;) if $key eq 'q'     }    my @data = `(@ARGV) 2&gt;&amp;1`;              # gather output+errors    for (my $i = 0; $i &lt; $LINES; $i++) {        addstr($i, 0, $data[$i] || ' ' x $COLS);    }     standout();    addstr($LINES-1, $COLS - 24, scalar localtime);    standend();    move(0,0);     refresh();                              # flush new output to display    my ($in, $out) = ('', '');    vec($in,fileno(STDIN),1) = 1;           # look for key on stdin     select($out = $in,undef,undef,$timeout);# wait up to this long}</PRE></DIV><PCLASS="para">Curses lets you tell whether the user typed one of the arrow keys or those other funny keys, like <CODECLASS="literal">HOME</CODE> or <CODECLASS="literal">INSERT</CODE>. This is normally difficult, because those keys send multiple bytes. With Curses, it's easy:</P><PRECLASS="programlisting">keypad(1);                  # enable keypad mode$key = getch();if ($key eq 'k'     ||      # vi mode    $key eq &quot;\cP&quot;   ||      # emacs mode    $key eq KEY_UP)         # arrow mode{    # do something} </PRE><PCLASS="para">Other Curses functions let you read the text at particular screen coordinates, control highlighting and standout mode, and even manage multiple windows.</P><PCLASS="para">The <ACLASS="indexterm"NAME="ch15-idx-1000006049-0"></A>perlmenu module, also from CPAN, is built on top of the lower-level Curses module. It provides high-level access to menus and fill-out forms. Here's a sample form from the perlmenu distribution:</P><PRECLASS="programlisting">                      Template Entry Demonstration    Address Data Example                                     Record # ___   Name: [________________________________________________]   Addr: [________________________________________________]   City: [__________________]          State: [__]       Zip: [\\\\\]    Phone: (\\\) \\\-\\\\                            Password: [^^^^^^^^]   Enter all information available.   Edit fields with left/right arrow keys or &quot;delete&quot;.   Switch fields with &quot;Tab&quot; or up/down arrow keys.   Indicate completion by pressing &quot;Return&quot;.   Refresh screen with &quot;Control-L&quot;.   Abort this demo here with &quot;Control-X&quot;.</PRE><PCLASS="para">The user types in the areas indicated, with regular text indicated by underline fields, numeric data by backslashed fields, and starred-out data with circumflexed fields. This is reminiscent of Perl's formats, except that forms are used for output, not input.<ACLASS="indexterm"NAME="ch15-idx-1000005134-0"></A><ACLASS="indexterm"NAME="ch15-idx-1000005134-1"></A><ACLASS="indexterm"NAME="ch15-idx-1000005134-2"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch15-pgfId-1269">See Also</A></H3><PCLASS="para">Your system's <ICLASS="filename">curses </I>(3) manpage (if you have it); the documentation for the Curses and the perlmenu modules from CPAN; the section on <ACLASS="olink"HREF="../prog/ch02_08.htm">"Formats"</A> in <ACLASS="olink"HREF="../prog/ch02_01.htm">Chapter 2</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>, or <ICLASS="filename">perlform </I>(1); <ACLASS="xref"HREF="ch03_11.htm"TITLE="Short Sleeps">Recipe 3.10</A></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="ch15_12.htm"TITLE="15.11. Editing Input"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 15.11. Editing Input"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_14.htm"TITLE="15.13. Controlling Another Program with Expect"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 15.13. Controlling Another Program with Expect"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">15.11. Editing Input</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.13. Controlling Another Program with Expect</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 + -