📄 ch05_02.htm
字号:
handle runtime errors (exceptions). The code in<em class="replaceable"><tt>block</tt></em> is compiled only once during thecompilation of the main program. If there is a syntax error in theblock, it will produce an error at compile time. If the code in<em class="replaceable"><tt>block</tt></em> produces a runtime error (or if a<tt class="literal">die</tt> statement is encountered), the<tt class="literal">eval</tt> exits, and the error is placed in<tt class="literal">$@</tt>. For example, the following code can be used totrap a divide-by-zero error at runtime:</p><blockquote><pre class="code">eval { $a = 10; $b = 0; $c = $a / $b; # Causes runtime error # Trapped by eval };print $@; # Prints "Illegal division by 0 at # try.pl line 3"</pre></blockquote><p>As with any code in a block, a final semicolon is not required.</p></div><a name="INDEX-939" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="exec"><b>exec</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>exec <em class="emphasis">command</em></pre></td><td align="right" /></tr></table><a name="INDEX-939" />Terminates thecurrently running Perl script and executes the program named in<em class="replaceable"><tt>command</tt></em>. The Perl program does not resumeafter the <tt class="literal">exec</tt> unless the <tt class="literal">exec</tt>cannot be run and produces an error. Unlike<tt class="literal">system</tt>, the executed<em class="replaceable"><tt>command</tt></em> is not forked off into a childprocess. An <tt class="literal">exec</tt> completely replaces the script inits current process.</p><p><em class="replaceable"><tt>command</tt></em> may be a scalar containing astring with the name of the program to run and any arguments. Thisstring is checked for shell metacharacters, and if there are any,passes the string to <tt class="literal">/bin/sh/ -c</tt> for parsing.Otherwise, the string is read as a program command, bypassing anyshell processing. The first word of the string is used as the programname, with any remaining words used as arguments.</p><p><em class="replaceable"><tt>command</tt></em> may also be a list value in whichthe first element is parsed as the program name and remainingelements as arguments. For example:</p><blockquote><pre class="code">exec 'echo', 'Your arguments are: ', @ARGV;</pre></blockquote><p>The <tt class="literal">exec</tt> function is not implemented for Perl onWin32 platforms.</p></div><a name="INDEX-940" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="exists"><b>exists</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>exists $<em class="emphasis">hash</em>{$<em class="emphasis">key</em>}</pre></td><td align="right" /></tr></table><a name="INDEX-940" />Returns true if thespecified hash key exists, even if the corresponding value isundefined.</p></div><a name="INDEX-941" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="exit"><b>exit</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>exit <em class="emphasis">status</em></pre></td><td align="right" /></tr></table><a name="INDEX-941" />Exits the currentPerl process immediately with that value given by<em class="replaceable"><tt>status</tt></em>. This could be the entire Perlscript you are running or only a child process created by<tt class="literal">fork</tt>. Here's a fragment that letsa user exit the program by typing <tt class="literal">x</tt> or<tt class="literal">X</tt>:</p><blockquote><pre class="code">$ans = <STDIN>;exit 0 if $ans =~ /^[Xx]/;</pre></blockquote><p>If <em class="replaceable"><tt>status</tt></em> is omitted, the function exitswith <tt class="literal">0</tt>. You shouldn't use<tt class="literal">exit</tt> to abort a subroutine ifthere's any chance that someone might want to trapwhatever error happened. Use <tt class="literal">die</tt> instead, whichcan be trapped by an <tt class="literal">eval</tt>.</p></div><a name="INDEX-942" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="exp"><b>exp</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>exp <em class="emphasis">num</em></pre></td><td align="right" /></tr></table><a name="INDEX-942" />Returns<em class="emphasis">e</em> to the power of<em class="replaceable"><tt>num</em>. If <em class="replaceable">num</tt></em> isomitted, it gives <tt class="literal">exp($_)</tt>. To do generalexponentiation, use the <tt class="literal">**</tt> operator.</p></div><a name="INDEX-943" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="fcntl"><b>fcntl</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>fcntl <em class="emphasis">filehandle</em>, <em class="emphasis">function</em>, <em class="emphasis">arg</em></pre></td><td align="right" /></tr></table><a name="INDEX-943" />Callsthe file control <em class="replaceable"><tt>function</tt></em> (with thefunction-specific <em class="replaceable"><tt>arg</tt></em>) to use on the fileor device opened with <em class="replaceable"><tt>filehandle</tt></em>.<tt class="literal">fcntl</tt> calls Unix's<tt class="literal">fcntl</tt> function (not available on Win32 platforms).If the function is not implemented, the program exits with a fatalerror. <tt class="literal">fcntl</tt> sets file descriptors for afilehandle. This built-in command is usable when you use the Fcntlmodule in the standard distribution:</p><blockquote><pre class="code">use Fcntl;</pre></blockquote><p>This module imports the correct <em class="replaceable"><tt>function</tt></em>definitions. See the description of the Fcntl module in <a href="ch08_01.htm">Chapter 8, "Standard Modules"</a>. </p><p>The return value of <tt class="literal">fcntl</tt> (and<tt class="literal">ioctl</tt>) is as follows:</p><a name="ch05-43-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>System call returns</p></th><th><p>Perl returns</p></th></tr><tr><td><p><tt class="literal">-1</tt></p></td><td><p>Undefined value</p></td></tr><tr><td><p><tt class="literal">0</tt></p></td><td><p>String <tt class="literal">"0 but true"</tt></p></td></tr><tr><td><p>Anything else</p></td><td><p>That number</p></td></tr></table><p>Thus Perl returns true on success and false on failure, yet you canstill easily determine the actual value returned by the operatingsystem.</p></div><a name="INDEX-944" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="fileno"><b>fileno</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>fileno <em class="emphasis">filehandle</em></pre></td><td align="right" /></tr></table><a name="INDEX-944" />Returns the filedescriptor for a filehandle. (A file descriptor is a small integer,unlike the filehandle, which is a symbol.) It returns<tt class="literal">undef</tt> if the handle is not open.It's useful for constructing bitmaps for<tt class="literal">select</tt> and for passing to certain obscure systemcalls if <tt class="literal">syscall</tt> is implemented.It's also useful for double-checking that the<tt class="literal">open</tt> function gave you the file descriptor youwanted.</p></div><a name="INDEX-945" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="flock"><b>flock</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>flock <em class="emphasis">filehandle</em>, <em class="emphasis">operation</em></pre></td><td align="right" /></tr></table><a name="INDEX-945" />Establishes orremoves a lock on a file opened with<em class="replaceable"><tt>filehandle</tt></em>. This function calls one of theUnix functions <tt class="literal">flock</tt>, <tt class="literal">lockf</tt>, orthe locking capabilities of <tt class="literal">fcntl</tt>, whichever yoursystem supports. If none of these functions exist on your system,<tt class="literal">flock</tt> will produce a fatal error.</p><p><em class="replaceable"><tt>operation</tt></em> is the type of locking functionto perform. The number by each operation name is the argument thatPerl's <tt class="literal">flock</tt> takes by default.You may also use the operation names if you explicitly import themfrom the Fcntl module with <tt class="literal">use Fcntl ":flock"</tt>.</p><dl><dt><a name="<tt class="literal">LOCK_SH</tt> (1)"><b><tt class="literal">LOCK_SH</tt> (1)</b></dt><dd>Establishes a shared lock on the file (read lock).</p></dd><dt><a name="<tt class="literal">LOCK_EX</tt> (2)"><b><tt class="literal">LOCK_EX</tt> (2)</b></dt><dd>Establishes an exclusive lock on the file (write lock).</p></dd><dt><a name="<tt class="literal">LOCK_UN</tt> (8)"><b><tt class="literal">LOCK_UN</tt> (8)</b></dt><dd>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -