📄 ch29_02.htm
字号:
<html><head><title>Perl Functions in Alphabetical Order (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Perl Functions in Alphabetical Order"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch29_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch29_01.htm">Chapter 29: Functions</a></td><td align="right" valign="top" width="172"><a href="ch30_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">29.2. Perl Functions in Alphabetical Order</h2><p>Many of the following function names are annotated with, um, annotations.Here are their meanings:</p><dl><dt><b><tt class="literal"><img border=0 src="figs/dollarunderscore.gif"></tt></b></dt><dd><p>Uses <tt class="literal">$_</tt> (<tt class="literal">$ARG</tt>) as a default variable.</p></dd><dt><b><tt class="literal"><img src="figs/dollarbang.gif"></tt></b></dt><dd><p>Sets <tt class="literal">$!</tt> (<tt class="literal">$OS_ERROR</tt>) on syscall errors.</p></dd><dt><b><tt class="literal"><img src="figs/dollarat.gif"></tt></b></dt><dd><p>Raises exceptions; use <tt class="literal">eval</tt> to trap <tt class="literal">$@</tt> (<tt class="literal">$EVAL_ERROR</tt>).</p></dd><dt><b><tt class="literal"><img src="figs/dollarquestion.gif"></tt></b></dt><dd><p>Sets <tt class="literal">$?</tt> (<tt class="literal">$CHILD_ERROR</tt>) when child process exits.</p></dd><dt><b><tt class="literal"><img src="figs/taint.gif"></tt></b></dt><dd><p>Taints returned data.</p></dd><dt><b><tt class="literal"><img src="figs/taintgrey.gif"></tt></b></dt><dd><p>Taints returned data under some system, locale, or handle settings.</p></dd><dt><b><tt class="literal"><img src="figs/xarg.gif"></tt></b></dt><dd><p>Raises an exception if given an argument of inappropriate type.</p></dd><dt><b><tt class="literal"><img src="figs/xro.gif"></tt></b></dt><dd><p>Raises an exception if modifying a read-only target.</p></dd><dt><b><tt class="literal"><img src="figs/xt.gif"></tt></b></dt><dd><p>Raises an exception if fed tainted data.</p></dd><dt><b><tt class="literal"><img src="figs/xu.gif"></tt></b></dt><dd><p>Raises an exception if unimplemented on current platform.</p></dd></dl><p><a name="INDEX-4625"></a><a name="INDEX-4626"></a>Functions that return tainted data when fed tainted data are notmarked, since that's most of them. In particular, if you use anyfunction on <tt class="literal">%ENV</tt> or <tt class="literal">@ARGV</tt>, you'll get tainted data.</p><p><a name="INDEX-4627"></a>Functions marked with <tt class="literal"><img src="figs/xarg.gif"></tt> raise an exception when they require, but do not receive, an argument of a particular type (such as filehandles for I/Ooperations, references for <tt class="literal">bless</tt>ing, etc.).</p><p>Functions marked with <tt class="literal"><img src="figs/xro.gif"></tt> sometimes need to alter their arguments.If they can't modify the argument because it's marked read-only,they'll raise an exception. Examples of read-only variables arethe special variables containing data captured during a patternmatch and variables that are really aliases to constants.</p><p>Functions marked with <tt class="literal"><img src="figs/xu.gif"></tt> may not be implemented on all platforms.Although many of these are named after functions in the Unix Clibrary, don't assume that just because you aren't running Unix,you can't call any of them. Many are emulated, eventhose you might never expect to see--such as <tt class="literal">fork</tt> on Win32systems, which works as of the 5.6 release of Perl. For moreinformation about the portability and behavior of system-specificfunctions, see the <em class="emphasis">perlport</em> manpage, plus any platform-specific documentation that came withyour Perl port.<a name="INDEX-4628"></a><a name="INDEX-4629"></a></p><p>Functions that raise other miscellaneous exceptions are marked with <tt class="literal"><img src="figs/dollarat.gif"></tt>,including math functions that throw range errors, such as <tt class="literal">sqrt(-1)</tt>.</p><h3 class="sect2">29.2.1. abs <img src="figs/dollarunderscore.gif"></h3><p><blockquote><pre class="programlisting">abs <em class="replaceable">VALUE</em>abs</pre></blockquote><a name="INDEX-4630"></a>This function returns the absolute value of its argument.<blockquote><pre class="programlisting">$diff = abs($first - $second);</pre></blockquote>Note: here and in subsequent examples, good style (and the <tt class="literal">use strict</tt> pragma) would dictate that you add a <tt class="literal">my</tt> modifier to declarea new lexically scoped variable, like this:<blockquote><pre class="programlisting">my $diff = abs($first - $second);</pre></blockquote>However, we've omitted <tt class="literal">my</tt> from most of our examples for clarity.Just assume that any such variable was declared earlier, if that cranksyour rotor.</p><h3 class="sect2">29.2.2. accept <img src="figs/dollarunderscore.gif"> <img src="figs/xarg.gif"> <img src="figs/xu.gif"></h3><p><blockquote><pre class="programlisting">accept <em class="replaceable">SOCKET</em>, <em class="replaceable">PROTOSOCKET</em></pre></blockquote><a name="INDEX-4631"></a><a name="INDEX-4632"></a>This function is used by server processes that wish to listen forsocket connections from clients.<em class="replaceable">PROTOSOCKET</em> must be a filehandle alreadyopened via the <tt class="literal">socket</tt> operator and bound to one ofthe server's network addresses or to <tt class="literal">INADDR_ANY</tt>.Execution is suspended until a connection is made, at which point the<em class="replaceable">SOCKET</em> filehandle is opened and attached tothe newly made connection. The original<em class="replaceable">PROTOSOCKET</em> remains unchanged; its onlypurpose is to be cloned into a real socket. The function returns theconnected address if the call succeeds, false otherwise. For example:<blockquote><pre class="programlisting">unless ($peer = accept(SOCK, PROTOSOCK)) { die "Can't accept a connection: $!\n";}</pre></blockquote>On systems that support it, the close-on-exec flag will be set for thenewly opened file descriptor, as determined by the value of<tt class="literal">$^F</tt> (<tt class="literal">$SYSTEM_FD_MAX</tt>).</p><p>See <em class="emphasis">accept</em>(2).See also the example in the section "Sockets" in<a href="ch16_01.htm">Chapter 16, "Interprocess Communication"</a>.</p><h3 class="sect2">29.2.3. alarm <img src="figs/dollarunderscore.gif"> <img src="figs/xu.gif"></h3><p><blockquote><pre class="programlisting">alarm <em class="replaceable">EXPR</em>alarm</pre></blockquote><a name="INDEX-4633"></a><a name="INDEX-4634"></a><a name="INDEX-4635"></a><a name="INDEX-4636"></a><a name="INDEX-4637"></a>This function sends a <tt class="literal">SIGALRM</tt> signal to the current processafter <em class="replaceable">EXPR</em> seconds.</p><p>Only one timer may be active at once. Each call disables the previoustimer, and an <em class="replaceable">EXPR</em> of 0 may be supplied tocancel the previous timer without starting a new one. The returnvalue is the amount of time remaining on the previous timer.<blockquote><pre class="programlisting">print "Answer me within one minute, or die: ";alarm(60); # kill program in one minute$answer = <STDIN>;$timeleft = alarm(0); # clear alarmprint "You had $timeleft seconds remaining\n";</pre></blockquote>It is usually a mistake to intermix <tt class="literal">alarm</tt> and <tt class="literal">sleep</tt> calls,because many systems use the <em class="emphasis">alarm</em>(2) syscall mechanism to implement<em class="emphasis">sleep</em>(3). On older machines, the elapsed time may be up to onesecond less than you specified because of how seconds are counted.Additionally, a busy system may not get around to running your processimmediately. See <a href="ch16_01.htm">Chapter 16, "Interprocess Communication"</a> for information on signal handling.</p><p>For alarms of finer granularity than one second, you might be ableto use the <tt class="literal">syscall</tt> function to access <em class="emphasis">setitimer</em>(2) if yoursystem supports it. The CPAN module, <tt class="literal">Timer::HiRes</tt>, also providesfunctions for this purpose.<a name="INDEX-4638"></a></p><h3 class="sect2">29.2.4. atan2 </h3><p><blockquote><pre class="programlisting">atan2 <em class="replaceable">Y</em>, <em class="replaceable">X</em></pre></blockquote><a name="INDEX-4639"></a><a name="INDEX-4640"></a><a name="INDEX-4641"></a><a name="INDEX-4642"></a><a name="INDEX-4643"></a>This function returns the principal value of the arc tangent of<em class="replaceable">Y</em><tt class="literal">/</tt><em class="replaceable">X</em> in the range -<img src="figs/pi.gif"> to <img src="figs/pi.gif">. A quick way to get anapproximate value of <img src="figs/pi.gif"> is to say:<blockquote><pre class="programlisting">$pi = atan2(1,1) * 4;</pre></blockquote>For the tangent operation, you may use the <tt class="literal">tan</tt> function fromeither the <tt class="literal">Math::Trig</tt> or the <tt class="literal">POSIX</tt> modules, or just use the familiarrelation:<blockquote><pre class="programlisting">sub tan { sin($_[0]) / cos($_[0]) }</pre></blockquote></p><h3 class="sect2">29.2.5. bind <img src="figs/dollarbang.gif"> <img src="figs/xarg.gif"> <img src="figs/xu.gif"> <img src="figs/xt.gif"></h3><p><blockquote><pre class="programlisting">bind <em class="replaceable">SOCKET</em>, <em class="replaceable">NAME</em></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -