perluniintro.html

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

HTML
860 行
字号
    perl -MDevel::Peek -e 'Dump(chr(0x100))'</pre>
<p>That shows the <code>UTF8</code> flag in FLAGS and both the UTF-8 bytes
and Unicode characters in <code>PV</code>.  See also later in this document
the discussion about the <code>utf8::is_utf8()</code> function.</p>
</li>
</ul>
<p>
</p>
<h2><a name="advanced_topics">Advanced Topics</a></h2>
<ul>
<li>
<p>String Equivalence</p>
<p>The question of string equivalence turns somewhat complicated
in Unicode: what do you mean by &quot;equal&quot;?</p>
<p>(Is <code>LATIN CAPITAL LETTER A WITH ACUTE</code> equal to
<code>LATIN CAPITAL LETTER A</code>?)</p>
<p>The short answer is that by default Perl compares equivalence (<code>eq</code>,
<code>ne</code>) based only on code points of the characters.  In the above
case, the answer is no (because 0x00C1 != 0x0041).  But sometimes, any
CAPITAL LETTER As should be considered equal, or even As of any case.</p>
<p>The long answer is that you need to consider character normalization
and casing issues: see <a href="../../lib/Unicode/Normalize.html">the Unicode::Normalize manpage</a>, Unicode Technical
Reports #15 and #21, <em>Unicode Normalization Forms</em> and <em>Case
Mappings</em>, <a href="http://www.unicode.org/unicode/reports/tr15/">http://www.unicode.org/unicode/reports/tr15/</a> and 
<a href="http://www.unicode.org/unicode/reports/tr21/">http://www.unicode.org/unicode/reports/tr21/</a></p>
<p>As of Perl 5.8.0, the &quot;Full&quot; case-folding of <em>Case
Mappings/SpecialCasing</em> is implemented.</p>
</li>
<li>
<p>String Collation</p>
<p>People like to see their strings nicely sorted--or as Unicode
parlance goes, collated.  But again, what do you mean by collate?</p>
<p>(Does <code>LATIN CAPITAL LETTER A WITH ACUTE</code> come before or after
<code>LATIN CAPITAL LETTER A WITH GRAVE</code>?)</p>
<p>The short answer is that by default, Perl compares strings (<code>lt</code>,
<code>le</code>, <code>cmp</code>, <code>ge</code>, <code>gt</code>) based only on the code points of the
characters.  In the above case, the answer is &quot;after&quot;, since
<code>0x00C1</code> &gt; <code>0x00C0</code>.</p>
<p>The long answer is that &quot;it depends&quot;, and a good answer cannot be
given without knowing (at the very least) the language context.
See <a href="../../lib/Unicode/Collate.html">the Unicode::Collate manpage</a>, and <em>Unicode Collation Algorithm</em>
<a href="http://www.unicode.org/unicode/reports/tr10/">http://www.unicode.org/unicode/reports/tr10/</a></p>
</li>
</ul>
<p>
</p>
<h2><a name="miscellaneous">Miscellaneous</a></h2>
<ul>
<li>
<p>Character Ranges and Classes</p>
<p>Character ranges in regular expression character classes (<code>/[a-z]/</code>)
and in the <a href="../../lib/Pod/perlfunc.html#item_tr_"><code>tr///</code></a> (also known as <a href="../../lib/Pod/perlfunc.html#item_y_"><code>y///</code></a>) operator are not magically
Unicode-aware.  What this means that <code>[A-Za-z]</code> will not magically start
to mean &quot;all alphabetic letters&quot;; not that it does mean that even for
8-bit characters, you should be using <code>/[[:alpha:]]/</code> in that case.</p>
<p>For specifying character classes like that in regular expressions,
you can use the various Unicode properties--<code>\pL</code>, or perhaps
<code>\p{Alphabetic}</code>, in this particular case.  You can use Unicode
code points as the end points of character ranges, but there is no
magic associated with specifying a certain range.  For further
information--there are dozens of Unicode character classes--see
<a href="../../lib/Pod/perlunicode.html">the perlunicode manpage</a>.</p>
</li>
<li>
<p>String-To-Number Conversions</p>
<p>Unicode does define several other decimal--and numeric--characters
besides the familiar 0 to 9, such as the Arabic and Indic digits.
Perl does not support string-to-number conversion for digits other
than ASCII 0 to 9 (and ASCII a to f for hexadecimal).</p>
</li>
</ul>
<p>
</p>
<h2><a name="questions_with_answers">Questions With Answers</a></h2>
<ul>
<li>
<p>Will My Old Scripts Break?</p>
<p>Very probably not.  Unless you are generating Unicode characters
somehow, old behaviour should be preserved.  About the only behaviour
that has changed and which could start generating Unicode is the old
behaviour of <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr()</code></a> where supplying an argument more than 255
produced a character modulo 255.  <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr(300)</code></a>, for example, was equal
to <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr(45)</code></a> or &quot;-&quot; (in ASCII), now it is LATIN CAPITAL LETTER I WITH
BREVE.</p>
</li>
<li>
<p>How Do I Make My Scripts Work With Unicode?</p>
<p>Very little work should be needed since nothing changes until you
generate Unicode data.  The most important thing is getting input as
Unicode; for that, see the earlier I/O discussion.</p>
</li>
<li>
<p>How Do I Know Whether My String Is In Unicode?</p>
<p>You shouldn't care.  No, you really shouldn't.  No, really.  If you
have to care--beyond the cases described above--it means that we
didn't get the transparency of Unicode quite right.</p>
<p>Okay, if you insist:</p>
<pre>
    <span class="keyword">print</span> <span class="variable">utf8::is_utf8</span><span class="operator">(</span><span class="variable">$string</span><span class="operator">)</span> <span class="operator">?</span> <span class="number">1</span> <span class="operator">:</span> <span class="number">0</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>
