📄 ch05_02.htm
字号:
<html><head><title>Perl Functions in Alphabetical Order (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><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="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl in a Nutshell" /><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="ch05_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch06_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">5.2. Perl Functions in Alphabetical Order</h2><a name="INDEX-901" /><a name="INDEX-902" /><a name="INDEX-903" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="abs"><b>abs</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>abs <em class="emphasis">value</em></pre></td><td align="right" /></tr></table><a name="INDEX-903" />Returns the absolute value of itsargument (or <tt class="literal">$_</tt> if omitted).</p></div><a name="INDEX-904" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="accept"><b>accept</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>accept <em class="emphasis">newsocket</em>, <em class="emphasis">genericsocket</em></pre></td><td align="right" /></tr></table><a name="INDEX-904" />Readies a server process to acceptsocket connections from clients. Execution is suspended until aconnection is made, at which time the<em class="replaceable"><tt>newsocket</tt></em> filehandle is opened andattached to the newly made connection. The function returns theconnected address if the call succeeds or false otherwise (and itputs the error code into <tt class="literal">$!</tt>).<em class="replaceable"><tt>genericsocket</tt></em> must be a filehandle alreadyopened via the <tt class="literal">socket</tt> function and bound to one ofthe server's network addresses.</p></div><a name="INDEX-905" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="alarm"><b>alarm</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>alarm <em class="emphasis">n</em></pre></td><td align="right" /></tr></table><a name="INDEX-905" />Sends a<tt class="literal">SIGALRM</tt> signal to the executing Perl program after<em class="replaceable"><tt>n</tt></em> seconds. On some older systems, alarmsgo off "at the top of the second,"so, for instance, an <tt class="literal">alarm 1</tt> may go off anywherebetween 0 to 1 seconds from now, depending on when in the currentsecond it is. An <tt class="literal">alarm 2</tt> may go off anywhere from1 to 2 seconds from now. And so on.</p><p>Each call disables the previous timer, and an argument of<tt class="literal">0</tt> may be supplied to cancel the previous timerwithout starting a new one. The return value is the number of secondsremaining on the previous timer.</p></div><a name="INDEX-906" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="atan2"><b>atan2</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>atan2 <em class="emphasis">y</em>, <em class="emphasis">x</em></pre></td><td align="right" /></tr></table><a name="INDEX-906" />Returns thearctangent of<em class="replaceable"><tt>y</em><tt class="literal">/</tt><em class="replaceable">x</tt></em>in the range -<img src="figs/U03C0.gif" alt="Figure 5.4" width="10" height="8" /> to <img src="figs/U03C0.gif" alt="Figure 5.4" width="10" height="8" />. A quick way to get anapproximate value of <img src="figs/U03C0.gif" alt="Figure 5.4" width="10" height="8" /> is to say:</p><blockquote><pre class="code">$pi = atan2(1,1) * 4;</pre></blockquote><p>For the tangent operation, you may use the <tt class="literal">POSIX::tan()</tt> function, or use the familiar relation:</p><blockquote><pre class="code">sub tan { sin($_[0]) / cos($_[0]) }</pre></blockquote></div><a name="INDEX-907" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="bind"><b>bind</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>bind <em class="emphasis">socket</em>, <em class="emphasis">address</em></pre></td><td align="right" /></tr></table><a name="INDEX-907" />Attaches an address to analready opened socket specified by the<em class="replaceable"><tt>socket</tt></em> filehandle. The function returnstrue for success, false otherwise (and puts the error code into<tt class="literal">$!</tt>). <em class="replaceable"><tt>address</tt></em> should bea packed address of the proper type for the socket.</p></div><a name="INDEX-908" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="binmode"><b>binmode</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>binmode <em class="emphasis">filehandle</em></pre></td><td align="right" /></tr></table><a name="INDEX-908" />Arranges for thefile to be treated in binary mode on operating systems thatdistinguish between binary and text files. It should be called after<tt class="literal">open</tt> but before any I/O is done on the filehandle.The only way to reset binary mode on a filehandle is to reopen thefile.</p><p><tt class="literal">binmode</tt> has no effect under Unix, Plan9, or othersystems that use a single <tt class="literal">\n</tt> (newline) characteras a line delimiter. On systems such as Win32 or MS-DOS,<tt class="literal">binmode</tt> is needed to prevent the translation ofthe line delimiter characters to and from <tt class="literal">\n</tt>.</p></div><a name="INDEX-909" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="bless"><b>bless</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>bless $<em class="emphasis">ref</em>, [<em class="emphasis">classname</em>]</pre></td><td align="right" /></tr></table><a name="INDEX-909" />Looks up the itempointed to by reference <em class="replaceable"><tt>ref</tt></em> and tells theitem that it is now an object in the<em class="replaceable"><tt>classname</tt></em> package—or the currentpackage if no class name is specified. It returns the reference forconvenience, since a <tt class="literal">bless</tt> is often the last thingin a constructor function. (Always use the two-argument version ifthe constructor doing the blessing might be inherited by a derivedclass. In such cases, the class you want to bless your object intowill normally be found as the first argument to the constructor inquestion.)</p></div><a name="INDEX-910" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="caller"><b>caller</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>caller [<em class="emphasis">n</em>]</pre></td><td align="right" /></tr></table><a name="INDEX-910" />Returns informationabout the stack of current subroutine calls. Without an argument, itreturns the package name in a scalar context; in a list context, itreturns the package name, filename, and line number from which thecurrently executing subroutine was called:</p><blockquote><pre class="code">($package, $filename, $line) = caller;</pre></blockquote><p>With an argument it evaluates <em class="replaceable"><tt>n</tt></em> as thenumber of stack frames to go back before the current one. It alsoreports some additional information that the debugger uses to print astack trace:</p><blockquote><pre class="code">$i = 0;while (($pack, $file, $line, $subname, $hasargs, $wantarray, $evaltext, $is_require) = caller($i++)) { ...}</pre></blockquote><p>Furthermore, when called from within the DB package,<tt class="literal">caller</tt> returns more detailed information: it setsthe list variable <tt class="literal">@DB::args</tt> as the argument passedin the given stack frame.</p></div><a name="INDEX-911" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="chdir"><b>chdir</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>chdir <em class="emphasis">dirname</em></pre></td><td align="right" /></tr></table><a name="INDEX-911" />Changes the workingdirectory to <em class="replaceable"><tt>dirname</tt></em>, if possible. If<em class="replaceable"><tt>dirname</tt></em> is omitted, it changes to the homedirectory. The function returns <tt class="literal">1</tt> upon success,<tt class="literal">0</tt> otherwise (and puts the error code into<tt class="literal">$!</tt>).</p></div><a name="INDEX-912" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="chmod"><b>chmod</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>chmod <em class="emphasis">mode</em>, <em class="emphasis">filelist</em></pre></td><td align="right" /></tr></table><a name="INDEX-912" />Changes thepermissions of a list of files. The first argument must be thepermissions mode given in its octal number representation. Thefunction returns the number of files successfully changed. Forexample:</p><blockquote><pre class="code">$cnt = chmod 0755, 'file1', 'file2';</pre></blockquote><p>will set <tt class="literal">$cnt</tt> to <tt class="literal">0</tt>,<tt class="literal">1</tt>, or <tt class="literal">2</tt>, depending on how manyfiles got changed (in the sense that the operation succeeded, not inthe sense that the bits were different afterward).</p></div><a name="INDEX-913" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="chomp"><b>chomp</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>chomp $<em class="emphasis">var</em>chomp @<em class="emphasis">list</em></pre></td><td align="right" /></tr></table><a name="INDEX-913" />Removes anyline-ending characters of a string in<tt class="literal">$</tt><em class="replaceable"><tt>var</tt></em>, or each string in<tt class="literal">@</tt><em class="replaceable"><tt>list</tt></em>, that correspondto the current value of <tt class="literal">$/</tt> (not just any lastcharacter, as <tt class="literal">chop</tt> does). <tt class="literal">chomp</tt>returns the number of characters deleted. If <tt class="literal">$/</tt> isempty (in paragraph mode), <tt class="literal">chomp</tt> removes allnewlines from the selected string (or strings, if<tt class="literal">chomp</tt>ing a list). If no argument is given, thefunction <tt class="literal">chomp</tt>s the <tt class="literal">$_</tt>variable.</p></div><a name="INDEX-914" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="chop"><b>chop</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>chop $<em class="emphasis">var</em>chop @<em class="emphasis">list</em></pre></td><td align="right" /></tr></table><a name="INDEX-914" />Chops off the lastcharacter of a string contained in the variable<tt class="literal">$</tt><em class="replaceable"><tt>var</tt></em> (or strings ineach element of a<tt class="literal">@</tt><em class="replaceable"><tt>list</tt></em>) and returns thecharacter chopped. The <tt class="literal">chop</tt> operator is usedprimarily to remove the newline from the end of an input record butis more efficient than <tt class="literal">s/\n$//</tt>. If no argument isgiven, the function chops the <tt class="literal">$_</tt> variable.</p></div><a name="INDEX-915" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="chown"><b>chown</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><table width="515" border="0" cellpadding="5"><tr><td align="left"><pre>chown <em class="emphasis">uid</em>, <em class="emphasis">gid</em>, <em class="emphasis">files</em></pre></td><td align="right" /></tr></table><a name="INDEX-915" />Changes the ownerand group of a list of files. The first two arguments must be the
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -