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

📄 ch24_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 2 页
字号:
You can't take the address of anything, although a similar operator inPerl is the backslash, which creates a reference.</p></li><li><p><tt class="literal">ARGV</tt> must becapitalized. <tt class="literal">$ARGV[0]</tt> is C's<tt class="literal">argv[1]</tt>, and C's <tt class="literal">argv[0]</tt> ends upin <tt class="literal">$0</tt>.<a name="INDEX-4146"></a></p></li><li><p><a name="INDEX-4147"></a>Syscalls such as <tt class="literal">link</tt>, <tt class="literal">unlink</tt>,and <tt class="literal">rename</tt> return true for success, not<tt class="literal">0</tt>.</p></li><li><p><a name="INDEX-4148"></a>The signal handlers in <tt class="literal">%SIG</tt> deal with signal names,not numbers.</p></li></ul><h3 class="sect2">24.1.4. Shell Traps</h3><p><a name="INDEX-4149"></a><a name="INDEX-4150"></a>Sharp shell programmers should take note of the following:</p><ul><li><p><a name="INDEX-4151"></a>Variables are prefixed with <tt class="literal">$</tt>,<tt class="literal">@</tt>, or <tt class="literal">%</tt> on the left side of theassignment as well as the right.  A shellish assignment like:<blockquote><pre class="programlisting">camel='dromedary';      # WRONG</pre></blockquote>won't be parsed the way you expect.  You need:<blockquote><pre class="programlisting">$camel='dromedary';     # ok</pre></blockquote></p></li><li><p><a name="INDEX-4152"></a>The loop variable of a <tt class="literal">foreach</tt> also requires a <tt class="literal">$</tt>. Although <em class="emphasis">csh</em>likes:<blockquote><pre class="programlisting">foreach hump (one two)    stuff_it $humpend</pre></blockquote>in Perl, this is written as:<blockquote><pre class="programlisting">foreach $hump ("one", "two") {    stuff_it($hump);}</pre></blockquote></p></li><li><p><a name="INDEX-4153"></a>The backtick operator does variable interpolation without regard to thepresence of single quotes in the command.</p></li><li><p>The backtick operator does no translation of the return value. In Perl,you have to trim the newline explicitly, like this:<blockquote><pre class="programlisting">chomp($thishost = `hostname`);</pre></blockquote></p></li><li><p><a name="INDEX-4154"></a>Shells (especially <em class="emphasis">csh</em>) do several levels of substitution on eachcommand line.  Perl does interpolation only within certain constructssuch as double quotes, backticks, angle brackets, and search patterns.</p></li><li><p>Shells tend to interpret scripts a little bit at a time.  Perl compilesthe entire program before executing it (except for <tt class="literal">BEGIN</tt> blocks,which execute before the compilation is done).<a name="INDEX-4155"></a></p></li><li><p><a name="INDEX-4156"></a>Program arguments are available via <tt class="literal">@ARGV</tt>, not<tt class="literal">$1</tt>, <tt class="literal">$2</tt>, and so on.</p></li><li><p>The environment is not automatically made available as individual scalarvariables.  Use the standard <tt class="literal">Env</tt> module if you want that to happen.<a name="INDEX-4157"></a></p></li></ul><h3 class="sect2">24.1.5. Previous Perl Traps</h3><p><a name="INDEX-4158"></a><a name="INDEX-4159"></a>Penitent Perl 4 (and Prior) Programmers should take note of thefollowing changes between release 4 and release 5 that might affect oldscripts:</p><ul><li><p><a name="INDEX-4160"></a><tt class="literal">@</tt> now always interpolates an array in double-quotish strings. Someprograms may now need to use backslashes to protect any <tt class="literal">@</tt> thatshouldn't interpolate.</p></li><li><p><a name="INDEX-4161"></a><a name="INDEX-4162"></a>Barewords that used to look like strings to Perl will now look likesubroutine calls if a subroutine by that name is defined before thecompiler sees them.  For example:<blockquote><pre class="programlisting">sub SeeYa { die "Hasta la vista, baby!" }$SIG{'QUIT'} = SeeYa;</pre></blockquote>In prior versions of Perl, that code would set the signal handler.  Now,it actually calls the function!  You may use the <span class="option">-w</span> switch to findsuch risky usage or <tt class="literal">use strict</tt> to outlaw it.</p></li><li><p><a name="INDEX-4163"></a><a name="INDEX-4164"></a>Identifiers starting with "<tt class="literal">_</tt>" are no longer forcedinto package <tt class="literal">main</tt>, except for the bare underscoreitself (as in <tt class="literal">$_</tt>, <tt class="literal">@_</tt>, and so on).</p></li><li><p><a name="INDEX-4165"></a>A double colon is now a valid package separator in an identifier.  Thus,the statement:<blockquote><pre class="programlisting">print "$a::$b::$c\n";</pre></blockquote>now parses <tt class="literal">$a::</tt> as the variable reference, where inprior versions only the <tt class="literal">$a</tt> was considered to be thevariable reference.  Similarly:<blockquote><pre class="programlisting">print "$var::abc::xyz\n";</pre></blockquote>is now interpreted as a single variable<tt class="literal">$var::abc::xyz</tt>, whereas in prior versions, thevariable <tt class="literal">$var</tt> would have been followed by theconstant text <tt class="literal">::abc::xyz</tt>.</p></li><li><p><tt class="literal">s'$pattern'replacement'</tt> now performs no interpolation on<tt class="literal">$pattern</tt>.  (The <tt class="literal">$</tt> would beinterpreted as an end-of-line assertion.) This behavior occurs onlywhen using single quotes as the substitution delimiter; in othersubstitutions, <tt class="literal">$pattern</tt> is always interpolated.<a name="INDEX-4166"></a></p></li><li><p><a name="INDEX-4167"></a>The second and third arguments of <tt class="literal">splice</tt> are nowevaluated in scalar context rather than in list context.</p></li><li><p><a name="INDEX-4168"></a>These are now semantic errors because of precedence:<blockquote><pre class="programlisting">shift @list + 20;    # Now parses like shift(@list + 20), illegal!$n = keys %map + 20; # Now parses like keys(%map + 20), illegal!</pre></blockquote>Because if those were to work, then this couldn't:<blockquote><pre class="programlisting">sleep $dormancy + 20;</pre></blockquote></p></li><li><p><a name="INDEX-4169"></a>The precedence of assignment operators is now the same as the precedenceof assignment.  Previous versions of Perl mistakenly gave them theprecedence of the associated operator.  So you now must parenthesizethem in expressions like:<blockquote><pre class="programlisting">/foo/ ? ($a += 2) : ($a -= 2);</pre></blockquote>Otherwise:<blockquote><pre class="programlisting">/foo/ ? $a += 2 : $a -= 2;</pre></blockquote>would be erroneously parsed as:<blockquote><pre class="programlisting">(/foo/ ? $a += 2 : $a) -= 2;</pre></blockquote>On the other hand:<blockquote><pre class="programlisting">$a += /foo/ ? 1 : 2;</pre></blockquote>now works as a C programmer would expect.</p></li><li><p><tt class="literal">open FOO || die</tt> is incorrect.  You need parentheses around thefilehandle, because <tt class="literal">open</tt> has the precedence of a list operator.</p></li><li><p><a name="INDEX-4170"></a>The elements of argument lists for formats are now evaluated in listcontext.  This means you can interpolate list values now.</p></li><li><p>You can't do a <tt class="literal">goto</tt> into a block that is optimized away.  Darn.</p></li><a name="INDEX-4171"></a><li><p>It is no longer legal to use whitespace as the name of a variable oras a delimiter for any kind of quote construct. Double darn.<a name="INDEX-4172"></a></p></li><li><p><a name="INDEX-4173"></a>The <tt class="literal">caller</tt> function now returns a false value in scalar contextif there is no caller.  This lets modules determine whether they'rebeing required or run directly.</p></li><li><p><tt class="literal">m//g</tt> now attaches its state to the searched string rather than theregular expression.  See <a href="ch05_01.htm">Chapter 5, "Pattern Matching"</a>, for furtherdetails.<a name="INDEX-4174"></a></p></li><li><p><tt class="literal">reverse</tt> is no longer allowed as the name of a <tt class="literal">sort</tt> subroutine.<a name="INDEX-4175"></a><a name="INDEX-4176"></a></p></li><li><p><a name="INDEX-4177"></a><em class="emphasis">taintperl</em> is no longer a separate executable.   There is now a <span class="option">-T</span>switch to turn on tainting when it isn't turned on automatically.</p></li><li><p><a name="INDEX-4178"></a>Double-quoted strings may no longer end with an unescaped <tt class="literal">$</tt> or <tt class="literal">@</tt>.</p></li><li><p>The archaic <tt class="literal">if</tt><em class="replaceable">BLOCK BLOCK</em> syntax is no longer supported.</p></li><li><p>Negative array subscripts now count from the end of the array.<a name="INDEX-4179"></a><a name="INDEX-4180"></a></p></li><li><p>The comma operator in a scalar context is now guaranteed to give ascalar context to its arguments.<a name="INDEX-4181"></a><a name="INDEX-4182"></a></p></li><li><p>The <tt class="literal">**</tt> operator now binds more tightly than unary minus.</p></li><li><p>Setting <tt class="literal">$#array</tt> lower now discards array elements immediately.</p></li><li><p><tt class="literal">delete</tt> is not guaranteed to return the deleted value for <tt class="literal">tie</tt>darrays, since this capability may be onerous for some modules toimplement.<a name="INDEX-4183"></a><a name="INDEX-4184"></a></p></li><li><p>The construct <tt class="literal">"this is $$x"</tt>, which used to interpolate the processID at that point, now tries to dereference <tt class="literal">$x</tt>.  <tt class="literal">$$</tt> by itselfstill works fine, however.</p></li><li><p><a name="INDEX-4185"></a>The behavior of <tt class="literal">foreach</tt> when it iterates over alist that is not an array has changed slightly.  It used to assign thelist to a temporary array but now, for efficiency, no longer does so.This means that you'll now be iterating over the actual values, notcopies of the values.  Modifications to the loop variable can changethe original values, even after the <tt class="literal">grep</tt>!For instance:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl4 -e '@a = (1,2,3); for (grep(/./, @a)) { $_++ }; print "@a\n"'</b></tt>1 2 3% <tt class="userinput"><b>perl5 -e '@a = (1,2,3); for (grep(/./, @a)) { $_++ }; print "@a\n"'</b></tt>2 3 4</pre></blockquote>To retain prior Perl semantics, you'd need to explicitly assign your list to a temporary array and then iterate over that.  Forexample, you might need to change:<blockquote><pre class="programlisting">foreach $var (grep /x/, @list) { ... }</pre></blockquote>to:<blockquote><pre class="programlisting">foreach $var (my @tmp = grep /x/, @list) { ... }</pre></blockquote>Otherwise changing <tt class="literal">$var</tt> will clobber the values of <tt class="literal">@list</tt>.  (Thismost often happens when you use <tt class="literal">$_</tt> for the loop variable and callsubroutines in the loop that don't properly localize <tt class="literal">$_</tt>.)</p></li><li><p>Some error messages and warnings will be different.</p></li><li><p>Some bugs may have been inadvertently removed.<a name="INDEX-4186"></a><a name="INDEX-4187"></a></p></li></ul><a name="INDEX-4188"></a><a name="INDEX-4189"></a><a name="INDEX-4190"></a><a name="INDEX-4191"></a><a name="INDEX-4308"></a><a name="INDEX-4309"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch23_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></a></td><td align="right" valign="top" width="172"><a href="ch24_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">23.3. Handling Insecure Code</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">24.2. Efficiency</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>

⌨️ 快捷键说明

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