handle.html
来自「perl教程」· HTML 代码 · 共 327 行 · 第 1/2 页
HTML
327 行
<dt><strong><a name="item_getline">$io->getline</a></strong>
<dd>
<p>This works like <$io> described in <a href="../../lib/Pod/perlop.html#i_o_operators">I/O Operators in the perlop manpage</a>
except that it's more readable and can be safely called in a
list context but still returns just one line. If used as the conditional
+within a <code>while</code> or C-style <code>for</code> loop, however, you will need to
+emulate the functionality of <$io> with <a href="../../lib/Pod/perlfunc.html#item_defined"><code>defined($_ = $io->getline)</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_getlines">$io->getlines</a></strong>
<dd>
<p>This works like <$io> when called in a list context to read all
the remaining lines in a file, except that it's more readable.
It will also <code>croak()</code> if accidentally called in a scalar context.</p>
</dd>
</li>
<dt><strong><a name="item_ungetc">$io->ungetc ( ORD )</a></strong>
<dd>
<p>Pushes a character with the given ordinal value back onto the given
handle's input stream. Only one character of pushback per handle is
guaranteed.</p>
</dd>
</li>
<dt><strong><a name="item_write">$io->write ( BUF, LEN [, OFFSET ] )</a></strong>
<dd>
<p>This <a href="#item_write"><code>write</code></a> is like <a href="#item_write"><code>write</code></a> found in C, that is it is the
opposite of read. The wrapper for the perl <a href="#item_write"><code>write</code></a> function is
called <code>format_write</code>.</p>
</dd>
</li>
<dt><strong><a name="item_error">$io->error</a></strong>
<dd>
<p>Returns a true value if the given handle has experienced any errors
since it was opened or since the last call to <a href="#item_clearerr"><code>clearerr</code></a>, or if the
handle is invalid. It only returns false for a valid handle with no
outstanding errors.</p>
</dd>
</li>
<dt><strong><a name="item_clearerr">$io->clearerr</a></strong>
<dd>
<p>Clear the given handle's error indicator. Returns -1 if the handle is
invalid, 0 otherwise.</p>
</dd>
</li>
<dt><strong><a name="item_sync">$io->sync</a></strong>
<dd>
<p><a href="#item_sync"><code>sync</code></a> synchronizes a file's in-memory state with that on the
physical medium. <a href="#item_sync"><code>sync</code></a> does not operate at the perlio api level, but
operates on the file descriptor (similar to sysread, sysseek and
systell). This means that any data held at the perlio api level will not
be synchronized. To synchronize data that is buffered at the perlio api
level you must use the flush method. <a href="#item_sync"><code>sync</code></a> is not implemented on all
platforms. Returns "0 but true" on success, <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> on error, <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>
for an invalid handle. See <em>fsync(3c)</em>.</p>
</dd>
</li>
<dt><strong><a name="item_flush">$io->flush</a></strong>
<dd>
<p><a href="#item_flush"><code>flush</code></a> causes perl to flush any buffered data at the perlio api level.
Any unread data in the buffer will be discarded, and any unwritten data
will be written to the underlying file descriptor. Returns "0 but true"
on success, <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> on error.</p>
</dd>
</li>
<dt><strong><a name="item_printflush">$io->printflush ( ARGS )</a></strong>
<dd>
<p>Turns on autoflush, print ARGS and then restores the autoflush status of the
<code>IO::Handle</code> object. Returns the return value from print.</p>
</dd>
</li>
<dt><strong><a name="item_blocking">$io->blocking ( [ BOOL ] )</a></strong>
<dd>
<p>If called with an argument <a href="#item_blocking"><code>blocking</code></a> will turn on non-blocking IO if
<code>BOOL</code> is false, and turn it off if <code>BOOL</code> is true.</p>
</dd>
<dd>
<p><a href="#item_blocking"><code>blocking</code></a> will return the value of the previous setting, or the
current setting if <code>BOOL</code> is not given.</p>
</dd>
<dd>
<p>If an error occurs <a href="#item_blocking"><code>blocking</code></a> will return undef and <a href="../../lib/Pod/perlvar.html#item___"><code>$!</code></a> will be set.</p>
</dd>
</li>
</dl>
<p>If the C functions <code>setbuf()</code> and/or <code>setvbuf()</code> are available, then
<code>IO::Handle::setbuf</code> and <code>IO::Handle::setvbuf</code> set the buffering
policy for an IO::Handle. The calling sequences for the Perl functions
are the same as their C counterparts--including the constants <code>_IOFBF</code>,
<code>_IOLBF</code>, and <code>_IONBF</code> for setvbuf()--except that the buffer parameter
specifies a scalar variable to use as a buffer. You should only
change the buffer before any I/O, or immediately after calling flush.</p>
<p>WARNING: The IO::Handle::setvbuf() is not available by default on
Perls 5.8.0 and later because <code>setvbuf()</code> is rather specific to using
the stdio library, while Perl prefers the new perlio subsystem instead.</p>
<p>WARNING: A variable used as a buffer by <code>setbuf</code> or <code>setvbuf</code> <strong>must not
be modified</strong> in any way until the IO::Handle is closed or <code>setbuf</code> or
<code>setvbuf</code> is called again, or memory corruption may result! Remember that
the order of global destruction is undefined, so even if your buffer
variable remains in scope until program termination, it may be undefined
before the file IO::Handle is closed. Note that you need to import the
constants <code>_IOFBF</code>, <code>_IOLBF</code>, and <code>_IONBF</code> explicitly. Like C, setbuf
returns nothing. setvbuf returns "0 but true", on success, <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> on
failure.</p>
<p>Lastly, there is a special method for working under <strong>-T</strong> and setuid/gid
scripts:</p>
<dl>
<dt><strong><a name="item_untaint">$io->untaint</a></strong>
<dd>
<p>Marks the object as taint-clean, and as such data read from it will also
be considered taint-clean. Note that this is a very trusting action to
take, and appropriate consideration for the data source and potential
vulnerability should be kept in mind. Returns 0 on success, -1 if setting
the taint-clean flag failed. (eg invalid handle)</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="note">NOTE</a></h1>
<p>An <code>IO::Handle</code> object is a reference to a symbol/GLOB reference (see
the <code>Symbol</code> package). Some modules that
inherit from <code>IO::Handle</code> may want to keep object related variables
in the hash table part of the GLOB. In an attempt to prevent modules
trampling on each other I propose the that any such module should prefix
its variables with its own name separated by _'s. For example the IO::Socket
module keeps a <code>timeout</code> variable in 'io_socket_timeout'.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Pod/perlfunc.html">the perlfunc manpage</a>,
<a href="../../lib/Pod/perlop.html#i_o_operators">I/O Operators in the perlop manpage</a>,
<a href="../../lib/IO/File.html">the IO::File manpage</a></p>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>Due to backwards compatibility, all filehandles resemble objects
of class <code>IO::Handle</code>, or actually classes derived from that class.
They actually aren't. Which means you can't derive your own
class from <code>IO::Handle</code> and inherit those methods.</p>
<p>
</p>
<hr />
<h1><a name="history">HISTORY</a></h1>
<p>Derived from FileHandle.pm by Graham Barr <<em><a href="mailto:gbarr@pobox.com">gbarr@pobox.com</a></em>></p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?