perlapio.html

来自「perl教程」· HTML 代码 · 共 663 行 · 第 1/3 页

HTML
663 行
字号
<p>These correspond functionally to <code>fread()</code> and <code>fwrite()</code> but the
arguments and return values are different.  The <a href="#item_perlio_read"><code>PerlIO_read()</code></a> and
<code>PerlIO_write()</code> signatures have been modeled on the more sane low level
<a href="../../lib/Pod/perlfunc.html#item_read"><code>read()</code></a> and <a href="../../lib/Pod/perlfunc.html#item_write"><code>write()</code></a> functions instead: The &quot;file&quot; argument is passed
first, there is only one &quot;count&quot;, and the return value can distinguish
between error and <code>EOF</code>.</p>
</dd>
<dd>
<p>Returns a byte count if successful (which may be zero or
positive), returns negative value and sets <code>errno</code> on error.
Depending on implementation <code>errno</code> may be <code>EINTR</code> if operation was
interrupted by a signal.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_close"><strong>PerlIO_close(f)</strong></a></strong>

<dd>
<p>Depending on implementation <code>errno</code> may be <code>EINTR</code> if operation was
interrupted by a signal.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_puts"><strong>PerlIO_puts(f,s)</strong>, <strong>PerlIO_putc(f,c)</strong></a></strong>

<dd>
<p>These correspond to <code>fputs()</code> and fputc().
Note that arguments have been revised to have &quot;file&quot; first.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_ungetc"><strong>PerlIO_ungetc(f,c)</strong></a></strong>

<dd>
<p>This corresponds to ungetc().  Note that arguments have been revised
to have &quot;file&quot; first.  Arranges that next read operation will return
the byte <strong>c</strong>.  Despite the implied &quot;character&quot; in the name only
values in the range 0..0xFF are defined. Returns the byte <strong>c</strong> on
success or -1 (<code>EOF</code>) on error.  The number of bytes that can be
&quot;pushed back&quot; may vary, only 1 character is certain, and then only if
it is the last character that was read from the handle.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_getc"><strong>PerlIO_getc(f)</strong></a></strong>

<dd>
<p>This corresponds to getc().
Despite the c in the name only byte range 0..0xFF is supported.
Returns the character read or -1 (<code>EOF</code>) on error.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_eof"><strong>PerlIO_eof(f)</strong></a></strong>

<dd>
<p>This corresponds to feof().  Returns a true/false indication of
whether the handle is at end of file.  For terminal devices this may
or may not be &quot;sticky&quot; depending on the implementation.  The flag is
cleared by PerlIO_seek(), or PerlIO_rewind().</p>
</dd>
</li>
<dt><strong><a name="item_perlio_error"><strong>PerlIO_error(f)</strong></a></strong>

<dd>
<p>This corresponds to ferror().  Returns a true/false indication of
whether there has been an IO error on the handle.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_fileno"><strong>PerlIO_fileno(f)</strong></a></strong>

<dd>
<p>This corresponds to fileno(), note that on some platforms, the meaning
of &quot;fileno&quot; may not match Unix. Returns -1 if the handle has no open
descriptor associated with it.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_clearerr"><strong>PerlIO_clearerr(f)</strong></a></strong>

<dd>
<p>This corresponds to clearerr(), i.e., clears 'error' and (usually)
'eof' flags for the &quot;stream&quot;. Does not return a value.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_flush"><strong>PerlIO_flush(f)</strong></a></strong>

<dd>
<p>This corresponds to fflush().  Sends any buffered write data to the
underlying file.  If called with <code>NULL</code> this may flush all open
streams (or core dump with some USE_STDIO implementations).  Calling
on a handle open for read only, or on which last operation was a read
of some kind may lead to undefined behaviour on some USE_STDIO
implementations.  The USE_PERLIO (layers) implementation tries to
behave better: it flushes all open streams when passed <code>NULL</code>, and
attempts to retain data on read streams either in the buffer or by
seeking the handle to the current logical position.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_seek"><strong>PerlIO_seek(f,offset,whence)</strong></a></strong>

<dd>
<p>This corresponds to fseek().  Sends buffered write data to the
underlying file, or discards any buffered read data, then positions
the file descriptor as specified by <strong>offset</strong> and <strong>whence</strong> (sic).
This is the correct thing to do when switching between read and write
on the same handle (see issues with <a href="#item_perlio_flush"><code>PerlIO_flush()</code></a> above).  Offset is
of type <code>Off_t</code> which is a perl Configure value which may not be same
as stdio's <code>off_t</code>.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_tell"><strong>PerlIO_tell(f)</strong></a></strong>

<dd>
<p>This corresponds to ftell().  Returns the current file position, or
(Off_t) -1 on error.  May just return value system &quot;knows&quot; without
making a system call or checking the underlying file descriptor (so
use on shared file descriptors is not safe without a
PerlIO_seek()). Return value is of type <code>Off_t</code> which is a perl
Configure value which may not be same as stdio's <code>off_t</code>.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_getpos"><strong>PerlIO_getpos(f,p)</strong>, <strong>PerlIO_setpos(f,p)</strong></a></strong>

<dd>
<p>These correspond (loosely) to <code>fgetpos()</code> and fsetpos(). Rather than
stdio's Fpos_t they expect a &quot;Perl Scalar Value&quot; to be passed. What is
stored there should be considered opaque. The layout of the data may
vary from handle to handle.  When not using stdio or if platform does
not have the stdio calls then they are implemented in terms of
<a href="#item_perlio_tell"><code>PerlIO_tell()</code></a> and PerlIO_seek().</p>
</dd>
</li>
<dt><strong><a name="item_perlio_rewind"><strong>PerlIO_rewind(f)</strong></a></strong>

<dd>
<p>This corresponds to rewind(). It is usually defined as being</p>
</dd>
<dd>
<pre>
    <span class="variable">PerlIO_seek</span><span class="operator">(</span><span class="variable">f</span><span class="operator">,(</span><span class="variable">Off_t</span><span class="operator">)</span><span class="number">0</span><span class="variable">L</span><span class="operator">,</span> <span class="variable">SEEK_SET</span><span class="operator">);</span>
    <span class="variable">PerlIO_clearerr</span><span class="operator">(</span><span class="variable">f</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item_perlio_tmpfile"><strong>PerlIO_tmpfile()</strong></a></strong>

<dd>
<p>This corresponds to tmpfile(), i.e., returns an anonymous PerlIO or
NULL on error.  The system will attempt to automatically delete the
file when closed.  On Unix the file is usually <a href="../../lib/Pod/perlfunc.html#item_unlink"><code>unlink</code></a>-ed just after
it is created so it does not matter how it gets closed. On other
systems the file may only be deleted if closed via <a href="#item_perlio_close"><code>PerlIO_close()</code></a>
and/or the program exits via <a href="../../lib/Pod/perlfunc.html#item_exit"><code>exit</code></a>.  Depending on the implementation
there may be &quot;race conditions&quot; which allow other processes access to
the file, though in general it will be safer in this regard than
ad. hoc. schemes.</p>
</dd>
</li>
<dt><strong><a name="item_perlio_setlinebuf"><strong>PerlIO_setlinebuf(f)</strong></a></strong>

<dd>
<p>This corresponds to setlinebuf().  Does not return a value. What
constitutes a &quot;line&quot; is implementation dependent but usually means
that writing &quot;\n&quot; flushes the buffer.  What happens with things like
&quot;this\nthat&quot; is uncertain.  (Perl core uses it <em>only</em> when &quot;dumping&quot;;
it has nothing to do with $| auto-flush.)</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="coexistence_with_stdio">Co-existence with stdio</a></h2>
<p>There is outline support for co-existence of PerlIO with stdio.
Obviously if PerlIO is implemented in terms of stdio there is no
problem. However in other cases then mechanisms must exist to create a
FILE * which can be passed to library code which is going to use stdio
calls.</p>
<p>The first step is to add this line:</p>
<pre>
   <span class="comment">#define PERLIO_NOT_STDIO 0</span>
</pre>
<p><em>before</em> including any perl header files. (This will probably become
the default at some point).  That prevents &quot;perlio.h&quot; from attempting
to #define stdio functions onto PerlIO functions.</p>
<p>XS code is probably better using &quot;typemap&quot; if it expects FILE *
arguments.  The standard typemap will be adjusted to comprehend any
changes in this area.</p>
<dl>
<dt><strong><a name="item_perlio_importfile"><strong>PerlIO_importFILE(f,mode)</strong></a></strong>

<dd>
<p>Used to get a PerlIO * from a FILE *.</p>
</dd>
<dd>
<p>The mode argument should be a string as would be passed to
fopen/PerlIO_open.  If it is NULL then - for legacy support - the code
will (depending upon the platform and the implementation) either
attempt to empirically determine the mode in which <em>f</em> is open, or
use &quot;r+&quot; to indicate a read/write stream.</p>
</dd>
<dd>
<p>Once called the FILE * should <em>ONLY</em> be closed by calling
<a href="#item_perlio_close"><code>PerlIO_close()</code></a> on the returned PerlIO *.</p>
</dd>
<dd>
<p>The PerlIO is set to textmode. Use PerlIO_binmode if this is
not the desired mode.</p>
</dd>
<dd>
<p>This is <strong>not</strong> the reverse of PerlIO_exportFILE().</p>
</dd>
</li>
<dt><strong><a name="item_perlio_exportfile"><strong>PerlIO_exportFILE(f,mode)</strong></a></strong>

<dd>
<p>Given a PerlIO * create a 'native' FILE * suitable for passing to code
expecting to be compiled and linked with ANSI C <em>stdio.h</em>.  The mode
argument should be a string as would be passed to fopen/PerlIO_open.
If it is NULL then - for legacy support - the FILE * is opened in same
mode as the PerlIO *.</p>
</dd>
<dd>
<p>The fact that such a FILE * has been 'exported' is recorded, (normally
by pushing a new :stdio &quot;layer&quot; onto the PerlIO *), which may affect
future PerlIO operations on the original PerlIO *.  You should not
call <code>fclose()</code> on the file unless you call <a href="#item_perlio_releasefile"><code>PerlIO_releaseFILE()</code></a>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?