📄 ch08_128.htm
字号:
<html><head><title>IO::Handle (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_127.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_129.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.128. IO::Handle</h2><p><a name="INDEX-1478" />The base class for all other IO handleclasses. Its main purpose is for the other IO classes to inherit fromit; programs should not create IO::Handle objects directly. Providesthe following methods.</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>new</pre><p>Constructor. Creates a new IO::Handle object.</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>new_from_fd (<em class="replaceable">fd</em>, <em class="replaceable">mode</em>)</pre><p>Constructor. Like <tt class="literal">new</tt>, creates an IO::Handleobject. It requires two parameters, which are passed to the method<tt class="literal">fdopen</tt>; if <tt class="literal">fdopen</tt> fails, theobject is destroyed. Otherwise, it is returned to the caller.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>clearerr</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>->clearerr</pre><p>Clears the given handle's error indicator.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>error</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>->error</pre><p>Returns true if the given handle has experienced any errors since itwas opened or since the last call to <tt class="literal">clearerr</tt>.</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">fd</em>, <em class="replaceable">mode</em>)</pre><p>Like an ordinary <tt class="literal">open</tt> except that the firstparameter is a filehandle name, an IO::Handle 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>flush</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>->flush</pre><p>Flushes the given handle's buffer.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>getline</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>->getline</pre><p>Works like<tt class="literal"><$</tt><em class="replaceable"><tt>fh</tt></em><tt class="literal">></tt>,described in the section on "I/OOperators" in the <em class="emphasis">perlop</em>manpage, but is more readable and can be safely called in an arraycontext, while still returning one line.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>getlines</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>->getlines</pre><p>Works like<tt class="literal"><$</tt><em class="replaceable"><tt>fh</tt></em><tt class="literal">></tt>when called in an array context to read all the remaining lines in afile, but is more readable. <tt class="literal">croak</tt> s if called inscalar context.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>opened</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>->opened</pre><p>Returns true if the object is currently a valid file descriptor.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>ungetc</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>->ungetc (<em class="replaceable">ord</em>)</pre><p>Pushes a character with the ordinal value<em class="replaceable"><tt>ord</tt></em> back onto the givenhandle's input stream.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>untaint</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>->untaint</pre><p>Special method for working under <em class="emphasis">-T</em> andsetuid/gid scripts. Marks the object as taint-clean, and as such,data read from it is also considered taint-clean.</p></div><div class="refentry"><table width="515" border="0" cellpadding="5"><tr><td align="left"><font size="+1"><b>write</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>->write (<em class="replaceable">buf</em>, <em class="replaceable">len</em>[, <em class="replaceable">offset</em>])</pre><p>Like <tt class="literal">write</tt> found in C; that is, the opposite of<tt class="literal">read</tt>. The wrapper for the Perl<tt class="literal">write</tt> function is <tt class="literal">format_write</tt>.</p><p>IO::Handle also provides the following methods, which are notdescribed in detail here because they are simply frontends for thecorresponding built-in functions. See <a href="ch05_01.htm">Chapter 5, "Function Reference"</a>for more information.</p><a name="ch08-336-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Method</p></th><th><p>Description</p></th></tr><tr><td><p><tt class="literal">close</tt></p></td><td><p>Closes file or pipe</p></td></tr><tr><td><p><tt class="literal">eof</tt></p></td><td><p>Returns <tt class="literal">1</tt> if next read will return end-of-file</p></td></tr><tr><td><p><tt class="literal">fileno</tt></p></td><td><p>Returns file descriptor for a filehandle</p></td></tr><tr><td><p><tt class="literal">getc</tt></p></td><td><p>Returns next character from input file</p></td></tr><tr><td><p><tt class="literal">print</tt></p></td><td><p>Prints a string or comma-separated list of strings</p></td></tr><tr><td><p><tt class="literal">printf</tt></p></td><td><p>Prints a formatted string</p></td></tr><tr><td><p><tt class="literal">read</tt></p></td><td><p>Reads data from a filehandle</p></td></tr><tr><td><p><tt class="literal">stat</tt></p></td><td><p>Returns an array of status information for a file</p></td></tr><tr><td><p><tt class="literal">sysread</tt></p></td><td><p>Reads data from a filehandle with system call<tt class="literal">read(2)</tt></p></td></tr><tr><td><p><tt class="literal">syswrite</tt></p></td><td><p>Writes data to a filehandle with system call<tt class="literal">write(2)</tt></p></td></tr><tr><td><p><tt class="literal">truncate</tt></p></td><td><p>Truncates a file to a specified length</p></td></tr></table><p><p>Finally, the following methods act on the equivalent Perl variables.See <a href="ch04_01.htm">Chapter 4, "The Perl Language"</a> for more information.</p><a name="ch08-337-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Method</p></th><th><p>Description</p></th></tr><tr><td><p><tt class="literal">autoflush</tt></p></td><td><p>If nonzero, forces a flush now and after each write or print (default<tt class="literal">0</tt>) </p></td></tr><tr><td><p><tt class="literal">format_page_number</tt></p></td><td><p>Current page number</p></td></tr><tr><td><p><tt class="literal">format_lines_per_page</tt></p></td><td><p>Current page length (default <tt class="literal">60</tt>)</p></td></tr><tr><td><p><tt class="literal">format_lines_left</tt></p></td><td><p>Number of lines left on page</p></td></tr><tr><td><p><tt class="literal">format_name</tt></p></td><td><p>Name of current report format</p></td></tr><tr><td><p><tt class="literal">format_top_name</tt></p></td><td><p>Name of current top-of-page format</p></td></tr><tr><td><p><tt class="literal">format_line_break_characters</tt></p></td><td><p>Current set of linebreak chars for a format</p></td></tr><tr><td><p><tt class="literal">format_formfeed</tt></p></td><td><p>Formfeed char used by formats (default <tt class="literal">\f</tt>)</p></td></tr><tr><td><p><tt class="literal">format_write</tt></p></td><td><p>Wrapper for <tt class="literal">write</tt> function</p></td></tr><tr><td><p><tt class="literal">input_record_separator</tt></p></td><td><p>Input record separator (default newline)</p></td></tr><tr><td><p><tt class="literal">input_line_number</tt></p></td><td><p>Current input line number for last filehandle accessed</p></td></tr><tr><td><p><tt class="literal">output_field_separator</tt></p></td><td><p>Output field separator for print</p></td></tr><tr><td><p><tt class="literal">output_record_separator</tt></p></td><td><p>Output record separator for print</p></td></tr></table></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_127.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_129.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.127. IO::File</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.129. IO::Pipe</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 + -