📄 ch05_02.htm
字号:
concatenated into a single string for output. If<em class="replaceable"><tt>message</tt></em> does not end with a newline(<tt class="literal">\n</tt>), the current script filename, line number,and input line number (if any) are appended to the message with anewline. With no argument, the function outputs the string<tt class="literal">Died</tt> as its default.</p><p><tt class="literal">die</tt> exits the programs with the current value ofthe <tt class="literal">$!</tt> variable, which contains the textdescribing the most recent operating system error value. This valuecan be used in the <em class="replaceable"><tt>message</tt></em> to describewhat the problem may have been.</p><p><tt class="literal">die</tt> behaves differently inside an<tt class="literal">eval</tt> statement. It places the error message in the<tt class="literal">$@</tt> variable and aborts the<tt class="literal">eval</tt>, which returns an undefined value. This useof <tt class="literal">die</tt> can raise runtime exceptions that can becaught at a higher level of the program.</p></div><a name="INDEX-928" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="do"><b>do</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>do {<em class="emphasis">block</em>}</pre></td><td align="right" /></tr></table><a name="INDEX-928" />Executesthe sequence of commands in the <em class="replaceable"><tt>block</tt></em> andreturns the value of the last expression evaluated. When modified bya loop modifier, Perl executes the <em class="replaceable"><tt>block</tt></em>once before testing the loop condition. (On other statements, theloop modifiers test the conditional first.)</p></div><a name="INDEX-929" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="dump"><b>dump</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>dump <em class="emphasis">label</em></pre></td><td align="right" /></tr></table><a name="INDEX-929" />Duringprogram execution, causes an immediate core dump after code previousto it has already been executed. Primarily, this is so that you canuse the <tt class="literal">undump</tt> program to turn your core dump intoan executable binary after having initialized all your variables atthe beginning of the program. Please note that youshouldn't use <tt class="literal">dump( )</tt> or<tt class="literal">undump( )</tt> with the availability of the PerlCompiler (B modules) as part of your Perl distribution. In addition,you're unlikely to have success with<tt class="literal">dump</tt> or <tt class="literal">undump</tt> on most modernUnix systems (that support dynamic loading from<tt class="literal">libdl</tt>). <tt class="literal">dump</tt> arranges for therevived binary, when run, to begin by executing a<tt class="literal">goto</tt> label (with all the restrictions that<tt class="literal">goto</tt> suffers). Think of the operation as a<tt class="literal">goto</tt> with an intervening core dump andreincarnation. If <em class="replaceable"><tt>label</tt></em> is omitted, thefunction arranges for the program to restart from the top. Note thatany files opened at the time of the dump will no longer be open whenthe program is reincarnated. See also the <em class="emphasis">-u</em>command-line switch.</p><p>As of Perl 5.8, <tt class="literal">dump</tt> is now largely obsolete,partly because it's difficult to convert a core fileinto an executable, and because the real compiler backends forgenerating portable bytecode and compilable C code have supersededit. That's why you should now invoke it as <tt class="literal">"CORE::dump()"</tt> if you don't want to be warned against a possible typo.</p><p>The <tt class="literal">undump</tt> program is not available on all systemsand may not be compatible with specific ports of Perl.</p></div><a name="INDEX-930" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="each"><b>each</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>each %<em class="emphasis">hash</em></pre></td><td align="right" /></tr></table><a name="INDEX-930" />Returns a two-elementlist consisting of the key and value for the next element of a hash.With successive calls to <tt class="literal">each</tt>, you can iterateover the entire hash. Entries are returned in an indeterminate order.When the hash is entirely read, a null list is returned. The nextcall to <tt class="literal">each</tt> after that will start a newiteration. The iterator can be reset either by reading all theelements from the hash or by calling the <tt class="literal">keys</tt>function in scalar context. You must not add elements to the hashwhile iterating over it, although you are permitted to use<tt class="literal">delete</tt>. In a scalar context,<tt class="literal">each</tt> returns only the key.</p><p>There is a single iterator for each hash, shared by all<tt class="literal">each</tt>, <tt class="literal">keys</tt>, and<tt class="literal">values</tt> function calls in the program. This meansthat after a <tt class="literal">keys</tt> or <tt class="literal">values</tt>call, the next <tt class="literal">each</tt> call will start again from thebeginning.</p></div><a name="INDEX-931" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="endgrent"><b>endgrent</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>endgrent</pre></td><td align="right" /></tr></table><a name="INDEX-931" />Closes the groupsfile (usually <em class="emphasis">/etc/group</em> on Unix systems) ifopen. Not implemented on Win32 systems.</p></div><a name="INDEX-932" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="endhostent"><b>endhostent</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>endhostent</pre></td><td align="right" /></tr></table><a name="INDEX-932" />Closes thehosts file (usually <em class="emphasis">/etc/hosts</em> on Unix systems)if open. Not implemented on Win32 systems.</p></div><a name="INDEX-933" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="endnetent"><b>endnetent</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>endnetent</pre></td><td align="right" /></tr></table><a name="INDEX-933" />Closes thenetworks file (usually <em class="emphasis">/etc/networks</em> on Unixsystems) if open. Not implemented on Win32 systems.</p></div><a name="INDEX-934" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="endprotoent"><b>endprotoent</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>endprotoent</pre></td><td align="right" /></tr></table><a name="INDEX-934" />Closes theprototypes file (usually <em class="emphasis">/etc/prototypes</em> on Unixsystems) if open. Not implemented on Win32 systems.</p></div><a name="INDEX-935" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="endpwent"><b>endpwent</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>endpwent</pre></td><td align="right" /></tr></table><a name="INDEX-935" />Closes thepassword file (<em class="emphasis">/etc/passwd</em> or equivalent on Unixsystems) if open. Not implemented on Win32 systems.</p></div><a name="INDEX-936" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="endservent"><b>endservent</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>endservent</pre></td><td align="right" /></tr></table><a name="INDEX-936" />Closes theservices file (usually <em class="emphasis">/etc/services</em> on Unixsystems) if open. Not implemented on Win32 systems.</p></div><a name="INDEX-937" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="eof"><b>eof</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>eof <em class="emphasis">filehandle</em>eof( )</pre></td><td align="right" /></tr></table><a name="INDEX-937" />Returns true if thenext read on <em class="replaceable"><tt>filehandle</tt></em> will returnend-of-file, or if <em class="replaceable"><tt>filehandle</tt></em> is not open.<em class="replaceable"><tt>filehandle</tt></em> may be an expression with avalue that gives the real filehandle name. An <tt class="literal">eof</tt>without an argument returns the end-of-file status for the last fileread. Empty parentheses <tt class="literal">( )</tt> may be used inconnection with the combined files listed on the command line. Thatis, inside a <tt class="literal">while (<>)</tt> loop, <tt class="literal">eof()</tt> will detect the end of only the last of a group of files.Use <tt class="literal">eof(ARGV)</tt> or <tt class="literal">eof</tt> (withoutparentheses) to test each file in a <tt class="literal">while(<>)</tt> loop. For example, the following code insertsdashes just before the last line of the last file:</p><blockquote><pre class="code">while (<>) { if (eof( )) { print "-" x 30, "\n"; } print;}</pre></blockquote></div><a name="INDEX-938" /><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><a name="eval"><b>eval</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>eval <em class="emphasis">string</em>eval {<em class="emphasis">block</em>}</pre></td><td align="right" /></tr></table><a name="INDEX-938" />Evaluates theexpression or code in its argument at runtime as a separate Perlprogram within the context of the larger script. Any variablesettings remain afterward, as do any subroutine or formatdefinitions. The code of the <tt class="literal">eval</tt> is treated as ablock, so any locally scoped variables declared within the<tt class="literal">eval</tt> last only until the <tt class="literal">eval</tt>is done. (See also <a href="ch05_02.htm#local">local</a> and<a href="ch05_02.htm#my">my</a>.) The value returned from an<tt class="literal">eval</tt> is the value of the last expressionevaluated. Like subroutines, you may also use the<tt class="literal">return</tt> function to return a value and exit the<tt class="literal">eval</tt>.</p><p>With <tt class="literal">eval</tt> <em class="replaceable"><tt>string</tt></em>, thecontents of <em class="replaceable"><tt>string</tt></em> are compiled andexecuted at runtime. For example:</p><blockquote><pre class="code">$a = 3, $b = 4;$c = '$a * $b';print (eval "$c"); # Prints 12</pre></blockquote><p>The string form of <tt class="literal">eval</tt> is useful for executingstrings produced at runtime from standard or other dynamic inputsources. If the string produces an error, either from syntax or atruntime, the <tt class="literal">eval</tt> exits with the undefined valueand places the error in <tt class="literal">$@</tt>. If<em class="replaceable"><tt>string</tt></em> is omitted, the operator evaluates<tt class="literal">$_</tt>.</p><p>The block form of <tt class="literal">eval</tt> is used in Perl programs to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -