📄 ch11_05.htm
字号:
<html><head><title>Reopening a Standard Filehandle (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><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="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><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="ch11_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch11_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">11.5. Reopening a Standard Filehandle</h2><p>We mentioned earlier that if you were to reopen a<a name="INDEX-794" />filehandle(that is, if you were to open a filehandle <tt class="literal">FRED</tt>when you've already got an open filehandle named<tt class="literal">FRED</tt>, say), the old one would be closed for youautomatically. And we said that you shouldn't reuse one of thesix standard filehandle names unless you intended to get thatone's special features. And we also said that the messages from<tt class="literal">die</tt> and <tt class="literal">warn</tt>, along withPerl's internally generated complaints, go automatically to<tt class="literal">STDERR</tt><a name="INDEX-795" />. If you put those three pieces ofinformation together, you now have an idea about how you could send<a name="INDEX-796" />error messages to a file, rather than toyour program's standard error stream:<a href="#FOOTNOTE-261">[261]</a></p><blockquote class="footnote"><a name="FOOTNOTE-261" /><p>[261]Don't do this without a reason. It's nearly alwaysbetter to let the user set up redirection when launching yourprogram, rather than have redirection hardcoded. But this is handy incases where your program is being run automatically by anotherprogram (say, by a web server or a scheduling utility like<tt class="literal">cron</tt> or <tt class="literal">at</tt>). Another reasonmight be that your program is going to start another process(probably with <tt class="literal">system</tt> or <tt class="literal">exec</tt>,which we'll see in <a href="ch14_01.htm">Chapter 14, "Process Management"</a>), and you needthat process to have different I/O connections.</p> </blockquote><blockquote><pre class="code"># Send errors to my private error logopen STDERR, ">>/home/barney/.error_log" or die "Can't open error log for append: $!";</pre></blockquote><p>After reopening <tt class="literal">STDERR</tt>, any error messages fromPerl will go into the new file. But what happens if the <tt class="literal">ordie</tt> part is executed -- where will<em class="emphasis">that</em> message go, if the new file couldn'tbe opened to accept the messages?</p><p>The answer is that if one of the three systemfilehandles -- <tt class="literal">STDIN</tt>,<tt class="literal">STDOUT</tt>, or <tt class="literal">STDERR</tt> -- failsto be reopened, Perl kindly restores the original one.<a href="#FOOTNOTE-262">[262]</a> That is, Perl closes theoriginal one (of those three) only when it sees that opening the newconnection is successful. Thus, this technique could be used toredirect any (or all) of those three system filehandles from insideyour program,<a href="#FOOTNOTE-263">[263]</a> almostas if the program had been run with that I/O redirection from theshell in the first place.<a name="INDEX-797" /></p><blockquote class="footnote"><a name="FOOTNOTE-262" /><p>[262]At least, this is true if you haven't changedPerl's special <tt class="literal">$^F</tt> variable, which tellsPerl that only those three are special like this. But you'dnever change that.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-263" /><p>[263]But don't open<tt class="literal">STDIN</tt> for output or the others for input. Justthinking about that makes our heads hurt.</p> </blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch11_04.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch11_06.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">11.4. Using Filehandles</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">11.6. File Tests</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -