perlio.html

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

HTML
392
字号
i.e. each byte is passed as-is. The stream will still be
buffered.</p>
</dd>
<dd>
<p>In Perl 5.6 and some books the <a href="#item__3araw"><code>:raw</code></a> layer (previously sometimes also
referred to as a &quot;discipline&quot;) is documented as the inverse of the
<a href="#item__3acrlf"><code>:crlf</code></a> layer. That is no longer the case - other layers which would
alter binary nature of the stream are also disabled.  If you want UNIX
line endings on a platform that normally does CRLF translation, but still
want UTF-8 or encoding defaults the appropriate thing to do is to add
<a href="#item__3aperlio"><code>:perlio</code></a> to PERLIO environment variable.</p>
</dd>
<dd>
<p>The implementation of <a href="#item__3araw"><code>:raw</code></a> is as a pseudo-layer which when &quot;pushed&quot;
pops itself and then any layers which do not declare themselves as suitable
for binary data. (Undoing :utf8 and :crlf are implemented by clearing
flags rather than popping layers but that is an implementation detail.)</p>
</dd>
<dd>
<p>As a consequence of the fact that <a href="#item__3araw"><code>:raw</code></a> normally pops layers
it usually only makes sense to have it as the only or first element in
a layer specification.  When used as the first element it provides
a known base on which to build e.g.</p>
</dd>
<dd>
<pre>
    open($fh,&quot;:raw:utf8&quot;,...)</pre>
</dd>
<dd>
<p>will construct a &quot;binary&quot; stream, but then enable UTF-8 translation.</p>
</dd>
</li>
<dt><strong><a name="item__3apop">:pop</a></strong>

<dd>
<p>A pseudo layer that removes the top-most layer. Gives perl code
a way to manipulate the layer stack. Should be considered
as experimental. Note that <a href="#item__3apop"><code>:pop</code></a> only works on real layers
and will not undo the effects of pseudo layers like <a href="#item__3autf8"><code>:utf8</code></a>.
An example of a possible use might be:</p>
</dd>
<dd>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,...)</span>
    <span class="operator">...</span>
    <span class="keyword">binmode</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span><span class="string">":encoding(...)"</span><span class="operator">);</span>  <span class="comment"># next chunk is encoded</span>
    <span class="operator">...</span>
    <span class="keyword">binmode</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span><span class="string">":pop"</span><span class="operator">);</span>            <span class="comment"># back to un-encoded</span>
</pre>
</dd>
<dd>
<p>A more elegant (and safer) interface is needed.</p>
</dd>
</li>
<dt><strong><a name="item__3awin32">:win32</a></strong>

<dd>
<p>On Win32 platforms this <em>experimental</em> layer uses native &quot;handle&quot; IO
rather than unix-like numeric file descriptor layer. Known to be
buggy as of perl 5.8.2.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="custom_layers">Custom Layers</a></h2>
<p>It is possible to write custom layers in addition to the above builtin
ones, both in C/XS and Perl.  Two such layers (and one example written
in Perl using the latter) come with the Perl distribution.</p>
<dl>
<dt><strong><a name="item__3aencoding">:encoding</a></strong>

<dd>
<p>Use <code>:encoding(ENCODING)</code> either in <a href="../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> or <a href="../lib/Pod/perlfunc.html#item_binmode"><code>binmode()</code></a> to install
a layer that does transparently character set and encoding transformations,
for example from Shift-JIS to Unicode.  Note that under <code>stdio</code>
an <a href="#item__3aencoding"><code>:encoding</code></a> also enables <a href="#item__3autf8"><code>:utf8</code></a>.  See <a href="../lib/PerlIO/encoding.html">the PerlIO::encoding manpage</a>
for more information.</p>
</dd>
</li>
<dt><strong><a name="item__3avia">:via</a></strong>

<dd>
<p>Use <code>:via(MODULE)</code> either in <a href="../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> or <a href="../lib/Pod/perlfunc.html#item_binmode"><code>binmode()</code></a> to install a layer
that does whatever transformation (for example compression /
decompression, encryption / decryption) to the filehandle.
See <a href="../lib/PerlIO/via.html">the PerlIO::via manpage</a> for more information.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="alternatives_to_raw">Alternatives to raw</a></h2>
<p>To get a binary stream an alternate method is to use:</p>
<pre>
    <span class="keyword">open</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span><span class="string">"whatever"</span><span class="operator">)</span>
    <span class="keyword">binmode</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">);</span>
</pre>
<p>this has advantage of being backward compatible with how such things have
had to be coded on some platforms for years.</p>
<p>To get an un-buffered stream specify an unbuffered layer (e.g. <a href="#item__3aunix"><code>:unix</code></a>)
in the open call:</p>
<pre>
    open($fh,&quot;&lt;:unix&quot;,$path)</pre>
<p>
</p>
<h2><a name="defaults_and_how_to_override_them">Defaults and how to override them</a></h2>
<p>If the platform is MS-DOS like and normally does CRLF to &quot;\n&quot;
translation for text files then the default layers are :</p>
<pre>
  unix crlf</pre>