</pre>
<p>But note that this doesn't mean that any of the characters in the
string are necessary UTF-8 encoded, or that any of the characters have
code points greater than 0xFF (255) or even 0x80 (128), or that the
string has any characters at all.  All the <code>is_utf8()</code> does is to
return the value of the internal &quot;utf8ness&quot; flag attached to the
<code>$string</code>.  If the flag is off, the bytes in the scalar are interpreted
as a single byte encoding.  If the flag is on, the bytes in the scalar
are interpreted as the (multi-byte, variable-length) UTF-8 encoded code
points of the characters.  Bytes added to an UTF-8 encoded string are
automatically upgraded to UTF-8.  If mixed non-UTF-8 and UTF-8 scalars
are merged (double-quoted interpolation, explicit concatenation, and
printf/sprintf parameter substitution), the result will be UTF-8 encoded
as if copies of the byte strings were upgraded to UTF-8: for example,</p>
<pre>
    <span class="variable">$a</span> <span class="operator">=</span> <span class="string">"ab\x80c"</span><span class="operator">;</span>
    <span class="variable">$b</span> <span class="operator">=</span> <span class="string">"\x{100}"</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="string">"$a = $b\n"</span><span class="operator">;</span>
</pre>
<p>the output string will be UTF-8-encoded <code>ab\x80c = \x{100}\n</code>, but
<a href="../../lib/Pod/perlvar.html#item__a"><code>$a</code></a> will stay byte-encoded.</p>
<p>Sometimes you might really need to know the byte length of a string
instead of the character length. For that use either the
<code>Encode::encode_utf8()</code> function or the <code>bytes</code> pragma and its only
defined function <a href="../../lib/Pod/perlfunc.html#item_length"><code>length()</code></a>:</p>
<pre>
    <span class="keyword">my</span> <span class="variable">$unicode</span> <span class="operator">=</span> <span class="keyword">chr</span><span class="operator">(</span><span class="number">0x100</span><span class="operator">);</span>
    <span class="keyword">print</span> <span class="keyword">length</span><span class="operator">(</span><span class="variable">$unicode</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span> <span class="comment"># will print 1</span>
    <span class="keyword">require</span> <span class="variable">Encode</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="keyword">length</span><span class="operator">(</span><span class="variable">Encode::encode_utf8</span><span class="operator">(</span><span class="variable">$unicode</span><span class="operator">)),</span> <span class="string">"\n"</span><span class="operator">;</span> <span class="comment"># will print 2</span>
    <span class="keyword">use</span> <span class="variable">bytes</span><span class="operator">;</span>
    <span class="keyword">print</span> <span class="keyword">length</span><span class="operator">(</span><span class="variable">$unicode</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span> <span class="comment"># will also print 2</span>
                                  <span class="comment"># (the 0xC4 0x80 of the UTF-8)</span>
</pre>
</li>
<li>
<p>How Do I Detect Data That's Not Valid In a Particular Encoding?</p>
<p>Use the <code>Encode</code> package to try converting it.
For example,</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Encode</span> <span class="string">'decode_utf8'</span><span class="operator">;</span>
    <span class="keyword">if</span> <span class="operator">(</span><span class="variable">decode_utf8</span><span class="operator">(</span><span class="variable">$string_of_bytes_that_I_think_is_utf8</span><span class="operator">))</span> <span class="operator">{</span>
        <span class="comment"># valid</span>
    <span class="operator">}</span> <span class="keyword">else</span> <span class="operator">{</span>
        <span class="comment"># invalid</span>
    <span class="operator">}</span>
</pre>
<p>For UTF-8 only, you can use:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">warnings</span><span class="operator">;</span>
    <span class="variable">@chars</span> <span class="operator">=</span> <span class="keyword">unpack</span><span class="operator">(</span><span class="string">"U0U*"</span><span class="operator">,</span> <span class="variable">$string_of_bytes_that_I_think_is_utf8</span><span class="operator">);</span>
</pre>
<p>If invalid, a <code>Malformed UTF-8 character (byte 0x##) in unpack</code>
warning is produced. The &quot;U0&quot; means &quot;expect strictly UTF-8 encoded
Unicode&quot;.  Without that the <a href="../../lib/Pod/perlfunc.html#item_unpack"><code>unpack(&quot;U*&quot;, ...)</code></a> would accept also
data like <a href="../../lib/Pod/perlfunc.html#item_chr"><code>chr(0xFF</code></a>), similarly to the <a href="../../lib/Pod/perlfunc.html#item_pack"><code>pack</code></a> as we saw earlier.</p>
</li>
<li>
<p>How Do I Convert Binary Data Into a Particular Encoding, Or Vice Versa?</p>
<p>This probably isn't as useful as you might think.
Normally, you shouldn't need to.</p>
<p>In one sense, what you are asking doesn't make much sense: encodings
are for characters, and binary data are not &quot;characters&quot;, so converting
&quot;data&quot; into some encoding isn't meaningful unless you know in what
character set and encoding the binary data is in, in which case it's
not just binary data, now is it?</p>
<p>If you have a raw sequence of bytes that you know should be
interpreted via a particular encoding, you can use <code>Encode</code>:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Encode</span> <span class="string">'from_to'</span><span class="operator">;</span>
    <span class="variable">from_to</span><span class="operator">(</span><span class="variable">$data</span><span class="operator">,</span> <span class="string">"iso-8859-1"</span><span class="operator">,</span> <span class="string">"utf-8"</span><span class="operator">);</span> <span class="comment"># from latin-1 to utf-8</span>
</pre>
<p>The call to <code>from_to()</code> changes the bytes in <code>$data</code>, but nothing

⌨️ 快捷键说明

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