📄 ch08_112.htm
字号:
<html><head><title>FileHandle (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="Java and XSLT" /><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="ch08_111.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="ch08_113.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.112. FileHandle</h2><p><a name="INDEX-1460" />Providesobject methods for working with filehandles. Provides the followingmethods.</p><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">fh</em> = new FileHandle [<em class="replaceable">filename</em>[, <em class="replaceable">mode</em>]]</pre><p>Constructor. Creates a FileHandle, which is a reference to a newlycreated symbol. The optional parameters,<em class="replaceable"><tt>filename</tt></em> and<em class="replaceable"><tt>mode</tt></em>, are passed to<tt class="literal">open</tt>. The FileHandle object is returned if theopen succeeds; otherwise, it is destroyed.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>new_from_fd</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">fh</em> = new_from_fd <em class="replaceable">FileHandle</em> <em class="replaceable">fd</em>, <em class="replaceable">mode</em></pre><p>Constructor. Creates a FileHandle, but it takes the file descriptor,<em class="replaceable"><tt>fd</tt></em>, instead of<em class="replaceable"><tt>filename</tt></em> as a parameter, along with<em class="replaceable"><tt>mode</tt></em>; the parameters are required.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>fdopen</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">fh</em>->fdopen <em class="replaceable">fdname</em> [<em class="replaceable">openmode</em>]</pre><p>Like <tt class="literal">open</tt>, except that its first parameter is nota filename but a filehandle name, a FileHandle object, or a filedescriptor number.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>getpos</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">pos</em> = $<em class="replaceable">fh</em>->getpos</pre><p>If the C functions <tt class="literal">fgetpos(3)</tt> and<tt class="literal">fsetpos(3)</tt> are available,<tt class="literal">getpos</tt> returns the current position,<tt class="literal">$</tt><em class="replaceable"><tt>pos</tt></em>, of theFileHandle.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>open</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">fh</em>->open <em class="replaceable">filename</em> [<em class="replaceable">openmode</em>]</pre><p>Takes <em class="replaceable">filename</em> and, optionally, the openmode, and opens a file. If the mode is present, it can be in Perlform (e.g., <tt class="literal">></tt>, <tt class="literal">+<</tt>) or inPOSIX form (e.g., <tt class="literal">w</tt>, <tt class="literal">r+</tt>).</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>setpos</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">fh</em>->setpos <em class="replaceable">pos</em></pre><p>Uses the value (<em class="replaceable">pos</em>) returned by<tt class="literal">getpos</tt> to restore a previous position of theFileHandle.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>setvbuf</b></font></td><td align="right"><i></i></td></tr></table><hr width="515" size="3" noshade="true" align="left" color="black" /><pre>$<em class="replaceable">fh</em>->setvbuf(<em class="replaceable">params</em>)</pre><p>Takes the same parameters as the C function<tt class="literal">setvbuf(3)</tt> and uses the C function to set thebuffering policy for the FileHandle.</p><p>The following additional FileHandle methods act as front-ends for thecorresponding built-in Perl functions (see theO'Reilly book <em class="emphasis">Programming Perl</em>or the <em class="emphasis">perlfunc</em> manpage for more details):</p><a name="ch08-286-fm2xml" /><table border="1" cellpadding="3"><tr><td> <p><tt class="literal">clearerr</tt></p></td><td> <p><tt class="literal">getc</tt></p></td></tr><tr><td> <p><tt class="literal">close</tt></p></td><td> <p><tt class="literal">gets</tt></p></td></tr><tr><td> <p><tt class="literal">eof</tt></p></td><td> <p><tt class="literal">seek</tt></p></td></tr><tr><td> <p><tt class="literal">fileno</tt></p></td><td><p><tt class="literal">tell</tt></p></td></tr></table><p><p>The next group of FileHandle methods correspond to Perl specialvariables (see <em class="emphasis">Programming Perl</em> or the<em class="emphasis">perlvar</em> manpage):</p><a name="ch08-287-fm2xml" /><table border="1" cellpadding="3"><tr><td> <p><tt class="literal">autoflush</tt></p></td><td> <p><tt class="literal">format_page_number</tt></p></td></tr><tr><td> <p><tt class="literal">format_formfeed</tt></p></td><td> <p><tt class="literal">format_top_name</tt></p></td></tr><tr><td> <p><tt class="literal">format_line_break_characters</tt></p></td><td> <p><tt class="literal">input_line_number</tt></p></td></tr><tr><td> <p><tt class="literal">format_lines_left</tt></p></td><td> <p><tt class="literal">input_record_separator</tt></p></td></tr><tr><td> <p><tt class="literal">format_lines_per_page</tt></p></td><td> <p><tt class="literal">output_field_separator</tt></p></td></tr><tr><td> <p><tt class="literal">format_name</tt></p></td><td> <p><tt class="literal">output_record_separator</tt></p></td></tr></table><p><p>Finally, the following methods are useful:</p><dl><dt><b><tt class="literal">$</tt><em class="replaceable">fh</em><tt class="literal">->print</tt></b></dt><dd>See Perl's built-in <tt class="literal">print</tt>function.</p></dd><dt><b><tt class="literal">$</tt><em class="replaceable">fh</em><tt class="literal">->printf</tt></b></dt><dd>See Perl's built-in <tt class="literal">printf</tt>function.</p></dd><dt><b><tt class="literal">$</tt><em class="replaceable">fh</em><tt class="literal">->getline</tt></b></dt><dd>Works like Perl's<tt class="literal"><FILEHANDLE></tt> construct, except that it canbe safely called in an array context (but it still returns just oneline).</p></dd><dt><b><tt class="literal">$</tt><em class="replaceable">fh</em><tt class="literal">->getlines</tt></b></dt><dd>Works like Perl's<tt class="literal"><FILEHANDLE></tt> construct when called in anarray context to read all remaining lines in a file.</p></dd></dl></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_111.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_113.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.111. FileCache</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.113. Filter::Simple </td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><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 + -