encoding.html

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

HTML
572
字号
of these are done by Inaba Hiroto.  Any other features and changes
are good for 5.8.0.</p>
<dl>
<dt><strong><a name="item__22non_2deuc_22_doublebyte_encodings">&quot;NON-EUC&quot; doublebyte encodings</a></strong>

<dd>
<p>Because perl needs to parse script before applying this pragma, such
encodings as Shift_JIS and Big-5 that may contain '\' (BACKSLASH;
\x5c) in the second byte fails because the second byte may
accidentally escape the quoting character that follows.  Perl 5.8.1
or later fixes this problem.</p>
</dd>
</li>
<dt><strong><a name="item_tr_">tr//</a></strong>

<dd>
<p><a href="#item_tr_"><code>tr//</code></a> was overlooked by Perl 5 porters when they released perl 5.8.0
See the section below for details.</p>
</dd>
</li>
<dt><strong><a name="item_data_pseudo_2dfilehandle">DATA pseudo-filehandle</a></strong>

<dd>
<p>Another feature that was overlooked was <code>DATA</code>.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="usage">USAGE</a></h1>
<dl>
<dt><strong><a name="item_use_encoding__5bencname_5d__3b">use encoding [<em>ENCNAME</em>] ;</a></strong>

<dd>
<p>Sets the script encoding to <em>ENCNAME</em>.  And unless ${^UNICODE} 
exists and non-zero, PerlIO layers of STDIN and STDOUT are set to
&quot;:encoding(<em>ENCNAME</em>)&quot;.</p>
</dd>
<dd>
<p>Note that STDERR WILL NOT be changed.</p>
</dd>
<dd>
<p>Also note that non-STD file handles remain unaffected.  Use <code>use
open</code> or <a href="../lib/Pod/perlfunc.html#item_binmode"><code>binmode</code></a> to change layers of those.</p>
</dd>
<dd>
<p>If no encoding is specified, the environment variable <a href="../lib/Pod/perlrun.html#item_perl_encoding">PERL_ENCODING</a>
is consulted.  If no encoding can be found, the error <code>Unknown encoding
'ENCNAME'</code> will be thrown.</p>
</dd>
</li>
<dt><strong><a name="item_use_encoding_encname__5b_stdin__3d_3e_encname_in__">use encoding <em>ENCNAME</em> [ STDIN =&gt; <em>ENCNAME_IN</em> ...] ;</a></strong>

<dd>
<p>You can also individually set encodings of STDIN and STDOUT via the
<code>STDIN =&gt; ENCNAME</code> form.  In this case, you cannot omit the
first <em>ENCNAME</em>.  <code>STDIN =&gt; undef</code> turns the IO transcoding
completely off.</p>
</dd>
<dd>
<p>When ${^UNICODE} exists and non-zero, these options will completely
ignored.  ${^UNICODE} is a variable introduced in perl 5.8.1.  See
<a href="../lib/Pod/perlrun.html">the perlrun manpage</a> see <a href="../lib/Pod/perlvar.html#___unicode_">${^UNICODE} in the perlvar manpage</a> and <a href="../lib/Pod/perlrun.html#c">-C in the perlrun manpage</a> for
details (perl 5.8.1 and later).</p>
</dd>
</li>
<dt><strong><a name="item_use_encoding_encname_filter_3d_3e1_3b">use encoding <em>ENCNAME</em> Filter=&gt;1;</a></strong>

<dd>
<p>This turns the encoding pragma into a source filter.  While the
default approach just decodes interpolated literals (in <code>qq()</code> and
qr()), this will apply a source filter to the entire source code.  See
<a href="#the_filter_option">The Filter Option</a> below for details.</p>
</dd>
</li>
<dt><strong><a name="item_no_encoding_3b">no encoding;</a></strong>

