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

📄 ch02_08.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Getting User Input (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#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="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_07.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch02_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.8. Getting User Input</h2><p>At this point, you're probably wondering how to get a valuefrom the keyboard into a Perl program. Here's the simplest way:use the line-input operator,<tt class="literal">&lt;STDIN&gt;</tt><a name="INDEX-238" /><a name="INDEX-239" /><a name="INDEX-240" />.<a href="#FOOTNOTE-61">[61]</a> Each time you use <tt class="literal">&lt;STDIN&gt;</tt> in aplace where a scalar value is expected, Perl reads the next completetext line from <em class="emphasis">standardinput</em><a name="INDEX-241" /> (up to the first newline), and uses thatstring as the value of <tt class="literal">&lt;STDIN&gt;</tt>. Standardinput can mean many things, but unless you do something uncommon, itmeans the keyboard of the user who invoked your program (probablyyou). If there's nothing waiting to be read (typically thecase, unless you type ahead a complete line), the Perl program willstop and wait for you to enter some characters followed by a newline(return).<a href="#FOOTNOTE-62">[62]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-61" /><p>[61]This is actually aline-input operator working on the filehandle<tt class="literal">STDIN</tt>, but we can't tell you about thatuntil we get to filehandles (in <a href="ch11_01.htm">Chapter 11, "Filehandles and File Tests"</a>).</p></blockquote><blockquote class="footnote"> <a name="FOOTNOTE-62" /><p>[62]To be honest, it's normally yoursystem that waits for the input; Perl waits for your system. Althoughthe details depend upon your system and its configuration, you cangenerally correct your mistyping with a backspace key before youpress return -- your system handles that, not Perl itself. If youneed more control over the input, get the<tt class="literal">Term::ReadLine</tt> module from CPAN.</p></blockquote><p>The string value of <tt class="literal">&lt;STDIN&gt;</tt> typically has anewline character on the end of it.<a href="#FOOTNOTE-63">[63]</a> So you <em class="emphasis">could</em> do something like this:</p><blockquote class="footnote"> <a name="FOOTNOTE-63" /><p>[63]The exception isif the standard input stream somehow runs out in the middle of aline. But that's not a proper text file, of course!</p></blockquote><blockquote><pre class="code">$line = &lt;STDIN&gt;;if ($line eq "\n") {  print "That was just a blank line!\n";} else {  print "That line of input was: $line";}</pre></blockquote><p>But in practice, you don't often want to keep the newline, soyou need the <tt class="literal">chomp</tt> operator.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_07.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch02_09.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.7. The if Control Structure</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">2.9. The chomp Operator</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><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 + -