/usr/share/doc/perl-doc-html/perlunifaq.html is in perl-doc-html 5.22.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>perlunifaq - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-gb">
<link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
<link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
<link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>
<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
<div id="page">
<div id="header">
<div id="homepage_link">
<a href="index.html"></a>
</div>
<div id="strapline">
Perl Programming Documentation
</div>
<div id="download_link" class="download">
<a href="http://www.perl.org/get.html">Download Perl</a>
</div>
<div id="explore_link" class="download">
<a id="explore_anchor" href="#">Explore</a>
</div>
</div>
<div id="body">
<div id="left_column">
<div class="side_group">
<div class="side_panel doc_panel">
<p>Manual</p>
<ul>
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">History / Changes</a>
<li><a href="index-licence.html">License</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Reference</p>
<ul>
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Special Variables</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platform Specific</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Modules</p>
<ul>
<li>
<a href="index-modules-A.html">A</a>
•
<a href="index-modules-B.html">B</a>
•
<a href="index-modules-C.html">C</a>
•
<a href="index-modules-D.html">D</a>
•
<a href="index-modules-E.html">E</a>
<li>
<a href="index-modules-F.html">F</a>
•
<a href="index-modules-G.html">G</a>
•
<a href="index-modules-H.html">H</a>
•
<a href="index-modules-I.html">I</a>
•
<a href="index-modules-L.html">L</a>
<li>
<a href="index-modules-M.html">M</a>
•
<a href="index-modules-N.html">N</a>
•
<a href="index-modules-O.html">O</a>
•
<a href="index-modules-P.html">P</a>
•
<a href="index-modules-S.html">S</a>
<li>
<a href="index-modules-T.html">T</a>
•
<a href="index-modules-U.html">U</a>
•
<a href="index-modules-X.html">X</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Tools</p>
<ul>
<li><a href="preferences.html">Preferences</a>
</ul>
</div>
</div>
</div>
<div id="centre_column">
<div id="content_header">
<div id="title_bar">
<div id="page_name">
<h1>perlunifaq</h1>
</div>
<div id="perl_version">
Perl 5 version 22.0 documentation
</div>
<div class="page_links" id="page_links_top">
<a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
</div>
<div class="page_links" id="page_links_bottom">
<a href="#" id="page_index_toggle">Show page index</a> •
<a href="#" id="recent_pages_toggle">Show recent pages</a>
</div>
<div id="search_form">
<form action="search.html" method="GET" id="search">
<input type="text" name="q" id="search_box" alt="Search">
</form>
</div>
</div>
<div id="breadcrumbs">
<a href="index.html">Home</a> >
<a href="index-faq.html">FAQs</a> >
perlunifaq
</div>
</div>
<div id="content_body">
<!--[if lt IE 7]>
<div class="noscript">
<p>
<strong>It looks like you're using Internet Explorer 6. This is a very old
browser which does not offer full support for modern websites.</strong>
</p>
<p>
Unfortunately this means that this website will not work on
your computer.
</p>
<p>
Don't miss out though! To view the site (and get a better experience from
many other websites), simply upgrade to
<a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
or download an alternative browser such as
<a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
<a href="http://www.apple.com/safari/download/">Safari</a>, or
<a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
<p>
All of these browsers are free. If you're using a PC at work, you may
need to contact your IT administrator.
</p>
</div>
<![endif]-->
<noscript>
<div class="noscript">
<p>
<strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
or are running a non-JavaScript capable web browser.</strong>
</p>
<p>
To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
</div>
</noscript>
<div id="recent_pages" class="hud_container">
<div id="recent_pages_header" class="hud_header">
<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
<div id="recent_pages_topright" class="hud_topright"></div>
</div>
<div id="recent_pages_content" class="hud_content">
</div>
<div id="recent_pages_footer" class="hud_footer">
<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="recent_pages_resize" class="hud_resize"></div>
</div>
</div>
<div id="from_search"></div>
<h1>perlunifaq</h1>
<!-- -->
<ul><li><a href="#NAME">NAME</a><li><a href="#Q-and-A">Q and A</a><ul><li><a href="#perlunitut-isn't-really-a-Unicode-tutorial%2c-is-it%3f">perlunitut isn't really a Unicode tutorial, is it?</a><li><a href="#What-character-encodings-does-Perl-support%3f">What character encodings does Perl support?</a><li><a href="#Which-version-of-perl-should-I-use%3f">Which version of perl should I use?</a><li><a href="#What-about-binary-data%2c-like-images%3f">What about binary data, like images?</a><li><a href="#When-should-I-decode-or-encode%3f">When should I decode or encode?</a><li><a href="#What-if-I-don't-decode%3f">What if I don't decode?</a><li><a href="#What-if-I-don't-encode%3f">What if I don't encode?</a><li><a href="#Is-there-a-way-to-automatically-decode-or-encode%3f">Is there a way to automatically decode or encode?</a><li><a href="#What-if-I-don't-know-which-encoding-was-used%3f">What if I don't know which encoding was used?</a><li><a href="#Can-I-use-Unicode-in-my-Perl-sources%3f">Can I use Unicode in my Perl sources?</a><li><a href="#Data%3a%3aDumper-doesn't-restore-the-UTF8-flag%3b-is-it-broken%3f">Data::Dumper doesn't restore the UTF8 flag; is it broken?</a><li><a href="#Why-do-regex-character-classes-sometimes-match-only-in-the-ASCII-range%3f">Why do regex character classes sometimes match only in the ASCII range?</a><li><a href="#Why-do-some-characters-not-uppercase-or-lowercase-correctly%3f">Why do some characters not uppercase or lowercase correctly?</a><li><a href="#How-can-I-determine-if-a-string-is-a-text-string-or-a-binary-string%3f">How can I determine if a string is a text string or a binary string?</a><li><a href="#How-do-I-convert-from-encoding-FOO-to-encoding-BAR%3f">How do I convert from encoding FOO to encoding BAR?</a><li><a href="#What-are-decode_utf8-and-encode_utf8%3f">What are decode_utf8 and encode_utf8?</a><li><a href="#What-is-a-%22wide-character%22%3f">What is a "wide character"?</a></ul><li><a href="#INTERNALS">INTERNALS</a><ul><li><a href="#What-is-%22the-UTF8-flag%22%3f">What is "the UTF8 flag"?</a><li><a href="#What-about-the-use-bytes-pragma%3f">What about the use bytes pragma?</a><li><a href="#What-about-the-use-encoding-pragma%3f">What about the use encoding pragma?</a><li><a href="#What-is-the-difference-between-%3aencoding-and-%3autf8%3f">What is the difference between :encoding and :utf8?</a><li><a href="#What's-the-difference-between-UTF-8-and-utf8%3f">What's the difference between UTF-8 and utf8?</a><li><a href="#I-lost-track%3b-what-encoding-is-the-internal-format-really%3f">I lost track; what encoding is the internal format really?</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perlunifaq - Perl Unicode FAQ</p>
<a name="Q-and-A"></a><h1>Q and A</h1>
<p>This is a list of questions and answers about Unicode in Perl, intended to be
read after <a href="perlunitut.html">perlunitut</a>.</p>
<a name="perlunitut-isn't-really-a-Unicode-tutorial%2c-is-it%3f"></a><h2>perlunitut isn't really a Unicode tutorial, is it?</h2>
<p>No, and this isn't really a Unicode FAQ.</p>
<p>Perl has an abstracted interface for all supported character encodings, so this
is actually a generic <code class="inline"><span class="w">Encode</span></code>
tutorial and <code class="inline"><span class="w">Encode</span></code>
FAQ. But many people
think that Unicode is special and magical, and I didn't want to disappoint
them, so I decided to call the document a Unicode tutorial.</p>
<a name="What-character-encodings-does-Perl-support%3f"></a><h2>What character encodings does Perl support?</h2>
<p>To find out which character encodings your Perl supports, run:</p>
<pre class="verbatim"><ol><li> <span class="w">perl</span> -<span class="w">MEncode</span> -le <span class="q">"print for Encode->encodings(':all')"</span></li></ol></pre><a name="Which-version-of-perl-should-I-use%3f"></a><h2>Which version of perl should I use?</h2>
<p>Well, if you can, upgrade to the most recent, but certainly <code class="inline"><span class="v">5.8.1</span></code>
or newer.
The tutorial and FAQ assume the latest release.</p>
<p>You should also check your modules, and upgrade them if necessary. For example,
HTML::Entities requires version >= 1.32 to function correctly, even though the
changelog is silent about this.</p>
<a name="What-about-binary-data%2c-like-images%3f"></a><h2>What about binary data, like images?</h2>
<p>Well, apart from a bare <code class="inline"><a class="l_k" href="functions/binmode.html">binmode</a> <span class="i">$fh</span></code>
, you shouldn't treat them specially.
(The binmode is needed because otherwise Perl may convert line endings on Win32
systems.)</p>
<p>Be careful, though, to never combine text strings with binary strings. If you
need text in a binary stream, encode your text strings first using the
appropriate encoding, then join them with binary strings. See also: "What if I
don't encode?".</p>
<a name="When-should-I-decode-or-encode%3f"></a><h2>When should I decode or encode?</h2>
<p>Whenever you're communicating text with anything that is external to your perl
process, like a database, a text file, a socket, or another program. Even if
the thing you're communicating with is also written in Perl.</p>
<a name="What-if-I-don't-decode%3f"></a><h2>What if I don't decode?</h2>
<p>Whenever your encoded, binary string is used together with a text string, Perl
will assume that your binary string was encoded with ISO-8859-1, also known as
latin-1. If it wasn't latin-1, then your data is unpleasantly converted. For
example, if it was UTF-8, the individual bytes of multibyte characters are seen
as separate characters, and then again converted to UTF-8. Such double encoding
can be compared to double HTML encoding (<code class="inline"><span class="i">&amp</span><span class="sc">;</span>gt<span class="sc">;</span></code>
), or double URI encoding
(<code class="inline"><span class="i">%253E</span></code>
).</p>
<p>This silent implicit decoding is known as "upgrading". That may sound
positive, but it's best to avoid it.</p>
<a name="What-if-I-don't-encode%3f"></a><h2>What if I don't encode?</h2>
<p>Your text string will be sent using the bytes in Perl's internal format. In
some cases, Perl will warn you that you're doing something wrong, with a
friendly warning:</p>
<pre class="verbatim"><ol><li> <span class="w">Wide</span> <span class="w">character</span> <span class="w">in</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">at</span> <span class="w">example</span>.<span class="w">pl</span> <span class="w">line</span> <span class="n">2.</span></li></ol></pre><p>Because the internal format is often UTF-8, these bugs are hard to spot,
because UTF-8 is usually the encoding you wanted! But don't be lazy, and don't
use the fact that Perl's internal format is UTF-8 to your advantage. Encode
explicitly to avoid weird bugs, and to show to maintenance programmers that you
thought this through.</p>
<a name="Is-there-a-way-to-automatically-decode-or-encode%3f"></a><h2>Is there a way to automatically decode or encode?</h2>
<p>If all data that comes from a certain handle is encoded in exactly the same
way, you can tell the PerlIO system to automatically decode everything, with
the <code class="inline"><span class="w">encoding</span></code>
layer. If you do this, you can't accidentally forget to decode
or encode anymore, on things that use the layered handle.</p>
<p>You can provide this layer when <code class="inline"><a class="l_k" href="functions/open.html">open</a></code>ing the file:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'>:encoding(UTF-8)'</span><span class="cm">,</span> <span class="i">$filename</span><span class="sc">;</span> <span class="c"># auto encoding on write</span></li><li> <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">'<:encoding(UTF-8)'</span><span class="cm">,</span> <span class="i">$filename</span><span class="sc">;</span> <span class="c"># auto decoding on read</span></li></ol></pre><p>Or if you already have an open filehandle:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/binmode.html">binmode</a> <span class="i">$fh</span><span class="cm">,</span> <span class="q">':encoding(UTF-8)'</span><span class="sc">;</span></li></ol></pre><p>Some database drivers for DBI can also automatically encode and decode, but
that is sometimes limited to the UTF-8 encoding.</p>
<a name="What-if-I-don't-know-which-encoding-was-used%3f"></a><h2>What if I don't know which encoding was used?</h2>
<p>Do whatever you can to find out, and if you have to: guess. (Don't forget to
document your guess with a comment.)</p>
<p>You could open the document in a web browser, and change the character set or
character encoding until you can visually confirm that all characters look the
way they should.</p>
<p>There is no way to reliably detect the encoding automatically, so if people
keep sending you data without charset indication, you may have to educate them.</p>
<a name="Can-I-use-Unicode-in-my-Perl-sources%3f"></a><h2>Can I use Unicode in my Perl sources?</h2>
<p>Yes, you can! If your sources are UTF-8 encoded, you can indicate that with the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span></code>
pragma.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span><span class="sc">;</span></li></ol></pre><p>This doesn't do anything to your input, or to your output. It only influences
the way your sources are read. You can use Unicode in string literals, in
identifiers (but they still have to be "word characters" according to <code class="inline">\<span class="w">w</span></code>
),
and even in custom delimiters.</p>
<a name="Data%3a%3aDumper-doesn't-restore-the-UTF8-flag%3b-is-it-broken%3f"></a><h2>Data::Dumper doesn't restore the UTF8 flag; is it broken?</h2>
<p>No, Data::Dumper's Unicode abilities are as they should be. There have been
some complaints that it should restore the UTF8 flag when the data is read
again with <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>. However, you should really not look at the flag, and
nothing indicates that Data::Dumper should break this rule.</p>
<p>Here's what happens: when Perl reads in a string literal, it sticks to 8 bit
encoding as long as it can. (But perhaps originally it was internally encoded
as UTF-8, when you dumped it.) When it has to give that up because other
characters are added to the text string, it silently upgrades the string to
UTF-8.</p>
<p>If you properly encode your strings for output, none of this is of your
concern, and you can just <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> dumped data as always.</p>
<a name="Why-do-regex-character-classes-sometimes-match-only-in-the-ASCII-range%3f"></a><h2>Why do regex character classes sometimes match only in the ASCII range?</h2>
<p>Starting in Perl 5.14 (and partially in Perl 5.12), just put a
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">'unicode_strings'</span></code>
near the beginning of your program.
Within its lexical scope you shouldn't have this problem. It also is
automatically enabled under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">':5.12'</span></code>
or <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="v">v5.12</span></code>
or
using <code class="inline">-<span class="w">E</span></code>
on the command line for Perl 5.12 or higher.</p>
<p>The rationale for requiring this is to not break older programs that
rely on the way things worked before Unicode came along. Those older
programs knew only about the ASCII character set, and so may not work
properly for additional characters. When a string is encoded in UTF-8,
Perl assumes that the program is prepared to deal with Unicode, but when
the string isn't, Perl assumes that only ASCII
is wanted, and so those characters that are not ASCII
characters aren't recognized as to what they would be in Unicode.
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">feature</span> <span class="q">'unicode_strings'</span></code>
tells Perl to treat all characters as
Unicode, whether the string is encoded in UTF-8 or not, thus avoiding
the problem.</p>
<p>However, on earlier Perls, or if you pass strings to subroutines outside
the feature's scope, you can force Unicode rules by changing the
encoding to UTF-8 by doing <code class="inline"><span class="i">utf8::upgrade</span><span class="s">(</span><span class="i">$string</span><span class="s">)</span></code>
. This can be used
safely on any string, as it checks and does not change strings that have
already been upgraded.</p>
<p>For a more detailed discussion, see <a href="http://search.cpan.org/perldoc/Unicode::Semantics">Unicode::Semantics</a> on CPAN.</p>
<a name="Why-do-some-characters-not-uppercase-or-lowercase-correctly%3f"></a><h2>Why do some characters not uppercase or lowercase correctly?</h2>
<p>See the answer to the previous question.</p>
<a name="How-can-I-determine-if-a-string-is-a-text-string-or-a-binary-string%3f"></a><h2>How can I determine if a string is a text string or a binary string?</h2>
<p>You can't. Some use the UTF8 flag for this, but that's misuse, and makes well
behaved modules like Data::Dumper look bad. The flag is useless for this
purpose, because it's off when an 8 bit encoding (by default ISO-8859-1) is
used to store the string.</p>
<p>This is something you, the programmer, has to keep track of; sorry. You could
consider adopting a kind of "Hungarian notation" to help with this.</p>
<a name="How-do-I-convert-from-encoding-FOO-to-encoding-BAR%3f"></a><h2>How do I convert from encoding FOO to encoding BAR?</h2>
<p>By first converting the FOO-encoded byte string to a text string, and then the
text string to a BAR-encoded byte string:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$text_string</span> = <span class="i">decode</span><span class="s">(</span><span class="q">'FOO'</span><span class="cm">,</span> <span class="i">$foo_string</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$bar_string</span> = <span class="i">encode</span><span class="s">(</span><span class="q">'BAR'</span><span class="cm">,</span> <span class="i">$text_string</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>or by skipping the text string part, and going directly from one binary
encoding to the other:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(from_to)</span><span class="sc">;</span></li><li> <span class="i">from_to</span><span class="s">(</span><span class="i">$string</span><span class="cm">,</span> <span class="q">'FOO'</span><span class="cm">,</span> <span class="q">'BAR'</span><span class="s">)</span><span class="sc">;</span> <span class="c"># changes contents of $string</span></li></ol></pre><p>or by letting automatic decoding and encoding do all the work:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$foofh</span><span class="cm">,</span> <span class="q">'<:encoding(FOO)'</span><span class="cm">,</span> <span class="q">'example.foo.txt'</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/open.html">open</a> <a class="l_k" href="functions/my.html">my</a> <span class="i">$barfh</span><span class="cm">,</span> <span class="q">'>:encoding(BAR)'</span><span class="cm">,</span> <span class="q">'example.bar.txt'</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="s">{</span> <span class="i">$barfh</span> <span class="s">}</span> <span class="i">$_</span> while <span class="q"><$foofh></span><span class="sc">;</span></li></ol></pre><a name="What-are-decode_utf8-and-encode_utf8%3f"></a><h2>What are <code class="inline"><span class="w">decode_utf8</span></code>
and <code class="inline"><span class="w">encode_utf8</span></code>
?</h2>
<p>These are alternate syntaxes for <code class="inline"><span class="i">decode</span><span class="s">(</span><span class="q">'utf8'</span><span class="cm">,</span> ...<span class="s">)</span></code>
and <code class="inline"><span class="i">encode</span><span class="s">(</span><span class="q">'utf8'</span><span class="cm">,</span>
...<span class="s">)</span></code>
.</p>
<a name="What-is-a-%22wide-character%22%3f"></a><h2>What is a "wide character"?</h2>
<p>This is a term used for characters occupying more than one byte.</p>
<p>The Perl warning "Wide character in ..." is caused by such a character.
With no specified encoding layer, Perl tries to
fit things into a single byte. When it can't, it
emits this warning (if warnings are enabled), and uses UTF-8 encoded data
instead.</p>
<p>To avoid this warning and to avoid having different output encodings in a single
stream, always specify an encoding explicitly, for example with a PerlIO layer:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/binmode.html">binmode</a> <span class="w">STDOUT</span><span class="cm">,</span> <span class="q">":encoding(UTF-8)"</span><span class="sc">;</span></li></ol></pre><a name="INTERNALS"></a><h1>INTERNALS</h1>
<a name="What-is-%22the-UTF8-flag%22%3f"></a><h2>What is "the UTF8 flag"?</h2>
<p>Please, unless you're hacking the internals, or debugging weirdness, don't
think about the UTF8 flag at all. That means that you very probably shouldn't
use <code class="inline"><span class="w">is_utf8</span></code>
, <code class="inline"><span class="w">_utf8_on</span></code>
or <code class="inline"><span class="w">_utf8_off</span></code>
at all.</p>
<p>The UTF8 flag, also called SvUTF8, is an internal flag that indicates that the
current internal representation is UTF-8. Without the flag, it is assumed to be
ISO-8859-1. Perl converts between these automatically. (Actually Perl usually
assumes the representation is ASCII; see <a href="#Why-do-regex-character-classes-sometimes-match-only-in-the-ASCII-range%3f">Why do regex character classes sometimes match only in the ASCII range?</a> above.)</p>
<p>One of Perl's internal formats happens to be UTF-8. Unfortunately, Perl can't
keep a secret, so everyone knows about this. That is the source of much
confusion. It's better to pretend that the internal format is some unknown
encoding, and that you always have to encode and decode explicitly.</p>
<a name="What-about-the-use-bytes-pragma%3f"></a><h2>What about the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">bytes</span></code>
pragma?</h2>
<p>Don't use it. It makes no sense to deal with bytes in a text string, and it
makes no sense to deal with characters in a byte string. Do the proper
conversions (by decoding/encoding), and things will work out well: you get
character counts for decoded data, and byte counts for encoded data.</p>
<p><code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">bytes</span></code>
is usually a failed attempt to do something useful. Just forget
about it.</p>
<a name="What-about-the-use-encoding-pragma%3f"></a><h2>What about the <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">encoding</span></code>
pragma?</h2>
<p>Don't use it. Unfortunately, it assumes that the programmer's environment and
that of the user will use the same encoding. It will use the same encoding for
the source code and for STDIN and STDOUT. When a program is copied to another
machine, the source code does not change, but the STDIO environment might.</p>
<p>If you need non-ASCII characters in your source code, make it a UTF-8 encoded
file and <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">utf8</span></code>
.</p>
<p>If you need to set the encoding for STDIN, STDOUT, and STDERR, for example
based on the user's locale, <code class="inline"><a class="l_k" href="functions/use.html">use</a> <a class="l_k" href="functions/open.html">open</a></code>
.</p>
<a name="What-is-the-difference-between-%3aencoding-and-%3autf8%3f"></a><h2>What is the difference between <code class="inline"><span class="j">:</span><span class="w">encoding</span></code>
and <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
?</h2>
<p>Because UTF-8 is one of Perl's internal formats, you can often just skip the
encoding or decoding step, and manipulate the UTF8 flag directly.</p>
<p>Instead of <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">UTF</span>-<span class="n">8</span><span class="s">)</span></code>
, you can simply use <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
, which skips the
encoding step if the data was already represented as UTF8 internally. This is
widely accepted as good behavior when you're writing, but it can be dangerous
when reading, because it causes internal inconsistency when you have invalid
byte sequences. Using <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
for input can sometimes result in security
breaches, so please use <code class="inline"><span class="j">:</span><span class="i">encoding</span><span class="s">(</span><span class="w">UTF</span>-<span class="n">8</span><span class="s">)</span></code>
instead.</p>
<p>Instead of <code class="inline"><span class="w">decode</span></code>
and <code class="inline"><span class="w">encode</span></code>
, you could use <code class="inline"><span class="w">_utf8_on</span></code>
and <code class="inline"><span class="w">_utf8_off</span></code>
,
but this is considered bad style. Especially <code class="inline"><span class="w">_utf8_on</span></code>
can be dangerous, for
the same reason that <code class="inline"><span class="j">:</span><span class="w">utf8</span></code>
can.</p>
<p>There are some shortcuts for oneliners;
see <a href="perlrun.html#-C-%5bnumber%2flist%5d">-C</a> in <a href="perlrun.html">perlrun</a>.</p>
<a name="What's-the-difference-between-UTF-8-and-utf8%3f"></a><h2>What's the difference between <code class="inline"><span class="w">UTF</span>-<span class="n">8</span></code>
and <code class="inline"><span class="w">utf8</span></code>
?</h2>
<p><code class="inline"><span class="w">UTF</span>-<span class="n">8</span></code>
is the official standard. <code class="inline"><span class="w">utf8</span></code>
is Perl's way of being liberal in
what it accepts. If you have to communicate with things that aren't so liberal,
you may want to consider using <code class="inline"><span class="w">UTF</span>-<span class="n">8</span></code>
. If you have to communicate with things
that are too liberal, you may have to use <code class="inline"><span class="w">utf8</span></code>
. The full explanation is in
<a href="Encode.html">Encode</a>.</p>
<p><code class="inline"><span class="w">UTF</span>-<span class="n">8</span></code>
is internally known as <code class="inline"><span class="w">utf</span>-<span class="n">8</span>-<span class="w">strict</span></code>
. The tutorial uses UTF-8
consistently, even where utf8 is actually used internally, because the
distinction can be hard to make, and is mostly irrelevant.</p>
<p>For example, utf8 can be used for code points that don't exist in Unicode, like
9999999, but if you encode that to UTF-8, you get a substitution character (by
default; see <a href="Encode.html#Handling-Malformed-Data">Handling Malformed Data in Encode</a> for more ways of dealing with
this.)</p>
<p>Okay, if you insist: the "internal format" is utf8, not UTF-8. (When it's not
some other encoding.)</p>
<a name="I-lost-track%3b-what-encoding-is-the-internal-format-really%3f"></a><h2>I lost track; what encoding is the internal format really?</h2>
<p>It's good that you lost track, because you shouldn't depend on the internal
format being any specific encoding. But since you asked: by default, the
internal format is either ISO-8859-1 (latin-1), or utf8, depending on the
history of the string. On EBCDIC platforms, this may be different even.</p>
<p>Perl knows how it stored the string internally, and will use that knowledge
when you <code class="inline"><span class="w">encode</span></code>
. In other words: don't try to find out what the internal
encoding for a certain string is, but instead just encode it into the encoding
that you want.</p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Juerd Waalboer <#####@juerd.nl></p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="perlunicode.html">perlunicode</a>, <a href="perluniintro.html">perluniintro</a>, <a href="Encode.html">Encode</a></p>
<div id="page_index" class="hud_container">
<div id="page_index_header" class="hud_header">
<div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
<div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
<div id="page_index_topright" class="hud_topright"></div>
</div>
<div id="page_index_content" class="hud_content">
<ul><li><a href="#NAME">NAME</a><li><a href="#Q-and-A">Q and A</a><ul><li><a href="#perlunitut-isn't-really-a-Unicode-tutorial%2c-is-it%3f">perlunitut isn't really a Unicode tutorial, is it?</a><li><a href="#What-character-encodings-does-Perl-support%3f">What character encodings does Perl support?</a><li><a href="#Which-version-of-perl-should-I-use%3f">Which version of perl should I use?</a><li><a href="#What-about-binary-data%2c-like-images%3f">What about binary data, like images?</a><li><a href="#When-should-I-decode-or-encode%3f">When should I decode or encode?</a><li><a href="#What-if-I-don't-decode%3f">What if I don't decode?</a><li><a href="#What-if-I-don't-encode%3f">What if I don't encode?</a><li><a href="#Is-there-a-way-to-automatically-decode-or-encode%3f">Is there a way to automatically decode or encode?</a><li><a href="#What-if-I-don't-know-which-encoding-was-used%3f">What if I don't know which encoding was used?</a><li><a href="#Can-I-use-Unicode-in-my-Perl-sources%3f">Can I use Unicode in my Perl sources?</a><li><a href="#Data%3a%3aDumper-doesn't-restore-the-UTF8-flag%3b-is-it-broken%3f">Data::Dumper doesn't restore the UTF8 flag; is it broken?</a><li><a href="#Why-do-regex-character-classes-sometimes-match-only-in-the-ASCII-range%3f">Why do regex character classes sometimes match only in the ASCII range?</a><li><a href="#Why-do-some-characters-not-uppercase-or-lowercase-correctly%3f">Why do some characters not uppercase or lowercase correctly?</a><li><a href="#How-can-I-determine-if-a-string-is-a-text-string-or-a-binary-string%3f">How can I determine if a string is a text string or a binary string?</a><li><a href="#How-do-I-convert-from-encoding-FOO-to-encoding-BAR%3f">How do I convert from encoding FOO to encoding BAR?</a><li><a href="#What-are-decode_utf8-and-encode_utf8%3f">What are decode_utf8 and encode_utf8?</a><li><a href="#What-is-a-%22wide-character%22%3f">What is a "wide character"?</a></ul><li><a href="#INTERNALS">INTERNALS</a><ul><li><a href="#What-is-%22the-UTF8-flag%22%3f">What is "the UTF8 flag"?</a><li><a href="#What-about-the-use-bytes-pragma%3f">What about the use bytes pragma?</a><li><a href="#What-about-the-use-encoding-pragma%3f">What about the use encoding pragma?</a><li><a href="#What-is-the-difference-between-%3aencoding-and-%3autf8%3f">What is the difference between :encoding and :utf8?</a><li><a href="#What's-the-difference-between-UTF-8-and-utf8%3f">What's the difference between UTF-8 and utf8?</a><li><a href="#I-lost-track%3b-what-encoding-is-the-internal-format-really%3f">I lost track; what encoding is the internal format really?</a></ul><li><a href="#AUTHOR">AUTHOR</a><li><a href="#SEE-ALSO">SEE ALSO</a></ul>
</div>
<div id="page_index_footer" class="hud_footer">
<div id="page_index_bottomleft" class="hud_bottomleft"></div>
<div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="page_index_resize" class="hud_resize"></div>
</div>
</div>
</div>
<div id="content_footer">
</div>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div id="footer_content">
<div id="footer_strapline">
perldoc.perl.org - Official documentation for the Perl programming language
</div>
<div id="footer_links">
<div id="address">
<p class="name">Contact details</p>
<p class="address">
Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
</p>
<p class="contact">
Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
</p>
</div>
<ul class="f1">
<li>Manual
<ul class="f2">
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">Changes</a>
</ul>
<li>Reference
<ul class="f2">
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Variables</a>
</ul>
<li>Modules
<ul class="f2">
<li><a href="index-modules-A.html">Modules</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
</ul>
<li>Misc
<ul class="f2">
<li><a href="index-licence.html">License</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platforms</a>
</ul> </ul>
<div class="clear"></div>
</div>
</div>
<div id="footer_end">
</div>
</div>
</div>
<script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
<script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
perldoc.setPath(0);
perldoc.pageName = 'perlunifaq';
perldoc.pageAddress = 'perlunifaq.html';
perldoc.contentPage = 1;
explorePerl.render();
explorePerl.addEvents('explore_anchor');
</script>
</body>
</html>
|