perluniintro.html
来自「perl教程」· HTML 代码 · 共 860 行 · 第 1/5 页
HTML
860 行
is now carried with the data, instead of being attached to the
operations. Only one case remains where an explicit <code>use utf8</code> is
needed: if your Perl script itself is encoded in UTF-8, you can use
UTF-8 in your identifier names, and in string and regular expression
literals, by saying <code>use utf8</code>. This is not the default because
scripts with legacy 8-bit data in them would break. See <a href="../../lib/utf8.html">the utf8 manpage</a>.</p>
<p>
</p>
<h2><a name="perl_s_unicode_model">Perl's Unicode Model</a></h2>
<p>Perl supports both pre-5.6 strings of eight-bit native bytes, and
strings of Unicode characters. The principle is that Perl tries to
keep its data as eight-bit bytes for as long as possible, but as soon
as Unicodeness cannot be avoided, the data is transparently upgraded
to Unicode.</p>
<p>Internally, Perl currently uses either whatever the native eight-bit
character set of the platform (for example Latin-1) is, defaulting to
UTF-8, to encode Unicode strings. Specifically, if all code points in
the string are <code>0xFF</code> or less, Perl uses the native eight-bit
character set. Otherwise, it uses UTF-8.</p>
<p>A user of Perl does not normally need to know nor care how Perl
happens to encode its internal strings, but it becomes relevant when
outputting Unicode strings to a stream without a PerlIO layer -- one with
the "default" encoding. In such a case, the raw bytes used internally
(the native character set or UTF-8, as appropriate for each string)
will be used, and a "Wide character" warning will be issued if those
strings contain a character beyond 0x00FF.</p>
<p>For example,</p>
<pre>
<span class="variable">perl</span> <span class="keyword">-e</span> <span class="string">'print "\x{DF}\n", "\x{0100}\x{DF}\n"'</span>
</pre>
<p>produces a fairly useless mixture of native bytes and UTF-8, as well
as a warning:</p>
<pre>
Wide character in print at ...</pre>
<p>To output UTF-8, use the <a href="../../lib/Pod/perlrun.html#item__3autf8"><code>:utf8</code></a> output layer. Prepending</p>
<pre>
<span class="keyword">binmode</span><span class="operator">(</span><span class="variable">STDOUT</span><span class="operator">,</span> <span class="string">":utf8"</span><span class="operator">);</span>
</pre>
<p>to this sample program ensures that the output is completely UTF-8,
and removes the program's warning.</p>
<p>You can enable automatic UTF-8-ification of your standard file
handles, default <a href="../../lib/Pod/perlfunc.html#item_open"><code>open()</code></a> layer, and <a href="../../lib/Pod/perlvar.html#item__argv"><code>@ARGV</code></a> by using either
the <code>-C</code> command line switch or the <a href="../../lib/Pod/perlrun.html#item_perl_unicode"><code>PERL_UNICODE</code></a> environment
variable, see <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a> for the documentation of the <code>-C</code> switch.</p>
<p>Note that this means that Perl expects other software to work, too:
if Perl has been led to believe that STDIN should be UTF-8, but then
STDIN coming in from another command is not UTF-8, Perl will complain
about the malformed UTF-8.</p>
<p>All features that combine Unicode and I/O also require using the new
PerlIO feature. Almost all Perl 5.8 platforms do use PerlIO, though:
you can see whether yours is by running "perl -V" and looking for
<code>useperlio=define</code>.</p>
<p>
</p>
<h2><a name="unicode_and_ebcdic">Unicode and EBCDIC</a></h2>
<p>Perl 5.8.0 also supports Unicode on EBCDIC platforms. There,
Unicode support is somewhat more complex to implement since
additional conversions are needed at every step. Some problems
remain, see <a href="../../lib/Pod/perlebcdic.html">the perlebcdic manpage</a> for details.</p>
<p>In any case, the Unicode support on EBCDIC platforms is better than
in the 5.6 series, which didn't work much at all for EBCDIC platform.
On EBCDIC platforms, the internal Unicode encoding form is UTF-EBCDIC
instead of UTF-8. The difference is that as UTF-8 is "ASCII-safe" in
that ASCII characters encode to UTF-8 as-is, while UTF-EBCDIC is
"EBCDIC-safe".</p>
<p>
</p>
<h2><a name="creating_unicode">Creating Unicode</a></h2>
<p>To create Unicode characters in literals for code points above <code>0xFF</code>,
use the <code>\x{...}</code> notation in double-quoted strings:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$smiley</span> <span class="operator">=</span> <span class="string">"\x{263a}"</span><span class="operator">;</span>
</pre>
<p>Similarly, it can be used in regular expression literals</p>
<pre>
<span class="variable">$smiley</span> <span class="operator">=~</span> <span class="regex">/\x{263a}/</span><span class="operator">;</span>
</pre>
<p>At run-time you can use <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr()</code></a>:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$hebrew_alef</span> <span class="operator">=</span> <span class="keyword">chr</span><span class="operator">(</span><span class="number">0x05d0</span><span class="operator">);</span>
</pre>
<p>See <a href="#further_resources">Further Resources</a> for how to find all these numeric codes.</p>
<p>Naturally, <a href="../../lib/Pod/perlfunc.html#item_ord"><code>ord()</code></a> will do the reverse: it turns a character into
a code point.</p>
<p>Note that <code>\x..</code> (no <code>{}</code> and only two hexadecimal digits), <code>\x{...}</code>,
and <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr(...)</code></a> for arguments less than <code>0x100</code> (decimal 256)
generate an eight-bit character for backward compatibility with older
Perls. For arguments of <code>0x100</code> or more, Unicode characters are
always produced. If you want to force the production of Unicode
characters regardless of the numeric value, use <a href="../../lib/Pod/perlfunc.html#item_pack"><code>pack("U", ...)</code></a>
instead of <code>\x..</code>, <code>\x{...}</code>, or <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr()</code></a>.</p>
<p>You can also use the <code>charnames</code> pragma to invoke characters
by name in double-quoted strings:</p>
<pre>
<span class="keyword">use</span> <span class="variable">charnames</span> <span class="string">':full'</span><span class="operator">;</span>
<span class="keyword">my</span> <span class="variable">$arabic_alef</span> <span class="operator">=</span> <span class="string">"\N{ARABIC LETTER ALEF}"</span><span class="operator">;</span>
</pre>
<p>And, as mentioned above, you can also <a href="../../lib/Pod/perlfunc.html#item_pack"><code>pack()</code></a> numbers into Unicode
characters:</p>
<pre>
<span class="keyword">my</span> <span class="variable">$georgian_an</span> <span class="operator">=</span> <span class="keyword">pack</span><span class="operator">(</span><span class="string">"U"</span><span class="operator">,</span> <span class="number">0x10a0</span><span class="operator">);</span>
</pre>
<p>Note that both <code>\x{...}</code> and <code>\N{...}</code> are compile-time string
constants: you cannot use variables in them. if you want similar
run-time functionality, use <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr()</code></a> and <code>charnames::vianame()</code>.</p>
<p>If you want to force the result to Unicode characters, use the special
<code>"U0"</code> prefix. It consumes no arguments but forces the result to be
in Unicode characters, instead of bytes.</p>
<pre>
<span class="keyword">my</span> <span class="variable">$chars</span> <span class="operator">=</span> <span class="keyword">pack</span><span class="operator">(</span><span class="string">"U0C*"</span><span class="operator">,</span> <span class="number">0x80</span><span class="operator">,</span> <span class="number">0x42</span><span class="operator">);</span>
</pre>
<p>Likewise, you can force the result to be bytes by using the special
<code>"C0"</code> prefix.</p>
<p>
</p>
<h2><a name="handling_unicode">Handling Unicode</a></h2>
<p>Handling Unicode is for the most part transparent: just use the
strings as usual. Functions like <a href="../../lib/Pod/perlfunc.html#item_index"><code>index()</code></a>, <a href="../../lib/Pod/perlfunc.html#item_length"><code>length()</code></a>, and
<a href="../../lib/Pod/perlvar.html#item_substr"><code>substr()</code></a> will work on the Unicode characters; regular expressions
will work on the Unicode characters (see <a href="../../lib/Pod/perlunicode.html">the perlunicode manpage</a> and <a href="../../lib/Pod/perlretut.html">the perlretut manpage</a>).</p>
<p>Note that Perl considers combining character sequences to be
separate characters, so for example</p>
<pre>
<span class="keyword">use</span> <span class="variable">charnames</span> <span class="string">':full'</span><span class="operator">;</span>
<span class="keyword">print</span> <span class="keyword">length</span><span class="operator">(</span><span class="string">"\N{LATIN CAPITAL LETTER A}\N{COMBINING ACUTE ACCENT}"</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>will print 2, not 1. The only exception is that regular expressions
have <code>\X</code> for matching a combining character sequence.</p>
<p>Life is not quite so transparent, however, when working with legacy
encodings, I/O, and certain special cases:</p>
<p>
</p>
<h2><a name="legacy_encodings">Legacy Encodings</a></h2>
<p>When you combine legacy data and Unicode the legacy data needs
to be upgraded to Unicode. Normally ISO 8859-1 (or EBCDIC, if
applicable) is assumed. You can override this assumption by
using the <code>encoding</code> pragma, for example</p>
<pre>
<span class="keyword">use</span> <span class="variable">encoding</span> <span class="string">'latin2'</span><span class="operator">;</span> <span class="comment"># ISO 8859-2</span>
</pre>
<p>in which case literals (string or regular expressions), <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr()</code></a>,
and <a href="../../lib/Pod/perlfunc.html#item_ord"><code>ord()</code></a> in your whole script are assumed to produce Unicode
characters from ISO 8859-2 code points. Note that the matching for
encoding names is forgiving: instead of <code>latin2</code> you could have
said <code>Latin 2</code>, or <code>iso8859-2</code>, or other variations. With just</p>
<pre>
<span class="keyword">use</span> <span class="variable">encoding</span><span class="operator">;</span>
</pre>
<p>the environment variable <a href="../../lib/Pod/perlrun.html#item_perl_encoding"><code>PERL_ENCODING</code></a> will be consulted.
If that variable isn't set, the encoding pragma will fail.</p>
<p>The <code>Encode</code> module knows about many encodings and has interfaces
for doing conversions between those encodings:</p>
<pre>
<span class="keyword">use</span> <span class="variable">Encode</span> <span class="string">'decode'</span><span class="operator">;</span>
<span class="variable">$data</span> <span class="operator">=</span> <span class="variable">decode</span><span class="operator">(</span><span class="string">"iso-8859-3"</span><span class="operator">,</span> <span class="variable">$data</span><span class="operator">);</span> <span class="comment"># convert from legacy to utf-8</span>
</pre>
<p>
</p>
<h2><a name="unicode_i_o">Unicode I/O</a></h2>
<p>Normally, writing out Unicode data</p>
<pre>
<span class="keyword">print</span> <span class="variable">FH</span> <span class="variable">$some_string_with_unicode</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>produces raw bytes that Perl happens to use to internally encode the
Unicode string. Perl's internal encoding depends on the system as
well as what characters happen to be in the string at the time. If
any of the characters are at code points <code>0x100</code> or above, you will get
a warning. To ensure that the output is explicitly rendered in the
encoding you desire--and to avoid the warning--open the stream with
the desired encoding. Some examples:</p>
<pre>
<span class="keyword">open</span> <span class="variable">FH</span><span class="operator">,</span> <span class="string">">:utf8"</span><span class="operator">,</span> <span class="string">"file"</span><span class="operator">;</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?