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

📄 userinterfaces.mht

📁 Perl 编程技巧大全。适合初学者阅读。
💻 MHT
📖 第 1 页 / 共 5 页
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: User Interfaces
Date: Wed, 29 Apr 2009 09:27:38 +0800
MIME-Version: 1.0
Content-Type: text/html;
	charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/userinterfaces.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>User Interfaces</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3D"Database Access" =
href=3D"dbaccess.html"=20
rel=3DPREVIOUS><LINK title=3D"Process Management and Communication"=20
href=3D"processmanagementetc.html" rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
  <TBODY>
  <TR>
    <TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
  <TR>
    <TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/dbaccess.html">Prev</A></=
TD>
    <TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
    <TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
      =
href=3D"http://pleac.sourceforge.net/pleac_perl/processmanagementetc.html=
">Next</A></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DUSERINTERFACES>15. User Interfaces</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN792>Parsing Program Arguments</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Getopt::Std</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#bebebe># -v ARG, -D ARG, -o ARG, sets $opt_v, $opt_D, =
$opt_o
</FONT>getopt<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"vDo"</FONT><FONT color=3D#00ffff>);</FONT><FONT =
color=3D#43cd80>              </FONT>
<FONT color=3D#bebebe># -v ARG, -D ARG, -o ARG, sets </FONT><FONT =
color=3D#cdcd00>$args</FONT><FONT color=3D#bebebe>{</FONT><FONT =
color=3D#00cd00>v</FONT><FONT color=3D#bebebe>}, </FONT><FONT =
color=3D#cdcd00>$args</FONT><FONT color=3D#bebebe>{</FONT><FONT =
color=3D#00cd00>D</FONT><FONT color=3D#bebebe>}, </FONT><FONT =
color=3D#cdcd00>$args</FONT><FONT color=3D#bebebe>{</FONT><FONT =
color=3D#00cd00>o</FONT><FONT color=3D#bebebe>}
</FONT>getopt<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"vDo"</FONT>, \<FONT color=3D#cdcd00>%args</FONT><FONT =
color=3D#00ffff>);</FONT>

getopts<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"vDo:"</FONT><FONT color=3D#00ffff>);</FONT>         =
<FONT color=3D#bebebe># -v, -D, -o ARG, sets $opt_v, $opt_D, $opt_o
</FONT>getopts<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"vDo:"</FONT>, \<FONT color=3D#cdcd00>%args</FONT><FONT =
color=3D#00ffff>);</FONT> <FONT color=3D#bebebe># -v, -D, -o ARG, sets =
</FONT><FONT color=3D#cdcd00>$args</FONT><FONT =
color=3D#bebebe>{</FONT><FONT color=3D#00cd00>v</FONT><FONT =
color=3D#bebebe>}, </FONT><FONT color=3D#cdcd00>$args</FONT><FONT =
color=3D#bebebe>{</FONT><FONT color=3D#00cd00>D</FONT><FONT =
color=3D#bebebe>}, </FONT><FONT color=3D#cdcd00>$args</FONT><FONT =
color=3D#bebebe>{</FONT><FONT color=3D#00cd00>o</FONT><FONT =
color=3D#bebebe>}
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Getopt::Long</FONT><FONT color=3D#00ffff>;</FONT>

GetOptions<FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#00cd00>"verbose"</FONT>  =3D&gt; \$verbose,     <FONT =
color=3D#bebebe># --verbose
</FONT>            <FONT color=3D#00cd00>"Debug"</FONT>    =3D&gt; =
\$debug,       <FONT color=3D#bebebe># --Debug
</FONT>            <FONT color=3D#00cd00>"output=3Ds"</FONT> =3D&gt; =
\$output <FONT color=3D#00ffff>);</FONT>    <FONT color=3D#bebebe># =
--output=3Dstring or --output=3Dstring
#-----------------------------
#% rm -r -f /tmp/testdir
#-----------------------------
#% rm -rf /tmp/testdir
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Getopt::Std</FONT><FONT color=3D#00ffff>;</FONT>
getopts<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"o:"</FONT><FONT color=3D#00ffff>);</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$opt_o<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Writing =
output to $opt_o"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Getopt::Std</FONT><FONT color=3D#00ffff>;</FONT>

<FONT color=3D#cdcd00>%option</FONT> =3D <FONT =
color=3D#00ffff>();</FONT>
getopts<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Do:"</FONT>, =
\<FONT color=3D#cdcd00>%option</FONT><FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>$option</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>D</FONT><FONT color=3D#00ffff>})</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Debugging =
mode enabled.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

 <FONT color=3D#bebebe># if not set, set output to "-".  opening "-" for =
