File: Encode.html

package info (click to toggle)
perl-doc-html 5.14.0-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 38,588 kB
  • sloc: xml: 36; makefile: 10
file content (854 lines) | stat: -rw-r--r-- 56,692 bytes parent folder | download
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Encode - 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>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <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>Encode</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 14.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> &bull;
		
                <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> &gt;
    
      
        <a href="index-modules-E.html">Core modules (E)</a> &gt;
      
    
    Encode
  

            </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>Encode</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#SYNOPSIS">SYNOPSIS</a><ul><li><a href="#Table-of-Contents">Table of Contents</a></ul><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#TERMINOLOGY">TERMINOLOGY</a></ul><li><a href="#PERL-ENCODING-API">PERL ENCODING API</a><ul><li><a href="#Listing-available-encodings">Listing available encodings</a><li><a href="#Defining-Aliases">Defining Aliases</a><li><a href="#Finding-IANA-Character-Set-Registry-names">Finding IANA Character Set Registry names</a></ul><li><a href="#Encoding-via-PerlIO">Encoding via PerlIO</a><li><a href="#Handling-Malformed-Data">Handling Malformed Data</a><ul><li><a href="#coderef-for-CHECK">coderef for CHECK</a></ul><li><a href="#Defining-Encodings">Defining Encodings</a><li><a href="#The-UTF8-flag">The UTF8 flag</a><ul><li><a href="#Messing-with-Perl's-Internals">Messing with Perl's Internals</a></ul><li><a href="#UTF-8-vs.-utf8-vs.-UTF8">UTF-8 vs. utf8 vs. UTF8</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#MAINTAINER">MAINTAINER</a><li><a href="#COPYRIGHT">COPYRIGHT</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>Encode - character encodings</p>
<a name="SYNOPSIS"></a><h1>SYNOPSIS</h1>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span><span class="sc">;</span></li></ol></pre><a name="Table-of-Contents"></a><h2>Table of Contents</h2>
<p>Encode consists of a collection of modules whose details are too big
to fit in one document.  This POD itself explains the top-level APIs
and general topics at a glance.  For other topics and more details,
see the PODs below:</p>
<pre class="verbatim"><ol><li>  Name			        Description</li><li>  --------------------------------------------------------</li><li>  Encode::Alias         Alias definitions to encodings</li><li>  Encode::Encoding      Encode Implementation Base Class</li><li>  Encode::Supported     List of Supported Encodings</li><li>  Encode::CN            Simplified Chinese Encodings</li><li>  Encode::JP            Japanese Encodings</li><li>  Encode::KR            Korean Encodings</li><li>  Encode::TW            Traditional Chinese Encodings</li><li>  --------------------------------------------------------</li></ol></pre><a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>The <code class="inline"><span class="w">Encode</span></code>
 module provides the interfaces between Perl's strings
