perlio.html

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

HTML
392
字号
<?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>PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space</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>PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space</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>
	<ul>

		<li><a href="#custom_layers">Custom Layers</a></li>
		<li><a href="#alternatives_to_raw">Alternatives to raw</a></li>
		<li><a href="#defaults_and_how_to_override_them">Defaults and how to override them</a></li>
		<li><a href="#querying_the_layers_of_filehandles">Querying the layers of filehandles</a></li>
	</ul>

	<li><a href="#author">AUTHOR</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>PerlIO - On demand loader for PerlIO layers and root of PerlIO::* name space</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
  <span class="keyword">open</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span><span class="string">"&lt;:crlf"</span><span class="operator">,</span> <span class="string">"my.txt"</span><span class="operator">);</span> <span class="comment"># support platform-native and CRLF text files</span>
</pre>
<pre>
  <span class="keyword">open</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">,</span><span class="string">"&lt;"</span><span class="operator">,</span><span class="string">"his.jpg"</span><span class="operator">);</span>      <span class="comment"># portably open a binary file for reading</span>
  <span class="keyword">binmode</span><span class="operator">(</span><span class="variable">$fh</span><span class="operator">);</span>
</pre>
<pre>
  Shell:
    PERLIO=perlio perl ....</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>When an undefined layer 'foo' is encountered in 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> layer specification then C code performs the equivalent of:</p>
<pre>
  <span class="keyword">use</span> <span class="variable">PerlIO</span> <span class="string">'foo'</span><span class="operator">;</span>
</pre>
<p>The perl code in PerlIO.pm then attempts to locate a layer by doing</p>
<pre>
  <span class="keyword">require</span> <span class="variable">PerlIO::foo</span><span class="operator">;</span>
</pre>
<p>Otherwise the <code>PerlIO</code> package is a place holder for additional
PerlIO related functions.</p>
<p>The following layers are currently defined:</p>
<dl>
<dt><strong><a name="item__3aunix">:unix</a></strong>

<dd>
<p>Lowest level layer which provides basic PerlIO operations in terms of
UNIX/POSIX numeric file descriptor calls
(open(), read(), write(), lseek(), close()).</p>
</dd>
</li>
<dt><strong><a name="item__3astdio">:stdio</a></strong>

<dd>
<p>Layer which calls <code>fread</code>, <code>fwrite</code> and <code>fseek</code>/<code>ftell</code> etc.  Note
that as this is &quot;real&quot; stdio it will ignore any layers beneath it and
got straight to the operating system via the C library as usual.</p>
</dd>
</li>
<dt><strong><a name="item__3aperlio">:perlio</a></strong>

<dd>
<p>A from scratch implementation of buffering for PerlIO. Provides fast
access to the buffer for <code>sv_gets</code> which implements perl's readline/&lt;&gt;
and in general attempts to minimize data copying.</p>
</dd>
<dd>
<p><a href="#item__3aperlio"><code>:perlio</code></a> will insert a <a href="#item__3aunix"><code>:unix</code></a> layer below itself to do low level IO.</p>
</dd>
</li>
<dt><strong><a name="item__3acrlf">:crlf</a></strong>

<dd>
<p>A layer that implements DOS/Windows like CRLF line endings.  On read
converts pairs of CR,LF to a single &quot;\n&quot; newline character.  On write
converts each &quot;\n&quot; to a CR,LF pair.  Note that this layer likes to be
one of its kind: it silently ignores attempts to be pushed into the
layer stack more than once.</p>
</dd>
<dd>
<p>It currently does <em>not</em> mimic MS-DOS as far as treating of Control-Z
as being an end-of-file marker.</p>
</dd>
<dd>
<p>(Gory details follow) To be more exact what happens is this: after
pushing itself to the stack, the <a href="#item__3acrlf"><code>:crlf</code></a> layer checks all the layers
below itself to find the first layer that is capable of being a CRLF
layer but is not yet enabled to be a CRLF layer.  If it finds such a
layer, it enables the CRLFness of that other deeper layer, and then
pops itself off the stack.  If not, fine, use the one we just pushed.</p>
</dd>
<dd>
<p>The end result is that a <a href="#item__3acrlf"><code>:crlf</code></a> means &quot;please enable the first CRLF
layer you can find, and if you can't find one, here would be a good
spot to place a new one.&quot;</p>
</dd>
<dd>
<p>Based on the <a href="#item__3aperlio"><code>:perlio</code></a> layer.</p>
</dd>
</li>
<dt><strong><a name="item__3ammap">:mmap</a></strong>