writing
</FONT> <FONT color=3D#bebebe># means STDOUT
</FONT> <FONT color=3D#cdcd00>$option</FONT><FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>o</FONT><FONT =
color=3D#00ffff>}</FONT> =3D <FONT color=3D#00cd00>"-"</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#ff7f50>defined</FONT> <FONT =
color=3D#cdcd00>$option</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>o</FONT><FONT color=3D#00ffff>};</FONT>
<FONT color=3D#43cd80>                             </FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Writing output =
to file </FONT><FONT color=3D#cdcd00>$option</FONT><FONT =
color=3D#00cd00>{o}\n"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#cdcd00>$option</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>o</FONT><FONT color=3D#00ffff>}</FONT> <FONT =
color=3D#98fb98>eq</FONT> <FONT color=3D#00cd00>"-"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>STDOUT, =
<FONT color=3D#00cd00>"&gt; </FONT><FONT =
color=3D#cdcd00>$option</FONT><FONT color=3D#00cd00>{o}"</FONT><FONT =
color=3D#00ffff>)</FONT>
     <FONT color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> =
<FONT color=3D#00cd00>"Can't open </FONT><FONT =
color=3D#cdcd00>$option</FONT><FONT color=3D#00cd00>{o} for output: =
$!\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
#% gnutar --extract --file latest.tar
#-----------------------------
#% gnutar --extract --file=3Dlatest.tar
#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Getopt::Long</FONT><FONT color=3D#00ffff>;</FONT>

GetOptions<FONT color=3D#00ffff>(</FONT> <FONT =
color=3D#00cd00>"extract"</FONT> =3D&gt; \$extract,
            <FONT color=3D#00cd00>"file=3Ds"</FONT>  =3D&gt; \$file =
<FONT color=3D#00ffff>);</FONT>

<FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>$extract<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
    <FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"I'm =
extracting.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>

<FONT color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"I wish I had a =
file"</FONT> <FONT color=3D#ffa500>unless</FONT> <FONT =
color=3D#ff7f50>defined</FONT> $file<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Working on the =
file $file\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN795>Testing Whether a Program Is Running=20
Interactively</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>I_am_interactive</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT color=3D#b2dfee>-t</FONT> =
STDIN &amp;&amp; <FONT color=3D#b2dfee>-t</FONT> STDOUT<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>POSIX</FONT> <FONT color=3D#ff7f50>qw</FONT>/<FONT =
color=3D#98fb98>getpgrp</FONT> tcgetpgrp/<FONT color=3D#00ffff>;</FONT>

<FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>I_am_interactive</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>local</FONT> <FONT =
color=3D#9ac0cd>*TTY</FONT><FONT color=3D#00ffff>;</FONT>  <FONT =
color=3D#bebebe># local file handle
</FONT>    <FONT color=3D#98fb98>open</FONT><FONT =
color=3D#00ffff>(</FONT>TTY, <FONT =
color=3D#00cd00>"/dev/tty"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#98fb98>or</FONT> <FONT color=3D#ffa500>die</FONT> <FONT =
color=3D#00cd00>"can't open /dev/tty: $!"</FONT><FONT =
color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$tpgrp</FONT> =
=3D tcgetpgrp<FONT color=3D#00ffff>(</FONT><FONT =
color=3D#98fb98>fileno</FONT><FONT color=3D#00ffff>(</FONT>TTY<FONT =
color=3D#00ffff>));</FONT>
    <FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$pgrp</FONT>  =
=3D <FONT color=3D#98fb98>getpgrp</FONT><FONT color=3D#00ffff>();</FONT>
    <FONT color=3D#98fb98>close</FONT> TTY<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>return</FONT> <FONT =
color=3D#00ffff>(</FONT>$tpgrp =3D=3D $pgrp<FONT =
color=3D#00ffff>);</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>I_am_interactive<FONT color=3D#00ffff>())</FONT> =
<FONT color=3D#00ffff>{</FONT>
        <FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"Prompt: "</FONT><FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#00ffff>}</FONT>
    $line =3D &lt;STDIN&gt;<FONT color=3D#00ffff>;</FONT>
    <FONT color=3D#ffa500>last</FONT> <FONT =
color=3D#ffa500>unless</FONT> <FONT color=3D#ff7f50>defined</FONT> =
$line<FONT color=3D#00ffff>;</FONT><FONT color=3D#43cd80> </FONT>
    <FONT color=3D#bebebe># do something with the line
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#b2dfee>prompt</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Prompt: "</FONT> =
<FONT color=3D#ffa500>if</FONT> I_am_interactive<FONT =
color=3D#00ffff>()</FONT> <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ffa500>for</FONT> <FONT =
color=3D#00ffff>(</FONT>prompt<FONT color=3D#00ffff>();</FONT> $line =3D =
&lt;STDIN&gt;<FONT color=3D#00ffff>;</FONT> prompt<FONT =
color=3D#00ffff>())</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#bebebe># do something with the line
</FONT><FONT color=3D#00ffff>}</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN798>Clearing the Screen</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
  <TBODY>
  <TR>
    <TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>use</FONT> <FONT =
color=3D#b2dfee>Term::Cap</FONT><FONT color=3D#00ffff>;</FONT>

$OSPEED =3D <FONT color=3D#cdcd00>9600</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>eval</FONT> <FONT color=3D#00ffff>{</FONT>
    <FONT color=3D#ffa500>require</FONT> <FONT =

⌨️ 快捷键说明

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