and the rest of the system.  Perl strings are sequences of
<b>characters</b>.</p>
<p>The repertoire of characters that Perl can represent is at least that
defined by the Unicode Consortium. On most platforms the ordinal
values of the characters (as returned by <code class="inline"><a class="l_k" href="functions/ord.html">ord(ch)</a></code>) is the "Unicode
codepoint" for the character (the exceptions are those platforms where
the legacy encoding is some variant of EBCDIC rather than a super-set
of ASCII - see <a href="perlebcdic.html">perlebcdic</a>).</p>
<p>Traditionally, computer data has been moved around in 8-bit chunks
often called "bytes". These chunks are also known as "octets" in
networking standards. Perl is widely used to manipulate data of many
types - not only strings of characters representing human or computer
languages but also "binary" data being the machine's representation of
numbers, pixels in an image - or just about anything.</p>
<p>When Perl is processing "binary data", the programmer wants Perl to
process "sequences of bytes". This is not a problem for Perl - as a
byte has 256 possible values, it easily fits in Perl's much larger
"logical character".</p>
<a name="TERMINOLOGY"></a><h2>TERMINOLOGY</h2>
<ul>
<li>
<p><i>character</i>: a character in the range 0..(2**32-1) (or more).
(What Perl's strings are made of.)</p>
</li>
<li>
<p><i>byte</i>: a character in the range 0..255
(A special case of a Perl character.)</p>
</li>
<li>
<p><i>octet</i>: 8 bits of data, with ordinal values 0..255
(Term for bytes passed to or from a non-Perl context, e.g. a disk file.)</p>
</li>
</ul>
<a name="PERL-ENCODING-API"></a><h1>PERL ENCODING API</h1>
<ul>
<li><a name="%24octets--%3d-encode(ENCODING%2c-%24string-%5b%2c-CHECK%5d)"></a><b>$octets  = encode(ENCODING, $string [, CHECK])</b>
<p>Encodes a string from Perl's internal form into <i>ENCODING</i> and returns
a sequence of octets.  ENCODING can be either a canonical name or
an alias.  For encoding names and aliases, see <a href="#Defining-Aliases">Defining Aliases</a>.
For CHECK, see <a href="#Handling-Malformed-Data">Handling Malformed Data</a>.</p>
<p>For example, to convert a string from Perl's internal format to
iso-8859-1 (also known as Latin1),</p>
<pre class="verbatim"><ol><li>  <span class="i">$octets</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;iso-8859-1&quot;</span><span class="cm">,</span> <span class="i">$string</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><b>CAVEAT</b>: When you run <code class="inline"><span class="i">$octets</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="cm">,</span> <span class="i">$string</span><span class="s">)</span></code>
, then
$octets <b>may not be equal to</b> $string.  Though they both contain the
same data, the UTF8 flag for $octets is <b>always</b> off.  When you
encode anything, UTF8 flag of the result is always off, even when it
contains completely valid utf8 string. See <a href="#The-UTF8-flag">The UTF8 flag</a> below.</p>
<p>If the $string is <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> then <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is returned.</p>
</li>
<li><a name="%24string-%3d-decode(ENCODING%2c-%24octets-%5b%2c-CHECK%5d)"></a><b>$string = decode(ENCODING, $octets [, CHECK])</b>
<p>Decodes a sequence of octets assumed to be in <i>ENCODING</i> into Perl's
internal form and returns the resulting string.  As in encode(),
ENCODING can be either a canonical name or an alias. For encoding names
and aliases, see <a href="#Defining-Aliases">Defining Aliases</a>.  For CHECK, see
<a href="#Handling-Malformed-Data">Handling Malformed Data</a>.</p>
<p>For example, to convert ISO-8859-1 data to a string in Perl's internal format:</p>
<pre class="verbatim"><ol><li>  <span class="i">$string</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&quot;iso-8859-1&quot;</span><span class="cm">,</span> <span class="i">$octets</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><b>CAVEAT</b>: When you run <code class="inline"><span class="i">$string</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="cm">,</span> <span class="i">$octets</span><span class="s">)</span></code>
, then $string
<b>may not be equal to</b> $octets.  Though they both contain the same data,
the UTF8 flag for $string is on unless $octets entirely consists of
ASCII data (or EBCDIC on EBCDIC machines).  See <a href="#The-UTF8-flag">The UTF8 flag</a>
below.</p>
<p>If the $string is <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> then <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is returned.</p>
</li>
<li><a name="%5b%24obj-%3d%5d-find_encoding(ENCODING)"></a><b>[$obj =] find_encoding(ENCODING)</b>
<p>Returns the <i>encoding object</i> corresponding to ENCODING.  Returns
undef if no matching ENCODING is find.</p>
<p>This object is what actually does the actual (en|de)coding.</p>
<pre class="verbatim"><ol><li>  <span class="i">$utf8</span> = <span class="i">decode</span><span class="s">(</span><span class="i">$name</span><span class="cm">,</span> <span class="i">$bytes</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>is in fact</p>
<pre class="verbatim"><ol><li>  <span class="i">$utf8</span> = <a class="l_k" href="functions/do.html">do</a><span class="s">{</span></li><li>    <span class="i">$obj</span> = <span class="i">find_encoding</span><span class="s">(</span><span class="i">$name</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="w">croak</span> <span class="q">qq(encoding &quot;$name&quot; not found)</span> unless <a class="l_k" href="functions/ref.html">ref</a> <span class="i">$obj</span><span class="sc">;</span></li><li>    <span class="i">$obj</span><span class="i">-&gt;decode</span><span class="s">(</span><span class="i">$bytes</span><span class="s">)</span></li><li>  <span class="s">}</span><span class="sc">;</span></li></ol></pre><p>with more error checking.</p>
<p>Therefore you can save time by reusing this object as follows;</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$enc</span> = <span class="i">find_encoding</span><span class="s">(</span><span class="q">&quot;iso-8859-1&quot;</span><span class="s">)</span><span class="sc">;</span></li><li>  while<span class="s">(</span>&lt;&gt;<span class="s">)</span><span class="s">{</span></li><li>     <a class="l_k" href="functions/my.html">my</a> <span class="i">$utf8</span> = <span class="i">$enc</span><span class="i">-&gt;decode</span><span class="s">(</span><span class="i">$_</span><span class="s">)</span><span class="sc">;</span></li><li>     <span class="c"># and do someting with $utf8;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Besides <code class="inline"><span class="i">-&gt;decode</span></code>
 and <code class="inline"><span class="i">-&gt;encode</span></code>
, other methods are
available as well.  For instance, <code class="inline"><span class="i">-&gt; name</span></code>
 returns the canonical
name of the encoding object.</p>
<pre class="verbatim"><ol><li>  <span class="i">find_encoding</span><span class="s">(</span><span class="q">&quot;latin1&quot;</span><span class="s">)</span><span class="i">-&gt;name</span><span class="sc">;</span> <span class="c"># iso-8859-1</span></li></ol></pre><p>See <a href="Encode/Encoding.html">Encode::Encoding</a> for details.</p>
</li>
<li><a name="%5b%24length-%3d%5d-from_to(%24octets%2c-FROM_ENC%2c-TO_ENC-%5b%2c-CHECK%5d)"></a><b>[$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])</b>
<p>Converts <b>in-place</b> data between two encodings. The data in $octets
must be encoded as octets and not as characters in Perl's internal
format. For example, to convert ISO-8859-1 data to Microsoft's CP1250
encoding:</p>
<pre class="verbatim"><ol><li>  <span class="i">from_to</span><span class="s">(</span><span class="i">$octets</span><span class="cm">,</span> <span class="q">&quot;iso-8859-1&quot;</span><span class="cm">,</span> <span class="q">&quot;cp1250&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>and to convert it back:</p>
<pre class="verbatim"><ol><li>  <span class="i">from_to</span><span class="s">(</span><span class="i">$octets</span><span class="cm">,</span> <span class="q">&quot;cp1250&quot;</span><span class="cm">,</span> <span class="q">&quot;iso-8859-1&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Note that because the conversion happens in place, the data to be
converted cannot be a string constant; it must be a scalar variable.</p>
<p>from_to() returns the length of the converted string in octets on
success, <i>undef</i> on error.</p>
<p><b>CAVEAT</b>: The following operations look the same but are not quite so;</p>
<pre class="verbatim"><ol><li>  <span class="i">from_to</span><span class="s">(</span><span class="i">$data</span><span class="cm">,</span> <span class="q">&quot;iso-8859-1&quot;</span><span class="cm">,</span> <span class="q">&quot;utf8&quot;</span><span class="s">)</span><span class="sc">;</span> <span class="c">#1</span></li><li>  <span class="i">$data</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&quot;iso-8859-1&quot;</span><span class="cm">,</span> <span class="i">$data</span><span class="s">)</span><span class="sc">;</span>  <span class="c">#2</span></li></ol></pre><p>Both #1 and #2 make $data consist of a completely valid UTF-8 string
but only #2 turns UTF8 flag on.  #1 is equivalent to</p>
<pre class="verbatim"><ol><li>  <span class="i">$data</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="cm">,</span> <span class="i">decode</span><span class="s">(</span><span class="q">&quot;iso-8859-1&quot;</span><span class="cm">,</span> <span class="i">$data</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>See <a href="#The-UTF8-flag">The UTF8 flag</a> below.</p>
<p>Also note that</p>
<pre class="verbatim"><ol><li>  <span class="i">from_to</span><span class="s">(</span><span class="i">$octets</span><span class="cm">,</span> <span class="i">$from</span><span class="cm">,</span> <span class="i">$to</span><span class="cm">,</span> <span class="i">$check</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>is equivalent to</p>
<pre class="verbatim"><ol><li>  <span class="i">$octets</span> = <span class="i">encode</span><span class="s">(</span><span class="i">$to</span><span class="cm">,</span> <span class="i">decode</span><span class="s">(</span><span class="i">$from</span><span class="cm">,</span> <span class="i">$octets</span><span class="s">)</span><span class="cm">,</span> <span class="i">$check</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Yes, it does not respect the $check during decoding.  It is
deliberately done that way.  If you need minute control, <code class="inline"><span class="w">decode</span></code>

then <code class="inline"><span class="w">encode</span></code>
 as follows;</p>
<pre class="verbatim"><ol><li>  <span class="i">$octets</span> = <span class="i">encode</span><span class="s">(</span><span class="i">$to</span><span class="cm">,</span> <span class="i">decode</span><span class="s">(</span><span class="i">$from</span><span class="cm">,</span> <span class="i">$octets</span><span class="cm">,</span> <span class="i">$check_from</span><span class="s">)</span><span class="cm">,</span> <span class="i">$check_to</span><span class="s">)</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="%24octets-%3d-encode_utf8(%24string)%3b"></a><b>$octets = encode_utf8($string);</b>
<p>Equivalent to <code class="inline"><span class="i">$octets</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="cm">,</span> <span class="i">$string</span><span class="s">)</span><span class="sc">;</span></code>
 The characters
that comprise $string are encoded in Perl's internal format and the
result is returned as a sequence of octets. All possible
characters have a UTF-8 representation so this function cannot fail.</p>
</li>
<li><a name="%24string-%3d-decode_utf8(%24octets-%5b%2c-CHECK%5d)%3b"></a><b>$string = decode_utf8($octets [, CHECK]);</b>
<p>equivalent to <code class="inline"><span class="i">$string</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="cm">,</span> <span class="i">$octets</span> [<span class="cm">,</span> CHECK]<span class="s">)</span></code>
.
The sequence of octets represented by
$octets is decoded from UTF-8 into a sequence of logical
characters. Not all sequences of octets form valid UTF-8 encodings, so
it is possible for this call to fail.  For CHECK, see
<a href="#Handling-Malformed-Data">Handling Malformed Data</a>.</p>
</li>
</ul>
<a name="Listing-available-encodings"></a><h2>Listing available encodings</h2>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span><span class="sc">;</span></li><li>  <span class="i">@list</span> = <span class="w">Encode</span><span class="w">-&gt;encodings</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Returns a list of the canonical names of the available encodings that
are loaded.  To get a list of all available encodings including the
ones that are not loaded yet, say</p>
<pre class="verbatim"><ol><li>  <span class="i">@all_encodings</span> = <span class="w">Encode</span><span class="w">-&gt;encodings</span><span class="s">(</span><span class="q">&quot;:all&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Or you can give the name of a specific module.</p>
<pre class="verbatim"><ol><li>  <span class="i">@with_jp</span> = <span class="w">Encode</span><span class="w">-&gt;encodings</span><span class="s">(</span><span class="q">&quot;Encode::JP&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>When "::" is not in the name, "Encode::" is assumed.</p>
<pre class="verbatim"><ol><li>  <span class="i">@ebcdic</span> = <span class="w">Encode</span><span class="w">-&gt;encodings</span><span class="s">(</span><span class="q">&quot;EBCDIC&quot;</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>To find out in detail which encodings are supported by this package,
see <a href="Encode/Supported.html">Encode::Supported</a>.</p>
<a name="Defining-Aliases"></a><h2>Defining Aliases</h2>
<p>To add a new alias to a given encoding, use:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode::Alias</span><span class="sc">;</span></li><li>  <span class="i">define_alias</span><span class="s">(</span><span class="w">newName</span> <span class="cm">=&gt;</span> <span class="w">ENCODING</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>After that, newName can be used as an alias for ENCODING.
ENCODING may be either the name of an encoding or an
<i>encoding object</i></p>
<p>But before you do so, make sure the alias is nonexistent with
<code class="inline"><span class="i">resolve_alias</span><span class="s">(</span><span class="s">)</span></code>
, which returns the canonical name thereof.
i.e.</p>
<pre class="verbatim"><ol><li>  Encode::resolve_alias("latin1") eq "iso-8859-1" # true</li><li>  Encode::resolve_alias("iso-8859-12")   # false; nonexistent</li><li>  Encode::resolve_alias($name) eq $name  # true if $name is canonical</li></ol></pre><p>resolve_alias() does not need <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Encode::Alias</span></code>
; it can be
exported via <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(resolve_alias)</span></code>
.</p>
<p>See <a href="Encode/Alias.html">Encode::Alias</a> for details.</p>
<a name="Finding-IANA-Character-Set-Registry-names"></a><h2>Finding IANA Character Set Registry names</h2>
<p>The canonical name of a given encoding does not necessarily agree with
IANA IANA Character Set Registry, commonly seen as <code class="inline">Content-Type:
text/plain; charset=<i>whatever</i></code>.  For most cases canonical names
work but sometimes it does not (notably 'utf-8-strict').</p>
<p>Therefore as of Encode version 2.21, a new method <code class="inline"><span class="i">mime_name</span><span class="s">(</span><span class="s">)</span></code>
 is added.</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$enc</span> = <span class="i">find_encoding</span><span class="s">(</span><span class="q">'UTF-8'</span><span class="s">)</span><span class="sc">;</span></li><li>  <a class="l_k" href="functions/warn.html">warn</a> <span class="i">$enc</span><span class="i">-&gt;name</span><span class="sc">;</span>      <span class="c"># utf-8-strict</span></li><li>  <a class="l_k" href="functions/warn.html">warn</a> <span class="i">$enc</span><span class="i">-&gt;mime_name</span><span class="sc">;</span> <span class="c"># UTF-8</span></li></ol></pre><p>See also:  <a href="Encode/Encoding.html">Encode::Encoding</a></p>
<a name="Encoding-via-PerlIO"></a><h1>Encoding via PerlIO</h1>
<p>If your perl supports <i>PerlIO</i> (which is the default), you can use a
PerlIO layer to decode and encode directly via a filehandle.  The
following two examples are totally identical in their functionality.</p>
<pre class="verbatim"><ol><li>  <span class="c"># via PerlIO</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">$in</span><span class="cm">,</span>  <span class="q">&quot;&lt;:encoding(shiftjis)&quot;</span><span class="cm">,</span> <span class="i">$infile</span>  or <a class="l_k" href="functions/die.html">die</a><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">$out</span><span class="cm">,</span> <span class="q">&quot;&gt;:encoding(euc-jp)&quot;</span><span class="cm">,</span>   <span class="i">$outfile</span> or <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li><li>  while<span class="s">(</span><span class="q">&lt;$in&gt;</span><span class="s">)</span><span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="i">$out</span> <span class="i">$_</span><span class="sc">;</span> <span class="s">}</span></li><li></li><li>  <span class="c"># via from_to</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">$in</span><span class="cm">,</span>  <span class="q">&quot;&lt;&quot;</span><span class="cm">,</span> <span class="i">$infile</span>  or <a class="l_k" href="functions/die.html">die</a><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">$out</span><span class="cm">,</span> <span class="q">&quot;&gt;&quot;</span><span class="cm">,</span> <span class="i">$outfile</span> or <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li><li>  while<span class="s">(</span><span class="q">&lt;$in&gt;</span><span class="s">)</span><span class="s">{</span></li><li>    <span class="i">from_to</span><span class="s">(</span><span class="i">$_</span><span class="cm">,</span> <span class="q">&quot;shiftjis&quot;</span><span class="cm">,</span> <span class="q">&quot;euc-jp&quot;</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li>    <a class="l_k" href="functions/print.html">print</a> <span class="i">$out</span> <span class="i">$_</span><span class="sc">;</span></li><li>  <span class="s">}</span></li></ol></pre><p>Unfortunately, it may be that encodings are PerlIO-savvy.  You can check
if your encoding is supported by PerlIO by calling the <code class="inline"><span class="w">perlio_ok</span></code>

method.</p>
<pre class="verbatim"><ol><li>  <span class="i">Encode::perlio_ok</span><span class="s">(</span><span class="q">&quot;hz&quot;</span><span class="s">)</span><span class="sc">;</span>             <span class="c"># False</span></li><li>  <span class="i">find_encoding</span><span class="s">(</span><span class="q">&quot;euc-cn&quot;</span><span class="s">)</span><span class="i">-&gt;perlio_ok</span><span class="sc">;</span>  <span class="c"># True where PerlIO is available</span></li><li></li><li>  <a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(perlio_ok)</span><span class="sc">;</span>            <span class="c"># exported upon request</span></li><li>  <span class="i">perlio_ok</span><span class="s">(</span><span class="q">&quot;euc-jp&quot;</span><span class="s">)</span></li></ol></pre><p>Fortunately, all encodings that come with Encode core are PerlIO-savvy
except for hz and ISO-2022-kr.  For gory details, see
<a href="Encode/Encoding.html">Encode::Encoding</a> and <a href="Encode/PerlIO.html">Encode::PerlIO</a>.</p>
<a name="Handling-Malformed-Data"></a><h1>Handling Malformed Data</h1>
<p>The optional <i>CHECK</i> argument tells Encode what to do when it
encounters malformed data.  Without CHECK, Encode::FB_DEFAULT ( == 0 )
is assumed.</p>
<p>As of version 2.12 Encode supports coderef values for CHECK.  See below.</p>
<ul>
<li><a name="*NOTE%3a*-Not-all-encoding-support-this-feature"></a><b><b>NOTE:</b> Not all encoding support this feature</b>
<p>Some encodings ignore <i>CHECK</i> argument.  For example,
<a href="Encode/Unicode.html">Encode::Unicode</a> ignores <i>CHECK</i> and it always croaks on error.</p>
</li>
</ul>
<p>Now here is the list of <i>CHECK</i> values available</p>
<ul>
<li><a name="_CHECK_-%3d-Encode%3a%3aFB_DEFAULT-(-%3d%3d-0)"></a><b><i>CHECK</i> = Encode::FB_DEFAULT ( == 0)</b>
<p>If <i>CHECK</i> is 0, (en|de)code will put a <i>substitution character</i> in
place of a malformed character.  When you encode, &lt;subchar&gt;
will be used.  When you decode the code point <code class="inline"><span class="n">0xFFFD</span></code>
 is used.  If
the data is supposed to be UTF-8, an optional lexical warning
(category utf8) is given.</p>
</li>
<li><a name="_CHECK_-%3d-Encode%3a%3aFB_CROAK-(-%3d%3d-1)"></a><b><i>CHECK</i> = Encode::FB_CROAK ( == 1)</b>
<p>If <i>CHECK</i> is 1, methods will die on error immediately with an error
message.  Therefore, when <i>CHECK</i> is set to 1,  you should trap the
error with eval{} unless you really want to let it die.</p>
</li>
<li><a name="_CHECK_-%3d-Encode%3a%3aFB_QUIET"></a><b><i>CHECK</i> = Encode::FB_QUIET</b>
<p>If <i>CHECK</i> is set to Encode::FB_QUIET, (en|de)code will immediately
return the portion of the data that has been processed so far when an
error occurs. The data argument will be overwritten with everything
after that point (that is, the unprocessed part of data).  This is
handy when you have to call decode repeatedly in the case where your
source data may contain partial multi-byte character sequences,
(i.e. you are reading with a fixed-width buffer). Here is a sample
code that does exactly this:</p>
<pre class="verbatim"><ol><li>  <a class="l_k" href="functions/my.html">my</a> <span class="i">$buffer</span> = <span class="q">&#39;&#39;</span><span class="sc">;</span> <a class="l_k" href="functions/my.html">my</a> <span class="i">$string</span> = <span class="q">&#39;&#39;</span><span class="sc">;</span></li><li>  while<span class="s">(</span><a class="l_k" href="functions/read.html">read</a> <span class="i">$fh</span><span class="cm">,</span> <span class="i">$buffer</span><span class="cm">,</span> <span class="n">256</span><span class="cm">,</span> <a class="l_k" href="functions/length.html">length</a><span class="s">(</span><span class="i">$buffer</span><span class="s">)</span><span class="s">)</span><span class="s">{</span></li><li>    <span class="i">$string</span> .= <span class="i">decode</span><span class="s">(</span><span class="i">$encoding</span><span class="cm">,</span> <span class="i">$buffer</span><span class="cm">,</span> <span class="w">Encode::FB_QUIET</span><span class="s">)</span><span class="sc">;</span></li><li>    <span class="c"># $buffer now contains the unprocessed partial character</span></li><li>  <span class="s">}</span></li></ol></pre></li>
<li><a name="_CHECK_-%3d-Encode%3a%3aFB_WARN"></a><b><i>CHECK</i> = Encode::FB_WARN</b>
<p>This is the same as above, except that it warns on error.  Handy when
you are debugging the mode above.</p>
</li>
<li><a name="perlqq-mode-(_CHECK_-%3d-Encode%3a%3aFB_PERLQQ)"></a><b>perlqq mode (<i>CHECK</i> = Encode::FB_PERLQQ)</b>
</li>
<li><a name="HTML-charref-mode-(_CHECK_-%3d-Encode%3a%3aFB_HTMLCREF)"></a><b>HTML charref mode (<i>CHECK</i> = Encode::FB_HTMLCREF)</b>
</li>
<li><a name="XML-charref-mode-(_CHECK_-%3d-Encode%3a%3aFB_XMLCREF)"></a><b>XML charref mode (<i>CHECK</i> = Encode::FB_XMLCREF)</b>
<p>For encodings that are implemented by Encode::XS, CHECK ==
Encode::FB_PERLQQ turns (en|de)code into <code class="inline"><span class="w">perlqq</span></code>
 fallback mode.</p>
<p>When you decode, <code class="inline">\x<i>HH</i></code> will be inserted for a malformed character,
where <i>HH</i> is the hex representation of the octet  that could not be
decoded to utf8.  And when you encode, <code class="inline">\x{<i>HHHH</i>}</code> will be inserted,
where <i>HHHH</i> is the Unicode ID of the character that cannot be found
in the character repertoire of the encoding.</p>
<p>HTML/XML character reference modes are about the same, in place of
<code class="inline">\x{<i>HHHH</i>}</code>, HTML uses <code class="inline">&amp;#<i>NNN</i>;</code> where <i>NNN</i> is a decimal number and
XML uses <code class="inline">&amp;#x<i>HHHH</i>;</code> where <i>HHHH</i> is the hexadecimal number.</p>
<p>In Encode 2.10 or later, <code class="inline"><span class="w">LEAVE_SRC</span></code>
 is also implied.</p>
</li>
<li><a name="The-bitmask"></a><b>The bitmask</b>
<p>These modes are actually set via a bitmask.  Here is how the FB_XX
constants are laid out.  You can import the FB_XX constants via
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(:fallbacks)</span></code>
; you can import the generic bitmask
constants via <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">Encode</span> <span class="q">qw(:fallback_all)</span></code>
.</p>
<pre class="verbatim"><ol><li>                     FB_DEFAULT FB_CROAK FB_QUIET FB_WARN  FB_PERLQQ</li><li> DIE_ON_ERR    0x0001             X</li><li> WARN_ON_ERR   0x0002                               X</li><li> RETURN_ON_ERR 0x0004                      X        X</li><li> LEAVE_SRC     0x0008                                        X</li><li> PERLQQ        0x0100                                        X</li><li> HTMLCREF      0x0200</li><li> XMLCREF       0x0400</li></ol></pre></li>
</ul>
<ul>
<li><a name="Encode%3a%3aLEAVE_SRC"></a><b>Encode::LEAVE_SRC</b>
<p>If the <code class="inline"><span class="w">Encode::LEAVE_SRC</span></code>
 bit is not set, but <i>CHECK</i> is, then the second
argument to <code class="inline"><span class="i">encode</span><span class="s">(</span><span class="s">)</span></code>
 or <code class="inline"><span class="i">decode</span><span class="s">(</span><span class="s">)</span></code>
 may be assigned to by the functions. If
you're not interested in this, then bitwise-or the bitmask with it.</p>
</li>
</ul>
<a name="coderef-for-CHECK"></a><h2>coderef for CHECK</h2>
<p>As of Encode 2.12 CHECK can also be a code reference which takes the
ord value of unmapped caharacter as an argument and returns a string
that represents the fallback character.  For instance,</p>
<pre class="verbatim"><ol><li>  <span class="i">$ascii</span> = <span class="i">encode</span><span class="s">(</span><span class="q">&quot;ascii&quot;</span><span class="cm">,</span> <span class="i">$utf8</span><span class="cm">,</span> <a class="l_k" href="functions/sub.html">sub</a><span class="s">{</span> <a class="l_k" href="functions/sprintf.html">sprintf</a> <span class="q">&quot;&lt;U+%04X&gt;&quot;</span><span class="cm">,</span> <a class="l_k" href="functions/shift.html">shift</a> <span class="s">}</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Acts like FB_PERLQQ but &lt;U+<i>XXXX</i>&gt; is used instead of
\x{<i>XXXX</i>}.</p>
<a name="Defining-Encodings"></a><h1>Defining Encodings</h1>
<p>To define a new encoding, use:</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(define_encoding)</span><span class="sc">;</span></li><li>    <span class="i">define_encoding</span><span class="s">(</span><span class="i">$object</span><span class="cm">,</span> <span class="q">&#39;canonicalName&#39;</span> <span class="s">[</span><span class="cm">,</span> <span class="w">alias</span>...<span class="s">]</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p><i>canonicalName</i> will be associated with <i>$object</i>.  The object
should provide the interface described in <a href="Encode/Encoding.html">Encode::Encoding</a>.
If more than two arguments are provided then additional
arguments are taken as aliases for <i>$object</i>.</p>
<p>See <a href="Encode/Encoding.html">Encode::Encoding</a> for more details.</p>
<a name="The-UTF8-flag"></a><h1>The UTF8 flag</h1>
<p>Before the introduction of Unicode support in perl, The <code class="inline">eq</code>
 operator
just compared the strings represented by two scalars. Beginning with
perl 5.8, <code class="inline">eq</code>
 compares two strings with simultaneous consideration of
<i>the UTF8 flag</i>. To explain why we made it so, I will quote page 402 of
<code class="inline">Programming Perl, 3rd ed.</code></p>
<ul>
<li><a name="Goal-%231%3a"></a><b>Goal #1:</b>
<p>Old byte-oriented programs should not spontaneously break on the old
byte-oriented data they used to work on.</p>
</li>
<li><a name="Goal-%232%3a"></a><b>Goal #2:</b>
<p>Old byte-oriented programs should magically start working on the new
character-oriented data when appropriate.</p>
</li>
<li><a name="Goal-%233%3a"></a><b>Goal #3:</b>
<p>Programs should run just as fast in the new character-oriented mode
as in the old byte-oriented mode.</p>
</li>
<li><a name="Goal-%234%3a"></a><b>Goal #4:</b>
<p>Perl should remain one language, rather than forking into a
byte-oriented Perl and a character-oriented Perl.</p>
</li>
</ul>
<p>Back when <code class="inline">Programming Perl, 3rd ed.</code> was written, not even Perl 5.6.0
was born and many features documented in the book remained
unimplemented for a long time.  Perl 5.8 corrected this and the introduction
of the UTF8 flag is one of them.  You can think of this perl notion as of a
byte-oriented mode (UTF8 flag off) and a character-oriented mode (UTF8
flag on).</p>
<p>Here is how Encode takes care of the UTF8 flag.</p>
<ul>
<li>
<p>When you encode, the resulting UTF8 flag is always off.</p>
</li>
<li>
<p>When you decode, the resulting UTF8 flag is on unless you can
unambiguously represent data.  Here is the definition of
dis-ambiguity.</p>
<p>After <code class="inline"><span class="i">$utf8</span> = <span class="i">decode</span><span class="s">(</span><span class="q">&#39;foo&#39;</span><span class="cm">,</span> <span class="i">$octet</span><span class="s">)</span><span class="sc">;</span></code>
,</p>
<pre class="verbatim"><ol><li>  When $octet is...   The UTF8 flag in $utf8 is</li><li>  ---------------------------------------------</li><li>  In ASCII only (or EBCDIC only)            OFF</li><li>  In ISO-8859-1                              ON</li><li>  In any other Encoding                      ON</li><li>  ---------------------------------------------</li></ol></pre><p>As you see, there is one exception, In ASCII.  That way you can assume
Goal #1.  And with Encode Goal #2 is assumed but you still have to be
careful in such cases mentioned in <b>CAVEAT</b> paragraphs.</p>
<p>This UTF8 flag is not visible in perl scripts, exactly for the same
reason you cannot (or you <i>don't have to</i>) see if a scalar contains a
string, integer, or floating point number.   But you can still peek
and poke these if you will.  See the section below.</p>
</li>
</ul>
<a name="Messing-with-Perl's-Internals"></a><h2>Messing with Perl's Internals</h2>
<p>The following API uses parts of Perl's internals in the current
implementation.  As such, they are efficient but may change.</p>
<ul>
<li><a name="is_utf8(STRING-%5b%2c-CHECK%5d)"></a><b>is_utf8(STRING [, CHECK])</b>
<p>[INTERNAL] Tests whether the UTF8 flag is turned on in the STRING.
If CHECK is true, also checks the data in STRING for being well-formed
UTF-8.  Returns true if successful, false otherwise.</p>
<p>As of perl 5.8.1, <a href="utf8.html">utf8</a> also has utf8::is_utf8().</p>
</li>
<li><a name="_utf8_on(STRING)"></a><b>_utf8_on(STRING)</b>
<p>[INTERNAL] Turns on the UTF8 flag in STRING.  The data in STRING is
<b>not</b> checked for being well-formed UTF-8.  Do not use unless you
<b>know</b> that the STRING is well-formed UTF-8.  Returns the previous
state of the UTF8 flag (so please don't treat the return value as
indicating success or failure), or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> if STRING is not a string.</p>
<p>This function does not work on tainted values.</p>
</li>
<li><a name="_utf8_off(STRING)"></a><b>_utf8_off(STRING)</b>
<p>[INTERNAL] Turns off the UTF8 flag in STRING.  Do not use frivolously.
Returns the previous state of the UTF8 flag (so please don't treat the
return value as indicating success or failure), or <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> if STRING is
not a string.</p>
<p>This function does not work on tainted values.</p>
</li>
</ul>
<a name="UTF-8-vs.-utf8-vs.-UTF8"></a><h1>UTF-8 vs. utf8 vs. UTF8</h1>
<pre class="verbatim"><ol><li>  ....We now view strings not as sequences of bytes, but as sequences</li><li>  of numbers in the range 0 .. 2**32-1 (or in the case of 64-bit</li><li>  computers, 0 .. 2**64-1) -- Programming Perl, 3rd ed.</li></ol></pre><p>That has been the perl's notion of UTF-8 but official UTF-8 is more
strict; Its ranges is much narrower (0 .. 10FFFF), some sequences are
not allowed (i.e. Those used in the surrogate pair, 0xFFFE, et al).</p>
<p>Now that is overruled by Larry Wall himself.</p>
<pre class="verbatim"><ol><li>  From: Larry Wall &lt;larry@wall.org&gt;</li><li>  Date: December 04, 2004 11:51:58 JST</li><li>  To: perl-unicode@perl.org</li><li>  Subject: Re: Make Encode.pm support the real UTF-8</li><li>  Message-Id: &lt;20041204025158.GA28754@wall.org&gt;</li><li>  </li><li>  On Fri, Dec 03, 2004 at 10:12:12PM +0000, Tim Bunce wrote:</li><li>  : I've no problem with 'utf8' being perl's unrestricted uft8 encoding,</li><li>  : but "UTF-8" is the name of the standard and should give the</li><li>  : corresponding behaviour.</li><li>  </li><li>  For what it's worth, that's how I've always kept them straight in my</li><li>  head.</li><li>  </li><li>  Also for what it's worth, Perl 6 will mostly default to strict but</li><li>  make it easy to switch back to lax.</li><li>  </li><li>  Larry</li></ol></pre><p>Do you copy?  As of Perl 5.8.7, <b>UTF-8</b> means strict, official UTF-8
while <b>utf8</b> means liberal, lax, version thereof.  And Encode version
2.10 or later thus groks the difference between <code class="inline"><span class="w">UTF</span>-<span class="n">8</span></code>
 and C"utf8".</p>
<pre class="verbatim"><ol><li>  <span class="i">encode</span><span class="s">(</span><span class="q">&quot;utf8&quot;</span><span class="cm">,</span>  <span class="q">&quot;\x{FFFF_FFFF}&quot;</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span> <span class="c"># okay</span></li><li>  <span class="i">encode</span><span class="s">(</span><span class="q">&quot;UTF-8&quot;</span><span class="cm">,</span> <span class="q">&quot;\x{FFFF_FFFF}&quot;</span><span class="cm">,</span> <span class="n">1</span><span class="s">)</span><span class="sc">;</span> <span class="c"># croaks</span></li></ol></pre><p><code class="inline"><span class="w">UTF</span>-<span class="n">8</span></code>
 in Encode is actually a canonical name for <code class="inline"><span class="w">utf</span>-<span class="n">8</span>-<span class="w">strict</span></code>
.
Yes, the hyphen between "UTF" and "8" is important.  Without it Encode
goes "liberal"</p>
<pre class="verbatim"><ol><li>  find_encoding("UTF-8")-&gt;name # is 'utf-8-strict'</li><li>  find_encoding("utf-8")-&gt;name # ditto. names are case insensitive</li><li>  find_encoding("utf_8")-&gt;name  # ditto. "_" are treated as "-"</li><li>  find_encoding("UTF8")-&gt;name  # is 'utf8'.</li></ol></pre><p>The UTF8 flag is internally called UTF8, without a hyphen. It indicates
whether a string is internally encoded as utf8, also without a hypen.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="Encode/Encoding.html">Encode::Encoding</a>,
<a href="Encode/Supported.html">Encode::Supported</a>,
<a href="Encode/PerlIO.html">Encode::PerlIO</a>,
<a href="encoding.html">encoding</a>,
<a href="perlebcdic.html">perlebcdic</a>,
<a href="functions/open.html">open</a>,
<a href="perlunicode.html">perlunicode</a>, <a href="perluniintro.html">perluniintro</a>, <a href="perlunifaq.html">perlunifaq</a>, <a href="perlunitut.html">perlunitut</a>
<a href="utf8.html">utf8</a>,
the Perl Unicode Mailing List &lt;perl-unicode@perl.org&gt;</p>
<a name="MAINTAINER"></a><h1>MAINTAINER</h1>
<p>This project was originated by Nick Ing-Simmons and later maintained
by Dan Kogai &lt;dankogai@dan.co.jp&gt;.  See AUTHORS for a full
list of people involved.  For any questions, use
&lt;perl-unicode@perl.org&gt; so we can all share.</p>
<p>While Dan Kogai retains the copyright as a maintainer, the credit
should go to all those involoved.  See AUTHORS for those submitted
codes.</p>
<a name="COPYRIGHT"></a><h1>COPYRIGHT</h1>
<p>Copyright 2002-2006 Dan Kogai &lt;dankogai@dan.co.jp&gt;</p>
<p>This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.</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="#SYNOPSIS">SYNOPSIS</a><ul><li><a href="#Table-of-Contents">Table of Contents</a></ul><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#TERMINOLOGY">TERMINOLOGY</a></ul><li><a href="#PERL-ENCODING-API">PERL ENCODING API</a><ul><li><a href="#Listing-available-encodings">Listing available encodings</a><li><a href="#Defining-Aliases">Defining Aliases</a><li><a href="#Finding-IANA-Character-Set-Registry-names">Finding IANA Character Set Registry names</a></ul><li><a href="#Encoding-via-PerlIO">Encoding via PerlIO</a><li><a href="#Handling-Malformed-Data">Handling Malformed Data</a><ul><li><a href="#coderef-for-CHECK">coderef for CHECK</a></ul><li><a href="#Defining-Encodings">Defining Encodings</a><li><a href="#The-UTF8-flag">The UTF8 flag</a><ul><li><a href="#Messing-with-Perl's-Internals">Messing with Perl's Internals</a></ul><li><a href="#UTF-8-vs.-utf8-vs.-UTF8">UTF-8 vs. utf8 vs. UTF8</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#MAINTAINER">MAINTAINER</a><li><a href="#COPYRIGHT">COPYRIGHT</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>


	    &nbsp;
          </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="http://perl.jonallen.info">Jon Allen (JJ)</a><br>
	      See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for more details
	    </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    = 'Encode';
  perldoc.pageAddress = 'Encode.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>