<p>(The low level &quot;unix&quot; layer may be replaced by a platform specific low
level layer.)</p>
<p>Otherwise if <code>Configure</code> found out how to do &quot;fast&quot; IO using system's
stdio, then the default layers are:</p>
<pre>
  unix stdio</pre>
<p>Otherwise the default layers are</p>
<pre>
  unix perlio</pre>
<p>These defaults may change once perlio has been better tested and tuned.</p>
<p>The default can be overridden by setting the environment variable
PERLIO to a space separated list of layers (<code>unix</code> or platform low
level layer is always pushed first).</p>
<p>This can be used to see the effect of/bugs in the various layers e.g.</p>
<pre>
  cd .../perl/t
  PERLIO=stdio  ./perl harness
  PERLIO=perlio ./perl harness</pre>
<p>For the various value of PERLIO see <a href="../lib/Pod/perlrun.html#perlio">PERLIO in the perlrun manpage</a>.</p>
<p>
</p>
<h2><a name="querying_the_layers_of_filehandles">Querying the layers of filehandles</a></h2>
<p>The following returns the <strong>names</strong> of the PerlIO layers on a filehandle.</p>
<pre>
   <span class="keyword">my</span> <span class="variable">@layers</span> <span class="operator">=</span> <span class="variable">PerlIO::get_layers</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">);</span> <span class="comment"># Or FH, *FH, "FH".</span>
</pre>
<p>The layers are returned in the order an <a href="../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> or <a href="../lib/Pod/perlfunc.html#item_binmode"><code>binmode()</code></a> call would
use them.  Note that the &quot;default stack&quot; depends on the operating
system and on the Perl version, and both the compile-time and
runtime configurations of Perl.</p>
<p>The following table summarizes the default layers on UNIX-like and
DOS-like platforms and depending on the setting of the <code>$ENV{PERLIO}</code>:</p>
<pre>
 PERLIO     UNIX-like                   DOS-like
 ------     ---------                   --------
 unset / &quot;&quot; unix perlio / stdio [1]     unix crlf
 stdio      unix perlio / stdio [1]     stdio
 perlio     unix perlio                 unix perlio
 mmap       unix mmap                   unix mmap</pre>
<pre>
 <span class="comment"># [1] "stdio" if Configure found out how to do "fast stdio" (depends</span>
 <span class="comment"># on the stdio implementation) and in Perl 5.8, otherwise "unix perlio"</span>
</pre>
<p>By default the layers from the input side of the filehandle is
returned, to get the output side use the optional <code>output</code> argument:</p>
<pre>
   <span class="keyword">my</span> <span class="variable">@layers</span> <span class="operator">=</span> <span class="variable">PerlIO::get_layers</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="string">output</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">);</span>
</pre>
<p>(Usually the layers are identical on either side of a filehandle but
for example with sockets there may be differences, or if you have
been using the <a href="../lib/Pod/perlfunc.html#item_open"><code>open</code></a> pragma.)</p>
<p>There is no set_layers(), nor does <code>get_layers()</code> return a tied array
mirroring the stack, or anything fancy like that.  This is not
accidental or unintentional.  The PerlIO layer stack is a bit more
complicated than just a stack (see for example the behaviour of <a href="#item__3araw"><code>:raw</code></a>).
You are supposed to use <a href="../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> and <a href="../lib/Pod/perlfunc.html#item_binmode"><code>binmode()</code></a> to manipulate the stack.</p>
<p><strong>Implementation details follow, please close your eyes.</strong></p>
<p>The arguments to layers are by default returned in parenthesis after
the name of the layer, and certain layers (like <code>utf8</code>) are not real
layers but instead flags on real layers: to get all of these returned
separately use the optional <code>details</code> argument:</p>
<pre>
   <span class="keyword">my</span> <span class="variable">@layer_and_args_and_flags</span> <span class="operator">=</span> <span class="variable">PerlIO::get_layers</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span> <span class="string">details</span> <span class="operator">=&gt;</span> <span class="number">1</span><span class="operator">);</span>
</pre>
<p>The result will be up to be three times the number of layers:
the first element will be a name, the second element the arguments
(unspecified arguments will be <a href="../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>), the third element the flags,
the fourth element a name again, and so forth.</p>
<p><strong>You may open your eyes now.</strong></p>
<p>
</p>
<hr />
<h1><a name="author">AUTHOR</a></h1>
<p>Nick Ing-Simmons &lt;<a href="mailto:nick@ing-simmons.net">nick@ing-simmons.net</a>&gt;</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../lib/Pod/perlfunc.html#binmode">binmode in the perlfunc manpage</a>, <a href="../lib/Pod/perlfunc.html#open">open in the perlfunc manpage</a>, <a href="../lib/Pod/perlunicode.html">the perlunicode manpage</a>, <a href="../lib/Pod/perliol.html">the perliol manpage</a>,
<a href="../lib/Encode.html">the Encode manpage</a></p>

</body>

</html>

⌨️ 快捷键说明

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