perlunicode.html
来自「perl教程」· HTML 代码 · 共 1,163 行 · 第 1/5 页
HTML
1,163 行
U+10000..U+3FFFF F0 90..BF 80..BF 80..BF
U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF
U+100000..U+10FFFF F4 80..8F 80..BF 80..BF</pre>
<p>Note the <code>A0..BF</code> in <code>U+0800..U+0FFF</code>, the <code>80..9F</code> in
<code>U+D000...U+D7FF</code>, the <code>90..B</code>F in <code>U+10000..U+3FFFF</code>, and the
<code>80...8F</code> in <code>U+100000..U+10FFFF</code>. The "gaps" are caused by legal
UTF-8 avoiding non-shortest encodings: it is technically possible to
UTF-8-encode a single code point in different ways, but that is
explicitly forbidden, and the shortest possible encoding should always
be used. So that's what Perl does.</p>
<p>Another way to look at it is via bits:</p>
<pre>
Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte</pre>
<pre>
0aaaaaaa 0aaaaaaa
00000bbbbbaaaaaa 110bbbbb 10aaaaaa
ccccbbbbbbaaaaaa 1110cccc 10bbbbbb 10aaaaaa
00000dddccccccbbbbbbaaaaaa 11110ddd 10cccccc 10bbbbbb 10aaaaaa</pre>
<p>As you can see, the continuation bytes all begin with <code>10</code>, and the
leading bits of the start byte tell how many bytes the are in the
encoded character.</p>
</li>
<li>
<p>UTF-EBCDIC</p>
<p>Like UTF-8 but EBCDIC-safe, in the way that UTF-8 is ASCII-safe.</p>
</li>
<li>
<p>UTF-16, UTF-16BE, UTF-16LE, Surrogates, and BOMs (Byte Order Marks)</p>
<p>The followings items are mostly for reference and general Unicode
knowledge, Perl doesn't use these constructs internally.</p>
<p>UTF-16 is a 2 or 4 byte encoding. The Unicode code points
<code>U+0000..U+FFFF</code> are stored in a single 16-bit unit, and the code
points <code>U+10000..U+10FFFF</code> in two 16-bit units. The latter case is
using <em>surrogates</em>, the first 16-bit unit being the <em>high
surrogate</em>, and the second being the <em>low surrogate</em>.</p>
<p>Surrogates are code points set aside to encode the <code>U+10000..U+10FFFF</code>
range of Unicode code points in pairs of 16-bit units. The <em>high
surrogates</em> are the range <code>U+D800..U+DBFF</code>, and the <em>low surrogates</em>
are the range <code>U+DC00..U+DFFF</code>. The surrogate encoding is</p>
<pre>
<span class="variable">$hi</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$uni</span> <span class="operator">-</span> <span class="number">0x10000</span><span class="operator">)</span> <span class="operator">/</span> <span class="number">0x400</span> <span class="operator">+</span> <span class="number">0xD800</span><span class="operator">;</span>
<span class="variable">$lo</span> <span class="operator">=</span> <span class="operator">(</span><span class="variable">$uni</span> <span class="operator">-</span> <span class="number">0x10000</span><span class="operator">)</span> <span class="operator">% </span><span class="number">0x400</span> <span class="operator">+</span> <span class="number">0xDC00</span><span class="operator">;</span>
</pre>
<p>and the decoding is</p>
<pre>
<span class="variable">$uni</span> <span class="operator">=</span> <span class="number">0x10000</span> <span class="operator">+</span> <span class="operator">(</span><span class="variable">$hi</span> <span class="operator">-</span> <span class="number">0xD800</span><span class="operator">)</span> <span class="operator">*</span> <span class="number">0x400</span> <span class="operator">+</span> <span class="operator">(</span><span class="variable">$lo</span> <span class="operator">-</span> <span class="number">0xDC00</span><span class="operator">);</span>
</pre>
<p>If you try to generate surrogates (for example by using chr()), you
will get a warning if warnings are turned on, because those code
points are not valid for a Unicode character.</p>
<p>Because of the 16-bitness, UTF-16 is byte-order dependent. UTF-16
itself can be used for in-memory computations, but if storage or
transfer is required either UTF-16BE (big-endian) or UTF-16LE
(little-endian) encodings must be chosen.</p>
<p>This introduces another problem: what if you just know that your data
is UTF-16, but you don't know which endianness? Byte Order Marks, or
BOMs, are a solution to this. A special character has been reserved
in Unicode to function as a byte order marker: the character with the
code point <code>U+FEFF</code> is the BOM.</p>
<p>The trick is that if you read a BOM, you will know the byte order,
since if it was written on a big-endian platform, you will read the
bytes <code>0xFE 0xFF</code>, but if it was written on a little-endian platform,
you will read the bytes <code>0xFF 0xFE</code>. (And if the originating platform
was writing in UTF-8, you will read the bytes <code>0xEF 0xBB 0xBF</code>.)</p>
<p>The way this trick works is that the character with the code point
<code>U+FFFE</code> is guaranteed not to be a valid Unicode character, so the
sequence of bytes <code>0xFF 0xFE</code> is unambiguously "BOM, represented in
little-endian format" and cannot be <code>U+FFFE</code>, represented in big-endian
format".</p>
</li>
<li>
<p>UTF-32, UTF-32BE, UTF-32LE</p>
<p>The UTF-32 family is pretty much like the UTF-16 family, expect that
the units are 32-bit, and therefore the surrogate scheme is not
needed. The BOM signatures will be <code>0x00 0x00 0xFE 0xFF</code> for BE and
<code>0xFF 0xFE 0x00 0x00</code> for LE.</p>
</li>
<li>
<p>UCS-2, UCS-4</p>
<p>Encodings defined by the ISO 10646 standard. UCS-2 is a 16-bit
encoding. Unlike UTF-16, UCS-2 is not extensible beyond <code>U+FFFF</code>,
because it does not use surrogates. UCS-4 is a 32-bit encoding,
functionally identical to UTF-32.</p>
</li>
<li>
<p>UTF-7</p>
<p>A seven-bit safe (non-eight-bit) encoding, which is useful if the
transport or storage is not eight-bit safe. Defined by RFC 2152.</p>
</li>
</ul>
<p>
</p>
<h2><a name="security_implications_of_unicode">Security Implications of Unicode</a></h2>
<ul>
<li>
<p>Malformed UTF-8</p>
<p>Unfortunately, the specification of UTF-8 leaves some room for
interpretation of how many bytes of encoded output one should generate
from one input Unicode character. Strictly speaking, the shortest
possible sequence of UTF-8 bytes should be generated,
because otherwise there is potential for an input buffer overflow at
the receiving end of a UTF-8 connection. Perl always generates the
shortest length UTF-8, and with warnings on Perl will warn about
non-shortest length UTF-8 along with other malformations, such as the
surrogates, which are not real Unicode code points.</p>
</li>
<li>
<p>Regular expressions behave slightly differently between byte data and
character (Unicode) data. For example, the "word character" character
class <code>\w</code> will work differently depending on if data is eight-bit bytes
or Unicode.</p>
<p>In the first case, the set of <code>\w</code> characters is either small--the
default set of alphabetic characters, digits, and the "_"--or, if you
are using a locale (see <a href="../../lib/Pod/perllocale.html">the perllocale manpage</a>), the <code>\w</code> might contain a few
more letters according to your language and country.</p>
<p>In the second case, the <code>\w</code> set of characters is much, much larger.
Most importantly, even in the set of the first 256 characters, it will
probably match different characters: unlike most locales, which are
specific to a language and country pair, Unicode classifies all the
characters that are letters <em>somewhere</em> as <code>\w</code>. For example, your
locale might not think that LATIN SMALL LETTER ETH is a letter (unless
you happen to speak Icelandic), but Unicode does.</p>
<p>As discussed elsewhere, Perl has one foot (two hooves?) planted in
each of two worlds: the old world of bytes and the new world of
characters, upgrading from bytes to characters when necessary.
If your legacy code does not explicitly use Unicode, no automatic
switch-over to characters should happen. Characters shouldn't get
downgraded to bytes, either. It is possible to accidentally mix bytes
and characters, however (see <a href="../../lib/Pod/perluniintro.html">the perluniintro manpage</a>), in which case <code>\w</code> in
regular expressions might start behaving differently. Review your
code. Use warnings and the <code>strict</code> pragma.</p>
</li>
</ul>
<p>
</p>
<h2><a name="unicode_in_perl_on_ebcdic">Unicode in Perl on EBCDIC</a></h2>
<p>The way Unicode is handled on EBCDIC platforms is still
experimental. On such platforms, references to UTF-8 encoding in this
document and elsewhere should be read as meaning the UTF-EBCDIC
specified in Unicode Technical Report 16, unless ASCII vs. EBCDIC issues
are specifically discussed. There is no <code>utfebcdic</code> pragma or
":utfebcdic" layer; rather, "utf8" and ":utf8" are reused to mean
the platform's "natural" 8-bit encoding of Unicode. See <a href="../../lib/Pod/perlebcdic.html">the perlebcdic manpage</a>
for more discussion of the issues.</p>
<p>
</p>
<h2><a name="locales">Locales</a></h2>
<p>Usually locale settings and Unicode do not affect each other, but
there are a couple of exceptions:</p>
<ul>
<li>
<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>
</li>
<li>
<p>Perl tries really hard to work both with Unicode and the old
byte-oriented world. Most often this is nice, but sometimes Perl's
straddling of the proverbial fence causes problems.</p>
</li>
</ul>
<p>
</p>
<h2><a name="when_unicode_does_not_happen">When Unicode Does Not Happen</a></h2>
<p>While Perl does have extensive ways to input and output in Unicode,
and few other 'entry points' like the @ARGV which can be interpreted
as Unicode (UTF-8), there still are many places where Unicode (in some
encoding or another) could be given as arguments or received as
results, or both, but it is not.</p>
<p>The following are such interfaces. For all of these interfaces Perl
currently (as of 5.8.3) simply assumes byte strings both as arguments
and results, or UTF-8 strings if the <code>encoding</code> pragma has been used.</p>
<p>One reason why Perl does not attempt to resolve the role of Unicode in
this cases is that the answers are highly dependent on the operating
system and the file system(s). For example, whether filenames can be
in Unicode, and in exactly what kind of encoding, is not exactly a
portable concept. Similarly for the qx and system: how well will the
'command line interface' (and which of them?) handle Unicode?</p>
<ul>
<li>
<p>chdir, chmod, chown, chroot, exec, link, lstat, mkdir,
rename, rmdir, stat, symlink, truncate, unlink, utime, -X</p>
</li>
<li>
<p>%ENV</p>
</li>
<li>
<p>glob (aka the <*>)</p>
</li>
<li>
<p>open, opendir, sysopen</p>
</li>
<li>
<p>qx (aka the backtick operator), system</p>
</li>
<li>
<p>readdir, readlink</p>
</li>
</ul>
<p>
</p>
<h2><a name="forcing_unicode_in_perl__or_unforcing_unicode_in_perl_">Forcing Unicode in Perl (Or Unforcing Unicode in Perl)</a></h2>
<p>Sometimes (see <a href="#when_unicode_does_not_happen">When Unicode Does Not Happen</a>) there are
situations where you simply need to force Perl to believe that a byte
string is UTF-8, or vice versa. The low-level calls
utf8::upgrade($bytestring) and utf8::downgrade($utf8string) are
the answers.</p>
<p>Do not use them without careful thought, though: Perl may easily get
very confused, angry, or even crash, if you suddenly change the 'nature'
of scalar like that. Especially careful you have to be if you use the
utf8::upgrade(): any random byte string is not valid UTF-8.</p>
<p>
</p>
<h2><a name="using_unicode_in_xs">Using Unicode in XS</a></h2>
<p>If you want to handle Perl Unicode in XS extensions, you may find the
following C APIs useful. See also <a href="../../lib/Pod/perlguts.html#unicode_support">Unicode Support in the perlguts manpage</a> for an
explanation about Unicode at the XS level, and <a href="../../lib/Pod/perlapi.html">the perlapi manpage</a> for the API
details.</p>
<ul>
<li>
<p><code>DO_UTF8(sv)</code> returns true if the <code>UTF8</code> flag is on and the bytes
pragma is not in effect. <code>SvUTF8(sv)</code> returns true is the <code>UTF8</code>
flag is on; the bytes pragma is ignored. The <code>UTF8</code> flag being on
does <strong>not</strong> mean that there are any characters of code points greater
than 255 (or 127) in the scalar or that there are even any characters
in the scalar. What the <code>UTF8</code> flag means is that the sequence of
octets in the representation of the scalar is the sequence of UTF-8
encoded code points of the characters of a string. The <code>UTF8</code> flag
being off means that each octet in this representation encodes a
single character with code point 0..255 within the stri
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?