<dd>
<p>Unsets the script encoding. The layers of STDIN, STDOUT are
reset to &quot;:raw&quot; (the default unprocessed raw stream of bytes).</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="the_filter_option">The Filter Option</a></h1>
<p>The magic of <code>use encoding</code> is not applied to the names of
identifiers.  In order to make <code>${&quot;\x{4eba}&quot;}++</code> ($human++, where human
is a single Han ideograph) work, you still need to write your script
in UTF-8 -- or use a source filter.  That's what 'Filter=&gt;1' does.</p>
<p>What does this mean?  Your source code behaves as if it is written in
UTF-8 with 'use utf8' in effect.  So even if your editor only supports
Shift_JIS, for example, you can still try examples in Chapter 15 of
<code>Programming Perl, 3rd Ed.</code>.  For instance, you can use UTF-8
identifiers.</p>
<p>This option is significantly slower and (as of this writing) non-ASCII
identifiers are not very stable WITHOUT this option and with the
source code written in UTF-8.</p>
<p>
</p>
<h2><a name="filterrelated_changes_at_encode_version_1_87">Filter-related changes at Encode version 1.87</a></h2>
<ul>
<li>
<p>The Filter option now sets STDIN and STDOUT like non-filter options.
And <code>STDIN=&gt;ENCODING</code> and <code>STDOUT=&gt;ENCODING</code> work like
non-filter version.</p>
</li>
<li>
<p><code>use utf8</code> is implicitly declared so you no longer have to <code>use
utf8</code> to <code>${&quot;\x{4eba}&quot;}++</code>.</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="caveats">CAVEATS</a></h1>
<p>
</p>
<h2><a name="not_scoped">NOT SCOPED</a></h2>
<p>The pragma is a per script, not a per block lexical.  Only the last
<code>use encoding</code> or <code>no encoding</code> matters, and it affects 
<strong>the whole script</strong>.  However, the &lt;no encoding&gt; pragma is supported and 
<strong>use encoding</strong> can appear as many times as you want in a given script. 
The multiple use of this pragma is discouraged.</p>
<p>By the same reason, the use this pragma inside modules is also
discouraged (though not as strongly discouraged as the case above.  
See below).</p>
<p>If you still have to write a module with this pragma, be very careful
of the load order.  See the codes below;</p>
<pre>
  <span class="comment"># called module</span>
  <span class="keyword">package</span> <span class="variable">Module_IN_BAR</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">encoding</span> <span class="string">"bar"</span><span class="operator">;</span>
  <span class="comment"># stuff in "bar" encoding here</span>
  <span class="number">1</span><span class="operator">;</span>
</pre>
<pre>
  <span class="comment"># caller script</span>
  <span class="keyword">use</span> <span class="variable">encoding</span> <span class="string">"foo"</span>
  <span class="keyword">use</span> <span class="variable">Module_IN_BAR</span><span class="operator">;</span>
  <span class="comment"># surprise! use encoding "bar" is in effect.</span>
</pre>
<p>The best way to avoid this oddity is to use this pragma RIGHT AFTER
other modules are loaded.  i.e.</p>
<pre>
  <span class="keyword">use</span> <span class="variable">Module_IN_BAR</span><span class="operator">;</span>
  <span class="keyword">use</span> <span class="variable">encoding</span> <span class="string">"foo"</span><span class="operator">;</span>
</pre>
<p>
</p>
<h2><a name="do_not_mix_multiple_encodings">DO NOT MIX MULTIPLE ENCODINGS</a></h2>
<p>Notice that only literals (string or regular expression) having only
legacy code points are affected: if you mix data like this</p>
<pre>
        <span class="operator">\</span><span class="variable">xDF</span><span class="operator">\x</span><span class="operator">{</span><span class="string">100</span><span class="operator">}</span>
</pre>
<p>the data is assumed to be in (Latin 1 and) Unicode, not in your native
encoding.  In other words, this will match in &quot;greek&quot;:</p>
<pre>
        <span class="string">"\xDF"</span> <span class="operator">=~</span> <span class="regex">/\x{3af}/</span>
</pre>
<p>but this will not</p>
<pre>
        <span class="string">"\xDF\x{100}"</span> <span class="operator">=~</span> <span class="regex">/\x{3af}\x{100}/</span>
</pre>
<p>since the <code>\xDF</code> (ISO 8859-7 GREEK SMALL LETTER IOTA WITH TONOS) on
the left will <strong>not</strong> be upgraded to <code>\x{3af}</code> (Unicode GREEK SMALL
LETTER IOTA WITH TONOS) because of the <code>\x{100}</code> on the left.  You
should not be mixing your legacy data and Unicode in the same string.</p>
<p>This pragma also affects encoding of the 0x80..0xFF code point range:
normally characters in that range are left as eight-bit bytes (unless
they are combined with characters with code points 0x100 or larger,
in which case all characters need to become UTF-8 encoded), but if
the <code>encoding</code> pragma is present, even the 0x80..0xFF range always
gets UTF-8 encoded.</p>
<p>After all, the best thing about this pragma is that you don't have to
resort to \x{....} just to spell your name in a native encoding.
So feel free to put your strings in your encoding in quotes and
regexes.</p>
<p>
</p>
<h2><a name="tr____with_ranges">tr/// with ranges</a></h2>
<p>The <strong>encoding</strong> pragma works by decoding string literals in
<a href="../lib/Pod/perlfunc.html#item_q_"><code>q//,qq//,qr//,qw///, qx//</code></a> and so forth.  In perl 5.8.0, this
does not apply to <a href="#item_tr_"><code>tr///</code></a>.  Therefore,</p>
<pre>
  <span class="keyword">use</span> <span class="variable">encoding</span> <span class="string">'euc-jp'</span><span class="operator">;</span>
  <span class="comment">#....</span>
  <span class="variable">$kana</span> <span class="operator">=~</span> <span class="regex">tr/\xA4\xA1-\xA4\xF3/\xA5\xA1-\xA5\xF3/</span><span class="operator">;</span>

⌨️ 快捷键说明

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