📄 filehandle.html
字号:
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../displayToc.js"></script>
<script language="JavaScript" src="../tocParas.js"></script>
<script language="JavaScript" src="../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../scineplex.css">
<title>FileHandle - supply object methods for filehandles</title>
<link rel="stylesheet" href="../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',1);</script>
<h1><a>FileHandle - supply object methods for filehandles</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>FileHandle - supply object methods for filehandles</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="keyword">use</span> <span class="variable">FileHandle</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">FileHandle</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="variable">$fh</span><span class="operator">-></span><span class="keyword">open</span><span class="operator">(</span><span class="string">"< file"</span><span class="operator">))</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="operator"><</span><span class="variable">$fh</span><span class="operator">>;</span>
<span class="variable">$fh</span><span class="operator">-></span><span class="keyword">close</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">FileHandle</span> <span class="string">"> FOO"</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="keyword">defined</span> <span class="variable">$fh</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="variable">$fh</span> <span class="string">"bar\n"</span><span class="operator">;</span>
<span class="variable">$fh</span><span class="operator">-></span><span class="keyword">close</span><span class="operator">;</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">FileHandle</span> <span class="string">"file"</span><span class="operator">,</span> <span class="string">"r"</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="keyword">defined</span> <span class="variable">$fh</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="operator"><</span><span class="variable">$fh</span><span class="operator">>;</span>
<span class="keyword">undef</span> <span class="variable">$fh</span><span class="operator">;</span> <span class="comment"># automatically closes the file</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">$fh</span> <span class="operator">=</span> <span class="variable">new</span> <span class="variable">FileHandle</span> <span class="string">"file"</span><span class="operator">,</span> <span class="variable">O_WRONLY</span><span class="operator">|</span><span class="variable">O_APPEND</span><span class="operator">;</span>
<span class="keyword">if</span> <span class="operator">(</span><span class="keyword">defined</span> <span class="variable">$fh</span><span class="operator">)</span> <span class="operator">{</span>
<span class="keyword">print</span> <span class="variable">$fh</span> <span class="string">"corge\n"</span><span class="operator">;</span>
<span class="keyword">undef</span> <span class="variable">$fh</span><span class="operator">;</span> <span class="comment"># automatically closes the file</span>
<span class="operator">}</span>
</pre>
<pre>
<span class="variable">$pos</span> <span class="operator">=</span> <span class="variable">$fh</span><span class="operator">-></span><span class="variable">getpos</span><span class="operator">;</span>
<span class="variable">$fh</span><span class="operator">-></span><span class="variable">setpos</span><span class="operator">(</span><span class="variable">$pos</span><span class="operator">);</span>
</pre>
<pre>
<span class="variable">$fh</span><span class="operator">-></span><span class="variable">setvbuf</span><span class="operator">(</span><span class="variable">$buffer_var</span><span class="operator">,</span> <span class="variable">_IOLBF</span><span class="operator">,</span> <span class="number">1024</span><span class="operator">);</span>
</pre>
<pre>
<span class="operator">(</span><span class="variable">$readfh</span><span class="operator">,</span> <span class="variable">$writefh</span><span class="operator">)</span> <span class="operator">=</span> <span class="variable">FileHandle::pipe</span><span class="operator">;</span>
</pre>
<pre>
<span class="variable">autoflush</span> <span class="variable">STDOUT</span> <span class="number">1</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>NOTE: This class is now a front-end to the IO::* classes.</p>
<p><code>FileHandle::new</code> creates a <code>FileHandle</code>, which is a reference to a
newly created symbol (see the <code>Symbol</code> package). If it receives any
parameters, they are passed to <code>FileHandle::open</code>; if the open fails,
the <code>FileHandle</code> object is destroyed. Otherwise, it is returned to
the caller.</p>
<p><code>FileHandle::new_from_fd</code> creates a <code>FileHandle</code> like <code>new</code> does.
It requires two parameters, which are passed to <code>FileHandle::fdopen</code>;
if the fdopen fails, the <code>FileHandle</code> object is destroyed.
Otherwise, it is returned to the caller.</p>
<p><code>FileHandle::open</code> accepts one parameter or two. With one parameter,
it is just a front end for the built-in <a href="../lib/Pod/perlfunc.html#item_open"><code>open</code></a> function. With two
parameters, the first parameter is a filename that may include
whitespace or other special characters, and the second parameter is
the open mode, optionally followed by a file permission value.</p>
<p>If <code>FileHandle::open</code> receives a Perl mode string (">", "+<", etc.)
or a POSIX <code>fopen()</code> mode string ("w", "r+", etc.), it uses the basic
Perl <a href="../lib/Pod/perlfunc.html#item_open"><code>open</code></a> operator.</p>
<p>If <code>FileHandle::open</code> is given a numeric mode, it passes that mode
and the optional permissions value to the Perl <a href="../lib/Pod/perlfunc.html#item_sysopen"><code>sysopen</code></a> operator.
For convenience, <code>FileHandle::import</code> tries to import the O_XXX
constants from the Fcntl module. If dynamic loading is not available,
this may fail, but the rest of FileHandle will still work.</p>
<p><code>FileHandle::fdopen</code> is like <a href="../lib/Pod/perlfunc.html#item_open"><code>open</code></a> except that its first parameter
is not a filename but rather a file handle name, a FileHandle object,
or a file descriptor number.</p>
<p>If the C functions <code>fgetpos()</code> and <code>fsetpos()</code> are available, then
<code>FileHandle::getpos</code> returns an opaque value that represents the
current position of the FileHandle, and <code>FileHandle::setpos</code> uses
that value to return to a previously visited position.</p>
<p>If the C function <code>setvbuf()</code> is available, then <code>FileHandle::setvbuf</code>
sets the buffering policy for the FileHandle. The calling sequence
for the Perl function is the same as its C counterpart, including the
macros <code>_IOFBF</code>, <code>_IOLBF</code>, and <code>_IONBF</code>, except that the buffer
parameter specifies a scalar variable to use as a buffer. WARNING: A
variable used as a buffer by <code>FileHandle::setvbuf</code> must not be
modified in any way until the FileHandle is closed or until
<code>FileHandle::setvbuf</code> is called again, or memory corruption may
result!</p>
<p>See <a href="../lib/Pod/perlfunc.html">the perlfunc manpage</a> for complete descriptions of each of the following
supported <code>FileHandle</code> methods, which are just front ends for the
corresponding built-in functions:</p>
<pre>
close
fileno
getc
gets
eof
clearerr
seek
tell</pre>
<p>See <a href="../lib/Pod/perlvar.html">the perlvar manpage</a> for complete descriptions of each of the following
supported <code>FileHandle</code> methods:</p>
<pre>
autoflush
output_field_separator
output_record_separator
input_record_separator
input_line_number
format_page_number
format_lines_per_page
format_lines_left
format_name
format_top_name
format_line_break_characters
format_formfeed</pre>
<p>Furthermore, for doing normal I/O you might need these:</p>
<dl>
<dt><strong><a name="item_print">$fh->print</a></strong>
<dd>
<p>See <a href="../lib/Pod/perlfunc.html#print">print in the perlfunc manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_printf">$fh->printf</a></strong>
<dd>
<p>See <a href="../lib/Pod/perlfunc.html#printf">printf in the perlfunc manpage</a>.</p>
</dd>
</li>
<dt><strong><a name="item_getline">$fh->getline</a></strong>
<dd>
<p>This works like <$fh> 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.</p>
</dd>
</li>
<dt><strong><a name="item_getlines">$fh->getlines</a></strong>
<dd>
<p>This works like <$fh> 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>
</dl>
<p>There are many other functions available since FileHandle is descended
from IO::File, IO::Seekable, and IO::Handle. Please see those
respective pages for documentation on more functions.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>The <strong>IO</strong> extension,
<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>.</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -