perluniintro.html

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

HTML
860 行
字号
material about the nature of the string has changed as far as Perl is
concerned.  Both before and after the call, the string <code>$data</code>
contains just a bunch of 8-bit bytes. As far as Perl is concerned,
the encoding of the string remains as &quot;system-native 8-bit bytes&quot;.</p>
<p>You might relate this to a fictional 'Translate' module:</p>
<pre>
   <span class="keyword">use</span> <span class="variable">Translate</span><span class="operator">;</span>
   <span class="keyword">my</span> <span class="variable">$phrase</span> <span class="operator">=</span> <span class="string">"Yes"</span><span class="operator">;</span>
   <span class="variable">Translate::from_to</span><span class="operator">(</span><span class="variable">$phrase</span><span class="operator">,</span> <span class="string">'english'</span><span class="operator">,</span> <span class="string">'deutsch'</span><span class="operator">);</span>
   <span class="comment">## phrase now contains "Ja"</span>
</pre>
<p>The contents of the string changes, but not the nature of the string.
Perl doesn't know any more after the call than before that the
contents of the string indicates the affirmative.</p>
<p>Back to converting data.  If you have (or want) data in your system's
native 8-bit encoding (e.g. Latin-1, EBCDIC, etc.), you can use
pack/unpack to convert to/from Unicode.</p>
<pre>
    <span class="variable">$native_string</span>  <span class="operator">=</span> <span class="keyword">pack</span><span class="operator">(</span><span class="string">"C*"</span><span class="operator">,</span> <span class="keyword">unpack</span><span class="operator">(</span><span class="string">"U*"</span><span class="operator">,</span> <span class="variable">$Unicode_string</span><span class="operator">));</span>
    <span class="variable">$Unicode_string</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="keyword">unpack</span><span class="operator">(</span><span class="string">"C*"</span><span class="operator">,</span> <span class="variable">$native_string</span><span class="operator">));</span>
</pre>
<p>If you have a sequence of bytes you <strong>know</strong> is valid UTF-8,
but Perl doesn't know it yet, you can make Perl a believer, too:</p>
<pre>
    <span class="keyword">use</span> <span class="variable">Encode</span> <span class="string">'decode_utf8'</span><span class="operator">;</span>
    <span class="variable">$Unicode</span> <span class="operator">=</span> <span class="variable">decode_utf8</span><span class="operator">(</span><span class="variable">$bytes</span><span class="operator">);</span>
</pre>
<p>You can convert well-formed UTF-8 to a sequence of bytes, but if
you just want to convert random binary data into UTF-8, you can't.
<strong>Any random collection of bytes isn't well-formed UTF-8</strong>.  You can
use <a href="../../lib/Pod/perlfunc.html#item_unpack"><code>unpack(&quot;C*&quot;, $string)</code></a> for the former, and you can create
well-formed Unicode data by <a href="../../lib/Pod/perlfunc.html#item_pack"><code>pack(&quot;U*&quot;, 0xff, ...)</code></a>.</p>
</li>
<li>
<p>How Do I Display Unicode?  How Do I Input Unicode?</p>
<p>See <a href="http://www.alanwood.net/unicode/">http://www.alanwood.net/unicode/</a> and
<a href="http://www.cl.cam.ac.uk/~mgk25/unicode.html">http://www.cl.cam.ac.uk/~mgk25/unicode.html</a></p>
</li>
<li>
<p>How Does Unicode Work With Traditional Locales?</p>
<p>In Perl, not very well.  Avoid using locales through the <code>locale</code>
pragma.  Use only one or the other.  But see <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a> for the
description of the <code>-C</code> switch and its environment counterpart,
<code>$ENV{PERL_UNICODE}</code> to see how to enable various Unicode features,
for example by using locale settings.</p>
</li>
</ul>
<p>
</p>
<h2><a name="hexadecimal_notation">Hexadecimal Notation</a></h2>
<p>The Unicode standard prefers using hexadecimal notation because
that more clearly shows the division of Unicode into blocks of 256 characters.
Hexadecimal is also simply shorter than decimal.  You can use decimal
notation, too, but learning to use hexadecimal just makes life easier
with the Unicode standard.  The <code>U+HHHH</code> notation uses hexadecimal,
for example.</p>
<p>The <code>0x</code> prefix means a hexadecimal number, the digits are 0-9 <em>and</em>
a-f (or A-F, case doesn't matter).  Each hexadecimal digit represents
four bits, or half a byte.  <code>print 0x..., &quot;\n&quot;</code> will show a
hexadecimal number in decimal, and <code>printf &quot;%x\n&quot;, $decimal</code> will
show a decimal number in hexadecimal.  If you have just the
&quot;hex digits&quot; of a hexadecimal number, you can use the <a href="../../lib/Pod/perlfunc.html#item_hex"><code>hex()</code></a> function.</p>
<pre>
    <span class="keyword">print</span> <span class="number">0x0009</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 9</span>
    <span class="keyword">print</span> <span class="number">0x000a</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 10</span>
    <span class="keyword">print</span> <span class="number">0x000f</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 15</span>
    <span class="keyword">print</span> <span class="number">0x0010</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 16</span>
    <span class="keyword">print</span> <span class="number">0x0011</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 17</span>
    <span class="keyword">print</span> <span class="number">0x0100</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 256</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="number">0x0041</span><span class="operator">,</span> <span class="string">"\n"</span><span class="operator">;</span>    <span class="comment"># 65</span>
</pre>
<pre>
    <span class="keyword">printf</span> <span class="string">"%x\n"</span><span class="operator">,</span>  <span class="number">65</span><span class="operator">;</span>    <span class="comment"># 41</span>
    <span class="keyword">printf</span> <span class="string">"%#x\n"</span><span class="operator">,</span> <span class="number">65</span><span class="operator">;</span>    <span class="comment"># 0x41</span>
</pre>
<pre>
    <span class="keyword">print</span> <span class="keyword">hex</span><span class="operator">(</span><span class="string">"41"</span><span class="operator">),</span> <span class="string">"\n"</span><span class="operator">;</span> <span class="comment"># 65</span>
</pre>
<p>
</p>
<h2><a name="further_resources">Further Resources</a></h2>
<ul>
<li>
<p>Unicode Consortium</p>
<pre>
    <a href="http://www.unicode.org/">http://www.unicode.org/</a></pre>
</li>
<li>
<p>Unicode FAQ</p>
<pre>
    <a href="http://www.unicode.org/unicode/faq/">http://www.unicode.org/unicode/faq/</a></pre>
</li>
<li>
<p>Unicode Glossary</p>
<pre>
    <a href="http://www.unicode.org/glossary/">http://www.unicode.org/glossary/</a></pre>
</li>
<li>
<p>Unicode Useful Resources</p>
<pre>
    <a href="http://www.unicode.org/unicode/onlinedat/resources.html">http://www.unicode.org/unicode/onlinedat/resources.html</a></pre>
</li>
<li>
<p>Unicode and Multilingual Support in HTML, Fonts, Web Browsers and Other Applications</p>
<pre>
    <a href="http://www.alanwood.net/unicode/">http://www.alanwood.net/unicode/</a></pre>
</li>
<li>
<p>UTF-8 and Unicode FAQ for Unix/Linux</p>
<pre>
    <a href="http://www.cl.cam.ac.uk/~mgk25/unicode.html">http://www.cl.cam.ac.uk/~mgk25/unicode.html</a></pre>
</li>
<li>
<p>Legacy Character Sets</p>
<pre>
    <a href="http://www.czyborra.com/">http://www.czyborra.com/</a>
    <a href="http://www.eki.ee/letter/">http://www.eki.ee/letter/</a></pre>
</li>
<li>
<p>The Unicode support files live within the Perl installation in the
directory</p>
<pre>
    <span class="variable">$Config</span><span class="operator">{</span><span class="string">installprivlib</span><span class="operator">}</span><span class="operator">/</span><span class="variable">unicore</span>
</pre>
<p>in Perl 5.8.0 or newer, and</p>
<pre>
    <span class="variable">$Config</span><span class="operator">{</span><span class="string">installprivlib</span><span class="operator">}</span><span class="operator">/</span><span class="variable">unicode</span>
</pre>
<p>in the Perl 5.6 series.  (The renaming to <em>lib/unicore</em> was done to
avoid naming conflicts with lib/Unicode in case-insensitive filesystems.)
The main Unicode data file is <em>UnicodeData.txt</em> (or <em>Unicode.301</em> in
Perl 5.6.1.)  You can find the <code>$Config{installprivlib}</code> by</p>
<pre>
    perl &quot;-V:installprivlib&quot;</pre>
<p>You can explore various information from the Unicode data files using
the <code>Unicode::UCD</code> module.</p>
</li>
</ul>
<p>
</p>
<hr />
<h1><a name="unicode_in_older_perls">UNICODE IN OLDER PERLS</a></h1>
<p>If you cannot upgrade your Perl to 5.8.0 or later, you can still
do some Unicode processing by using the modules <code>Unicode::String</code>,
<code>Unicode::Map8</code>, and <code>Unicode::Map</code>, available from CPAN.
If you have the GNU recode installed, you can also use the
Perl front-end <code>Convert::Recode</code> for character conversions.</p>
<p>The following are fast conversions from ISO 8859-1 (Latin-1) bytes
to UTF-8 bytes and back, the code works even with older Perl 5 versions.</p>
<pre>
    <span class="comment"># ISO 8859-1 to UTF-8</span>
    <span class="regex">s/([\x80-\xFF])/chr(0xC0|ord($1)&gt;&gt;6).chr(0x80|ord($1)&amp;0x3F)/eg</span><span class="operator">;</span>
</pre>
<pre>
    <span class="comment"># UTF-8 to ISO 8859-1</span>
    <span class="regex">s/([\xC2\xC3])([\x80-\xBF])/chr(ord($1)&lt;&lt;6&amp;0xC0|ord($2)&amp;0x3F)/eg</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p><a href="../../lib/Pod/perlunicode.html">the perlunicode manpage</a>, <a href="../../lib/Encode.html">the Encode manpage</a>, <a href="../../lib/encoding.html">the encoding manpage</a>, <a href="../../lib/open.html">the open manpage</a>, <a href="../../lib/utf8.html">the utf8 manpage</a>, <a href="../../lib/bytes.html">the bytes manpage</a>,
<a href="../../lib/Pod/perlretut.html">the perlretut manpage</a>, <a href="../../lib/Pod/perlrun.html">the perlrun manpage</a>, <a href="../../lib/Unicode/Collate.html">the Unicode::Collate manpage</a>, <a href="../../lib/Unicode/Normalize.html">the Unicode::Normalize manpage</a>,
<a href="../../lib/Unicode/UCD.html">the Unicode::UCD manpage</a></p>
<p>
</p>
<hr />
<h1><a name="acknowledgments">ACKNOWLEDGMENTS</a></h1>
<p>Thanks to the kind readers of the <a href="mailto:perl5-porters@perl.org,">perl5-porters@perl.org,</a>
<a href="mailto:perl-unicode@perl.org,">perl-unicode@perl.org,</a> <a href="m

⌨️ 快捷键说明

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