<dd>
<p>A layer which implements &quot;reading&quot; of files by using <code>mmap()</code> to
make (whole) file appear in the process's address space, and then
using that as PerlIO's &quot;buffer&quot;. This <em>may</em> be faster in certain
circumstances for large files, and may result in less physical memory
use when multiple processes are reading the same file.</p>
</dd>
<dd>
<p>Files which are not <code>mmap()</code>-able revert to behaving like the <a href="#item__3aperlio"><code>:perlio</code></a>
layer. Writes also behave like <a href="#item__3aperlio"><code>:perlio</code></a> layer as <code>mmap()</code> for write
needs extra house-keeping (to extend the file) which negates any advantage.</p>
</dd>
<dd>
<p>The <a href="#item__3ammap"><code>:mmap</code></a> layer will not exist if platform does not support <code>mmap()</code>.</p>
</dd>
</li>
<dt><strong><a name="item__3autf8">:utf8</a></strong>

<dd>
<p>Declares that the stream accepts perl's internal encoding of
characters.  (Which really is UTF-8 on ASCII machines, but is
UTF-EBCDIC on EBCDIC machines.)  This allows any character perl can
represent to be read from or written to the stream. The UTF-X encoding
is chosen to render simple text parts (i.e.  non-accented letters,
digits and common punctuation) human readable in the encoded file.</p>
</dd>
<dd>
<p>Here is how to write your native data out using UTF-8 (or UTF-EBCDIC)
and then read it back in.</p>
</dd>
<dd>
<pre>
        <span class="keyword">open</span><span class="operator">(</span><span class="variable">F</span><span class="operator">,</span> <span class="string">"&gt;:utf8"</span><span class="operator">,</span> <span class="string">"data.utf"</span><span class="operator">);</span>
        <span class="keyword">print</span> <span class="variable">F</span> <span class="variable">$out</span><span class="operator">;</span>
        <span class="keyword">close</span><span class="operator">(</span><span class="variable">F</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<pre>
        <span class="keyword">open</span><span class="operator">(</span><span class="variable">F</span><span class="operator">,</span> <span class="string">"&lt;:utf8"</span><span class="operator">,</span> <span class="string">"data.utf"</span><span class="operator">);</span>
        <span class="variable">$in</span> <span class="operator">=</span> <span class="operator">&lt;</span><span class="variable">F</span><span class="operator">&gt;;</span>
        <span class="keyword">close</span><span class="operator">(</span><span class="variable">F</span><span class="operator">);</span>
</pre>
</dd>
</li>
<dt><strong><a name="item__3abytes">:bytes</a></strong>

<dd>
<p>This is the inverse of <a href="#item__3autf8"><code>:utf8</code></a> layer. It turns off the flag
on the layer below so that data read from it is considered to
be &quot;octets&quot; i.e. characters in range 0..255 only. Likewise
on output perl will warn if a &quot;wide&quot; character is written
to a such a stream.</p>
</dd>
</li>
<dt><strong><a name="item__3araw">:raw</a></strong>

<dd>
<p>The <a href="#item__3araw"><code>:raw</code></a> layer is <em>defined</em> as being identical to calling
<a href="../lib/Pod/perlfunc.html#item_binmode"><code>binmode($fh)</code></a> - the stream is made suitable for passing binary data

⌨️ 快捷键说明

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