📄 ch02_06.htm
字号:
<html><head><title>Output with print (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 & 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_05.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_07.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.6. Output with print </h2><p>It's generally a good idea to have your program produce someoutput; otherwise, someone may think it didn't do anything. The<tt class="literal">print( )</tt><a name="INDEX-197" /> operator makes this possible. It takesa scalar argument and puts it out without any embellishment ontostandard output. Unless you've done something odd, this will beyour terminal display. For example:</p><blockquote><pre class="code">print "hello world\n"; # say hello world, followed by a newlineprint "The answer is ";print 6 * 7;print ".\n";</pre></blockquote><p>You can actually give <tt class="literal">print</tt> a series of values,separated by commas.</p><blockquote><pre class="code">print "The answer is ", 6 * 7, ".\n";</pre></blockquote><p>This is actually a <em class="emphasis">list</em>, but we haven'ttalked about lists yet, so we'll put that off for later.</p><a name="lperl3-CHP-2-SECT-6.1" /><div class="sect2"><h3 class="sect2">2.6.1. Interpolation of Scalar Variables into Strings</h3><p>When a string literal is double-quoted, it is subject to<em class="emphasis">variable interpolation<a href="#FOOTNOTE-55">[55]</a></em><a name="INDEX-198" /> <a name="INDEX-199" /> <a name="INDEX-200" /> (besides being checked for backslashescapes). This means that any scalar variable<a href="#FOOTNOTE-56">[56]</a> name in the string is replaced with itscurrent value. For example:</p><blockquote class="footnote"> <a name="FOOTNOTE-55" /><p>[55]This has nothing todo with mathematical or statistical interpolation.</p></blockquote><blockquote class="footnote"> <a name="FOOTNOTE-56" /><p>[56]Andsome other variable types, but we won't see those untillater.</p> </blockquote><blockquote><pre class="code">$meal = "brontosaurus steak";$barney = "fred ate a $meal"; # $barney is now "fred ate a brontosaurus steak"$barney = 'fred ate a ' . $meal; # another way to write that</pre></blockquote><p>As you see on the last line above, you can get the same resultswithout the double quotes. But the double-quoted string is often themore convenient way to write it.</p><p>If the scalar variable has never been given a value,<a href="#FOOTNOTE-57">[57]</a> the emptystring is used instead:</p><blockquote class="footnote"><a name="FOOTNOTE-57" /><p>[57]This is actually the special undefined value,<tt class="literal">undef</tt>, which we'll see a little later inthis chapter. If warnings are turned on, Perl will complain aboutinterpolating the undefined value.</p> </blockquote><blockquote><pre class="code">$barney = "fred ate a $meat"; # $barney is now "fred ate a "</pre></blockquote><p>Don't bother with interpolating if you have just the one lonevariable:</p><blockquote><pre class="code">print "$fred"; # unneeded quote marksprint $fred; # better style</pre></blockquote><p>There's nothing really <em class="emphasis">wrong</em> with puttingquote marks around a lone variable, but the other programmers willlaugh at you behind your back.<a href="#FOOTNOTE-58">[58]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-58" /><p>[58]Well, it may force avalue to be interpreted as a string, rather than a number. In a fewrare cases that may be needed, but nearly always it's just awaste of typing.</p> </blockquote><p><em class="emphasis">Variable interpolation</em> is also known as<em class="emphasis">double-quoteinterpolation</em><a name="INDEX-201" />,because it happens when double-quote marks (but not single quotes)are used. It happens for some other strings in Perl, whichwe'll mention as we get to them.</p><p>To put a <a name="INDEX-202" /><a name="INDEX-203" /> <a name="INDEX-204" />real dollar sign intoa double-quoted string, precede the dollar sign with a backslash,which turns off the dollar sign's special significance:</p><blockquote><pre class="code">$fred = 'hello';print "The name is \$fred.\n"; # prints a dollar signprint 'The name is $fred' . "\n"; # so does this</pre></blockquote><p>The variable name will be the longest possible variable name thatmakes sense at that part of the string. This can be a problem if youwant to follow the replaced value immediately with some constant textthat begins with a letter, digit, or underscore.<a href="#FOOTNOTE-59">[59]</a> As Perl scansfor variable names, it would consider those characters to beadditional name characters, which is not what you want. Perl providesa delimiter for the variable name in a manner similar to the shell.Simply enclose the<em class="emphasis">name</em><a name="INDEX-205" /><a name="INDEX-206" /> of the variable in a pair of<a name="INDEX-207" /> <a name="INDEX-208" />curly braces. Or,you can end that part of the string and start another part of thestring with a concatenation operator:</p><blockquote class="footnote"><a name="FOOTNOTE-59" /><p>[59]There are some other characters that may be a problem as well.If you need a left square bracket or a left curly brace just after ascalar variable's name, precede it with a backslash. You mayalso do that if the variable's name is followed by anapostrophe or a pair of colons, or you could use the curly-bracemethod described in the main text</p> </blockquote><blockquote><pre class="code">$what = "brontosaurus steak";$n = 3;print "fred ate $n $whats.\n"; # not the steaks, but the value of $whatsprint "fred ate $n ${what}s.\n"; # now uses $whatprint "fred ate $n $what" . "s.\n"; # another way to do itprint 'fred ate ' . $n . ' ' . $what . "s.\n"; # an especially difficult way</pre></blockquote></div><a name="lperl3-CHP-2-SECT-6.2" /><div class="sect2"><h3 class="sect2">2.6.2. Operator Precedence and Associativity</h3><p><a name="INDEX-209" /><a name="INDEX-210" /> <a name="INDEX-211" /><a name="INDEX-212" /> <a name="INDEX-213" />Operatorprecedence determines which operations in a complex group ofoperations happen first. For example, in the expression<tt class="literal">2+3*4</tt>, do we perform the addition first or themultiplication first? If we did the addition first, we'd get<tt class="literal">5*4</tt>, or <tt class="literal">20</tt>. But if we did themultiplication first (as we were taught in math class), we'dget <tt class="literal">2+12</tt>, or <tt class="literal">14</tt>. Fortunately,Perl chooses the common mathematical definition, performing themultiplication first. Because of this, we say multiplication has a<em class="emphasis">higher precedence</em> than addition.</p><p>You can override the default precedence order by using<a name="INDEX-214" /> <a name="INDEX-215" />parentheses.Anything in parentheses is completely computed before the operatoroutside of the parentheses is applied (just like you learned in mathclass). So if I really want the addition before the multiplication, Ican say <tt class="literal">(2+3)*4</tt>, yielding <tt class="literal">20</tt>.Also, if I wanted to demonstrate that multiplication is performedbefore addition, I could add a decorative but unnecessary set ofparentheses, as in <tt class="literal">2+(3*4)</tt>.</p><p>While precedence is simple for addition and multiplication, we startrunning into problems when faced with, say, string concatenationcompared with exponentiation. The proper way to resolve this is toconsult the official, accept-no-substitutes Perl operator precedencechart, shown in <a href="ch02_03.htm#lperl3-CHP-2-TABLE-1">Table 2-1</a>.<a href="#FOOTNOTE-60">[60]</a> (Note that some of the operators have not yet beendescribed, and in fact, may not even appear anywhere in this book,but don't let that scare you from reading about them in the<em class="emphasis">perlop</em> manpage.)</p><blockquote class="footnote"> <a name="FOOTNOTE-60" /><p>[60]Cprogrammers: Rejoice! The operators that are available in both Perland C have the same precedence and associativity in both.</p></blockquote><a name="lperl3-CHP-2-TABLE-2" /><h4 class="objtitle">Table 2-2. Associativity and precedence of operators (highest to lowest) </h4><table border="1"><tr><th><p>Associativity</p></th><th><p>Operators</p></th></tr><tr><td><p><a name="INDEX-216" />left</p></td><td><p>parentheses and arguments to list operators</p></td></tr><tr><td><p><a name="INDEX-217" />left</p></td><td><p><tt class="literal">-></tt></p></td></tr><tr><td> </td><td><p><tt class="literal">++</tt> <tt class="literal">--</tt> (autoincrement andautodecrement)</p></td></tr><tr><td><p>right</p></td><td><p><tt class="literal">**</tt></p></td></tr><tr><td><p>right</p></td><td><p><tt class="literal">\</tt> <tt class="literal">!</tt> <tt class="literal">~</tt><tt class="literal">+</tt> <tt class="literal">-</tt> (unary operators)</p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">=~</tt> <tt class="literal">!~</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">*</tt> <tt class="literal">/</tt> <tt class="literal">%</tt><tt class="literal">x</tt></p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal">+</tt> <tt class="literal">-</tt> <tt class="literal">.</tt>(binary operators)</p></td></tr><tr><td><p>left</p></td><td><p><tt class="literal"><<</tt> <tt class="literal">>></tt></p></td></tr><tr><td> </td><td><p>named unary operators (<tt class="literal">-X</tt> filetests,<tt class="literal">rand</tt>)</p></td></tr><tr><td> </td><td><p><tt class="literal"><</tt> <tt class="literal"><=</tt><tt class="literal">></tt> <tt class="literal">>=</tt><tt class="literal">lt</tt> <tt class="literal">le</tt> <tt class="literal">gt</tt><tt class="literal">ge</tt> (the "unequal" ones)</p></td></tr><tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -