Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "Encode 3" | |
132 | .TH Encode 3 "2002-06-01" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | Encode \- character encodings | |
135 | .SH "SYNOPSIS" | |
136 | .IX Header "SYNOPSIS" | |
137 | .Vb 1 | |
138 | \& use Encode; | |
139 | .Ve | |
140 | .Sh "Table of Contents" | |
141 | .IX Subsection "Table of Contents" | |
142 | Encode consists of a collection of modules whose details are too big | |
143 | to fit in one document. This \s-1POD\s0 itself explains the top-level APIs | |
144 | and general topics at a glance. For other topics and more details, | |
145 | see the PODs below: | |
146 | .PP | |
147 | .Vb 10 | |
148 | \& Name Description | |
149 | \& -------------------------------------------------------- | |
150 | \& Encode::Alias Alias definitions to encodings | |
151 | \& Encode::Encoding Encode Implementation Base Class | |
152 | \& Encode::Supported List of Supported Encodings | |
153 | \& Encode::CN Simplified Chinese Encodings | |
154 | \& Encode::JP Japanese Encodings | |
155 | \& Encode::KR Korean Encodings | |
156 | \& Encode::TW Traditional Chinese Encodings | |
157 | \& -------------------------------------------------------- | |
158 | .Ve | |
159 | .SH "DESCRIPTION" | |
160 | .IX Header "DESCRIPTION" | |
161 | The \f(CW\*(C`Encode\*(C'\fR module provides the interfaces between Perl's strings | |
162 | and the rest of the system. Perl strings are sequences of | |
163 | \&\fBcharacters\fR. | |
164 | .PP | |
165 | The repertoire of characters that Perl can represent is at least that | |
166 | defined by the Unicode Consortium. On most platforms the ordinal | |
167 | values of the characters (as returned by \f(CW\*(C`ord(ch)\*(C'\fR) is the \*(L"Unicode | |
168 | codepoint\*(R" for the character (the exceptions are those platforms where | |
169 | the legacy encoding is some variant of \s-1EBCDIC\s0 rather than a super-set | |
170 | of \s-1ASCII\s0 \- see perlebcdic). | |
171 | .PP | |
172 | Traditionally, computer data has been moved around in 8\-bit chunks | |
173 | often called \*(L"bytes\*(R". These chunks are also known as \*(L"octets\*(R" in | |
174 | networking standards. Perl is widely used to manipulate data of many | |
175 | types \- not only strings of characters representing human or computer | |
176 | languages but also \*(L"binary\*(R" data being the machine's representation of | |
177 | numbers, pixels in an image \- or just about anything. | |
178 | .PP | |
179 | When Perl is processing \*(L"binary data\*(R", the programmer wants Perl to | |
180 | process \*(L"sequences of bytes\*(R". This is not a problem for Perl \- as a | |
181 | byte has 256 possible values, it easily fits in Perl's much larger | |
182 | \&\*(L"logical character\*(R". | |
183 | .Sh "\s-1TERMINOLOGY\s0" | |
184 | .IX Subsection "TERMINOLOGY" | |
185 | .IP "\(bu" 2 | |
186 | \&\fIcharacter\fR: a character in the range 0..(2**32\-1) (or more). | |
187 | (What Perl's strings are made of.) | |
188 | .IP "\(bu" 2 | |
189 | \&\fIbyte\fR: a character in the range 0..255 | |
190 | (A special case of a Perl character.) | |
191 | .IP "\(bu" 2 | |
192 | \&\fIoctet\fR: 8 bits of data, with ordinal values 0..255 | |
193 | (Term for bytes passed to or from a non-Perl context, e.g. a disk file.) | |
194 | .SH "PERL ENCODING API" | |
195 | .IX Header "PERL ENCODING API" | |
196 | .ie n .IP "$octets = encode(\s-1ENCODING\s0, $string [, \s-1CHECK\s0])" 2 | |
197 | .el .IP "$octets = encode(\s-1ENCODING\s0, \f(CW$string\fR [, \s-1CHECK\s0])" 2 | |
198 | .IX Item "$octets = encode(ENCODING, $string [, CHECK])" | |
199 | Encodes a string from Perl's internal form into \fI\s-1ENCODING\s0\fR and returns | |
200 | a sequence of octets. \s-1ENCODING\s0 can be either a canonical name or | |
201 | an alias. For encoding names and aliases, see \*(L"Defining Aliases\*(R". | |
202 | For \s-1CHECK\s0, see \*(L"Handling Malformed Data\*(R". | |
203 | .Sp | |
204 | For example, to convert a string from Perl's internal format to | |
205 | iso\-8859\-1 (also known as Latin1), | |
206 | .Sp | |
207 | .Vb 1 | |
208 | \& $octets = encode("iso-8859-1", $string); | |
209 | .Ve | |
210 | .Sp | |
211 | \&\fB\s-1CAVEAT\s0\fR: When you run \f(CW\*(C`$octets = encode("utf8", $string)\*(C'\fR, then \f(CW$octets\fR | |
212 | \&\fBmay not be equal to\fR \f(CW$string\fR. Though they both contain the same data, the utf8 flag | |
213 | for \f(CW$octets\fR is \fBalways\fR off. When you encode anything, utf8 flag of | |
214 | the result is always off, even when it contains completely valid utf8 | |
215 | string. See \*(L"The \s-1UTF\-8\s0 flag\*(R" below. | |
216 | .Sp | |
217 | encode($valid_encoding, undef) is harmless but warns you for | |
218 | \&\f(CW\*(C`Use of uninitialized value in subroutine entry\*(C'\fR. | |
219 | encode($valid_encoding, '') is harmless and warnless. | |
220 | .ie n .IP "$string = decode(\s-1ENCODING\s0, $octets [, \s-1CHECK\s0])" 2 | |
221 | .el .IP "$string = decode(\s-1ENCODING\s0, \f(CW$octets\fR [, \s-1CHECK\s0])" 2 | |
222 | .IX Item "$string = decode(ENCODING, $octets [, CHECK])" | |
223 | Decodes a sequence of octets assumed to be in \fI\s-1ENCODING\s0\fR into Perl's | |
224 | internal form and returns the resulting string. As in \fIencode()\fR, | |
225 | \&\s-1ENCODING\s0 can be either a canonical name or an alias. For encoding names | |
226 | and aliases, see \*(L"Defining Aliases\*(R". For \s-1CHECK\s0, see | |
227 | \&\*(L"Handling Malformed Data\*(R". | |
228 | .Sp | |
229 | For example, to convert \s-1ISO\-8859\-1\s0 data to a string in Perl's internal format: | |
230 | .Sp | |
231 | .Vb 1 | |
232 | \& $string = decode("iso-8859-1", $octets); | |
233 | .Ve | |
234 | .Sp | |
235 | \&\fB\s-1CAVEAT\s0\fR: When you run \f(CW\*(C`$string = decode("utf8", $octets)\*(C'\fR, then \f(CW$string\fR | |
236 | \&\fBmay not be equal to\fR \f(CW$octets\fR. Though they both contain the same data, | |
237 | the utf8 flag for \f(CW$string\fR is on unless \f(CW$octets\fR entirely consists of | |
238 | \&\s-1ASCII\s0 data (or \s-1EBCDIC\s0 on \s-1EBCDIC\s0 machines). See \*(L"The \s-1UTF\-8\s0 flag\*(R" | |
239 | below. | |
240 | .Sp | |
241 | decode($valid_encoding, undef) is harmless but warns you for | |
242 | \&\f(CW\*(C`Use of uninitialized value in subroutine entry\*(C'\fR. | |
243 | decode($valid_encoding, '') is harmless and warnless. | |
244 | .IP "[$length =] from_to($octets, \s-1FROM_ENC\s0, \s-1TO_ENC\s0 [, \s-1CHECK\s0])" 2 | |
245 | .IX Item "[$length =] from_to($octets, FROM_ENC, TO_ENC [, CHECK])" | |
246 | Converts \fBin-place\fR data between two encodings. The data in \f(CW$octets\fR | |
247 | must be encoded as octets and not as characters in Perl's internal | |
248 | format. For example, to convert \s-1ISO\-8859\-1\s0 data to Microsoft's \s-1CP1250\s0 encoding: | |
249 | .Sp | |
250 | .Vb 1 | |
251 | \& from_to($octets, "iso-8859-1", "cp1250"); | |
252 | .Ve | |
253 | .Sp | |
254 | and to convert it back: | |
255 | .Sp | |
256 | .Vb 1 | |
257 | \& from_to($octets, "cp1250", "iso-8859-1"); | |
258 | .Ve | |
259 | .Sp | |
260 | Note that because the conversion happens in place, the data to be | |
261 | converted cannot be a string constant; it must be a scalar variable. | |
262 | .Sp | |
263 | \&\fIfrom_to()\fR returns the length of the converted string in octets on success, undef | |
264 | otherwise. | |
265 | .Sp | |
266 | \&\fB\s-1CAVEAT\s0\fR: The following operations look the same but are not quite so; | |
267 | .Sp | |
268 | .Vb 2 | |
269 | \& from_to($data, "iso-8859-1", "utf8"); #1 | |
270 | \& $data = decode("iso-8859-1", $data); #2 | |
271 | .Ve | |
272 | .Sp | |
273 | Both #1 and #2 make \f(CW$data\fR consist of a completely valid \s-1UTF\-8\s0 string | |
274 | but only #2 turns utf8 flag on. #1 is equivalent to | |
275 | .Sp | |
276 | .Vb 1 | |
277 | \& $data = encode("utf8", decode("iso-8859-1", $data)); | |
278 | .Ve | |
279 | .Sp | |
280 | See \*(L"The \s-1UTF\-8\s0 flag\*(R" below. | |
281 | .IP "$octets = encode_utf8($string);" 2 | |
282 | .IX Item "$octets = encode_utf8($string);" | |
283 | Equivalent to \f(CW\*(C`$octets = encode("utf8", $string);\*(C'\fR The characters | |
284 | that comprise \f(CW$string\fR are encoded in Perl's internal format and the | |
285 | result is returned as a sequence of octets. All possible | |
286 | characters have a \s-1UTF\-8\s0 representation so this function cannot fail. | |
287 | .IP "$string = decode_utf8($octets [, \s-1CHECK\s0]);" 2 | |
288 | .IX Item "$string = decode_utf8($octets [, CHECK]);" | |
289 | equivalent to \f(CW\*(C`$string = decode("utf8", $octets [, CHECK])\*(C'\fR. | |
290 | The sequence of octets represented by | |
291 | \&\f(CW$octets\fR is decoded from \s-1UTF\-8\s0 into a sequence of logical | |
292 | characters. Not all sequences of octets form valid \s-1UTF\-8\s0 encodings, so | |
293 | it is possible for this call to fail. For \s-1CHECK\s0, see | |
294 | \&\*(L"Handling Malformed Data\*(R". | |
295 | .Sh "Listing available encodings" | |
296 | .IX Subsection "Listing available encodings" | |
297 | .Vb 2 | |
298 | \& use Encode; | |
299 | \& @list = Encode->encodings(); | |
300 | .Ve | |
301 | .PP | |
302 | Returns a list of the canonical names of the available encodings that | |
303 | are loaded. To get a list of all available encodings including the | |
304 | ones that are not loaded yet, say | |
305 | .PP | |
306 | .Vb 1 | |
307 | \& @all_encodings = Encode->encodings(":all"); | |
308 | .Ve | |
309 | .PP | |
310 | Or you can give the name of a specific module. | |
311 | .PP | |
312 | .Vb 1 | |
313 | \& @with_jp = Encode->encodings("Encode::JP"); | |
314 | .Ve | |
315 | .PP | |
316 | When \*(L"::\*(R" is not in the name, \*(L"Encode::\*(R" is assumed. | |
317 | .PP | |
318 | .Vb 1 | |
319 | \& @ebcdic = Encode->encodings("EBCDIC"); | |
320 | .Ve | |
321 | .PP | |
322 | To find out in detail which encodings are supported by this package, | |
323 | see Encode::Supported. | |
324 | .Sh "Defining Aliases" | |
325 | .IX Subsection "Defining Aliases" | |
326 | To add a new alias to a given encoding, use: | |
327 | .PP | |
328 | .Vb 3 | |
329 | \& use Encode; | |
330 | \& use Encode::Alias; | |
331 | \& define_alias(newName => ENCODING); | |
332 | .Ve | |
333 | .PP | |
334 | After that, newName can be used as an alias for \s-1ENCODING\s0. | |
335 | \&\s-1ENCODING\s0 may be either the name of an encoding or an | |
336 | \&\fIencoding object\fR | |
337 | .PP | |
338 | But before you do so, make sure the alias is nonexistent with | |
339 | \&\f(CW\*(C`resolve_alias()\*(C'\fR, which returns the canonical name thereof. | |
340 | i.e. | |
341 | .PP | |
342 | .Vb 3 | |
343 | \& Encode::resolve_alias("latin1") eq "iso-8859-1" # true | |
344 | \& Encode::resolve_alias("iso-8859-12") # false; nonexistent | |
345 | \& Encode::resolve_alias($name) eq $name # true if $name is canonical | |
346 | .Ve | |
347 | .PP | |
348 | \&\fIresolve_alias()\fR does not need \f(CW\*(C`use Encode::Alias\*(C'\fR; it can be | |
349 | exported via \f(CW\*(C`use Encode qw(resolve_alias)\*(C'\fR. | |
350 | .PP | |
351 | See Encode::Alias for details. | |
352 | .SH "Encoding via PerlIO" | |
353 | .IX Header "Encoding via PerlIO" | |
354 | If your perl supports \fIPerlIO\fR (which is the default), you can use a PerlIO layer to decode | |
355 | and encode directly via a filehandle. The following two examples | |
356 | are totally identical in their functionality. | |
357 | .PP | |
358 | .Vb 4 | |
359 | \& # via PerlIO | |
360 | \& open my $in, "<:encoding(shiftjis)", $infile or die; | |
361 | \& open my $out, ">:encoding(euc-jp)", $outfile or die; | |
362 | \& while(<$in>){ print $out $_; } | |
363 | .Ve | |
364 | .PP | |
365 | .Vb 7 | |
366 | \& # via from_to | |
367 | \& open my $in, "<", $infile or die; | |
368 | \& open my $out, ">", $outfile or die; | |
369 | \& while(<$in>){ | |
370 | \& from_to($_, "shiftjis", "euc-jp", 1); | |
371 | \& print $out $_; | |
372 | \& } | |
373 | .Ve | |
374 | .PP | |
375 | Unfortunately, it may be that encodings are PerlIO\-savvy. You can check | |
376 | if your encoding is supported by PerlIO by calling the \f(CW\*(C`perlio_ok\*(C'\fR | |
377 | method. | |
378 | .PP | |
379 | .Vb 2 | |
380 | \& Encode::perlio_ok("hz"); # False | |
381 | \& find_encoding("euc-cn")->perlio_ok; # True where PerlIO is available | |
382 | .Ve | |
383 | .PP | |
384 | .Vb 2 | |
385 | \& use Encode qw(perlio_ok); # exported upon request | |
386 | \& perlio_ok("euc-jp") | |
387 | .Ve | |
388 | .PP | |
389 | Fortunately, all encodings that come with Encode core are PerlIO-savvy | |
390 | except for hz and ISO\-2022\-kr. For gory details, see Encode::Encoding and Encode::PerlIO. | |
391 | .SH "Handling Malformed Data" | |
392 | .IX Header "Handling Malformed Data" | |
393 | .RS 2 | |
394 | The \fI\s-1CHECK\s0\fR argument is used as follows. When you omit it, | |
395 | the behaviour is the same as if you had passed a value of 0 for | |
396 | \&\fI\s-1CHECK\s0\fR. | |
397 | .RE | |
398 | .IP "\fI\s-1CHECK\s0\fR = Encode::FB_DEFAULT ( == 0)" 2 | |
399 | .IX Item "CHECK = Encode::FB_DEFAULT ( == 0)" | |
400 | If \fI\s-1CHECK\s0\fR is 0, (en|de)code will put a \fIsubstitution character\fR | |
401 | in place of a malformed character. For UCM-based encodings, | |
402 | <subchar> will be used. For Unicode, the code point \f(CW0xFFFD\fR is used. | |
403 | If the data is supposed to be \s-1UTF\-8\s0, an optional lexical warning | |
404 | (category utf8) is given. | |
405 | .IP "\fI\s-1CHECK\s0\fR = Encode::FB_CROAK ( == 1)" 2 | |
406 | .IX Item "CHECK = Encode::FB_CROAK ( == 1)" | |
407 | If \fI\s-1CHECK\s0\fR is 1, methods will die on error immediately with an error | |
408 | message. Therefore, when \fI\s-1CHECK\s0\fR is set to 1, you should trap the | |
409 | fatal error with eval{} unless you really want to let it die on error. | |
410 | .IP "\fI\s-1CHECK\s0\fR = Encode::FB_QUIET" 2 | |
411 | .IX Item "CHECK = Encode::FB_QUIET" | |
412 | If \fI\s-1CHECK\s0\fR is set to Encode::FB_QUIET, (en|de)code will immediately | |
413 | return the portion of the data that has been processed so far when | |
414 | an error occurs. The data argument will be overwritten with | |
415 | everything after that point (that is, the unprocessed part of data). | |
416 | This is handy when you have to call decode repeatedly in the case | |
417 | where your source data may contain partial multi-byte character | |
418 | sequences, for example because you are reading with a fixed-width | |
419 | buffer. Here is some sample code that does exactly this: | |
420 | .Sp | |
421 | .Vb 7 | |
422 | \& my $data = ''; my $utf8 = ''; | |
423 | \& while(defined(read $fh, $buffer, 256)){ | |
424 | \& # buffer may end in a partial character so we append | |
425 | \& $data .= $buffer; | |
426 | \& $utf8 .= decode($encoding, $data, Encode::FB_QUIET); | |
427 | \& # $data now contains the unprocessed partial character | |
428 | \& } | |
429 | .Ve | |
430 | .IP "\fI\s-1CHECK\s0\fR = Encode::FB_WARN" 2 | |
431 | .IX Item "CHECK = Encode::FB_WARN" | |
432 | This is the same as above, except that it warns on error. Handy when | |
433 | you are debugging the mode above. | |
434 | .IP "perlqq mode (\fI\s-1CHECK\s0\fR = Encode::FB_PERLQQ)" 2 | |
435 | .IX Item "perlqq mode (CHECK = Encode::FB_PERLQQ)" | |
436 | .PD 0 | |
437 | .IP "\s-1HTML\s0 charref mode (\fI\s-1CHECK\s0\fR = Encode::FB_HTMLCREF)" 2 | |
438 | .IX Item "HTML charref mode (CHECK = Encode::FB_HTMLCREF)" | |
439 | .IP "\s-1XML\s0 charref mode (\fI\s-1CHECK\s0\fR = Encode::FB_XMLCREF)" 2 | |
440 | .IX Item "XML charref mode (CHECK = Encode::FB_XMLCREF)" | |
441 | .PD | |
442 | For encodings that are implemented by Encode::XS, \s-1CHECK\s0 == | |
443 | Encode::FB_PERLQQ turns (en|de)code into \f(CW\*(C`perlqq\*(C'\fR fallback mode. | |
444 | .Sp | |
445 | When you decode, \f(CW\*(C`\ex\f(CI\s-1HH\s0\f(CW\*(C'\fR will be inserted for a malformed character, | |
446 | where \fI\s-1HH\s0\fR is the hex representation of the octet that could not be | |
447 | decoded to utf8. And when you encode, \f(CW\*(C`\ex{\f(CI\s-1HHHH\s0\f(CW}\*(C'\fR will be inserted, | |
448 | where \fI\s-1HHHH\s0\fR is the Unicode \s-1ID\s0 of the character that cannot be found | |
449 | in the character repertoire of the encoding. | |
450 | .Sp | |
451 | \&\s-1HTML/XML\s0 character reference modes are about the same, in place of | |
452 | \&\f(CW\*(C`\ex{\f(CI\s-1HHHH\s0\f(CW}\*(C'\fR, \s-1HTML\s0 uses \f(CW\*(C`&#\f(CI\s-1NNNN\s0\f(CW\*(C'\fR; where \fI\s-1NNNN\s0\fR is a decimal digit and | |
453 | \&\s-1XML\s0 uses \f(CW\*(C`&#x\f(CI\s-1HHHH\s0\f(CW\*(C'\fR; where \fI\s-1HHHH\s0\fR is the hexadecimal digit. | |
454 | .IP "The bitmask" 2 | |
455 | .IX Item "The bitmask" | |
456 | These modes are actually set via a bitmask. Here is how the \s-1FB_XX\s0 | |
457 | constants are laid out. You can import the \s-1FB_XX\s0 constants via | |
458 | \&\f(CW\*(C`use Encode qw(:fallbacks)\*(C'\fR; you can import the generic bitmask | |
459 | constants via \f(CW\*(C`use Encode qw(:fallback_all)\*(C'\fR. | |
460 | .Sp | |
461 | .Vb 8 | |
462 | \& FB_DEFAULT FB_CROAK FB_QUIET FB_WARN FB_PERLQQ | |
463 | \& DIE_ON_ERR 0x0001 X | |
464 | \& WARN_ON_ERR 0x0002 X | |
465 | \& RETURN_ON_ERR 0x0004 X X | |
466 | \& LEAVE_SRC 0x0008 | |
467 | \& PERLQQ 0x0100 X | |
468 | \& HTMLCREF 0x0200 | |
469 | \& XMLCREF 0x0400 | |
470 | .Ve | |
471 | .Sh "Unimplemented fallback schemes" | |
472 | .IX Subsection "Unimplemented fallback schemes" | |
473 | In the future, you will be able to use a code reference to a callback | |
474 | function for the value of \fI\s-1CHECK\s0\fR but its \s-1API\s0 is still undecided. | |
475 | .Sp | |
476 | The fallback scheme does not work on \s-1EBCDIC\s0 platforms. | |
477 | .SH "Defining Encodings" | |
478 | .IX Header "Defining Encodings" | |
479 | To define a new encoding, use: | |
480 | .Sp | |
481 | .Vb 2 | |
482 | \& use Encode qw(define_encoding); | |
483 | \& define_encoding($object, 'canonicalName' [, alias...]); | |
484 | .Ve | |
485 | .Sp | |
486 | \&\fIcanonicalName\fR will be associated with \fI$object\fR. The object | |
487 | should provide the interface described in Encode::Encoding. | |
488 | If more than two arguments are provided then additional | |
489 | arguments are taken as aliases for \fI$object\fR. | |
490 | .Sp | |
491 | See Encode::Encoding for more details. | |
492 | .SH "The UTF\-8 flag" | |
493 | .IX Header "The UTF-8 flag" | |
494 | Before the introduction of utf8 support in perl, The \f(CW\*(C`eq\*(C'\fR operator | |
495 | just compared the strings represented by two scalars. Beginning with | |
496 | perl 5.8, \f(CW\*(C`eq\*(C'\fR compares two strings with simultaneous consideration | |
497 | of \fIthe utf8 flag\fR. To explain why we made it so, I will quote page | |
498 | 402 of \f(CW\*(C`Programming Perl, 3rd ed.\*(C'\fR | |
499 | .RS 2 | |
500 | .IP "Goal #1:" 2 | |
501 | .IX Item "Goal #1:" | |
502 | Old byte-oriented programs should not spontaneously break on the old | |
503 | byte-oriented data they used to work on. | |
504 | .IP "Goal #2:" 2 | |
505 | .IX Item "Goal #2:" | |
506 | Old byte-oriented programs should magically start working on the new | |
507 | character-oriented data when appropriate. | |
508 | .IP "Goal #3:" 2 | |
509 | .IX Item "Goal #3:" | |
510 | Programs should run just as fast in the new character-oriented mode | |
511 | as in the old byte-oriented mode. | |
512 | .IP "Goal #4:" 2 | |
513 | .IX Item "Goal #4:" | |
514 | Perl should remain one language, rather than forking into a | |
515 | byte-oriented Perl and a character-oriented Perl. | |
516 | .RE | |
517 | .RS 2 | |
518 | .Sp | |
519 | Back when \f(CW\*(C`Programming Perl, 3rd ed.\*(C'\fR was written, not even Perl 5.6.0 | |
520 | was born and many features documented in the book remained | |
521 | unimplemented for a long time. Perl 5.8 corrected this and the introduction | |
522 | of the \s-1UTF\-8\s0 flag is one of them. You can think of this perl notion as of a | |
523 | byte-oriented mode (utf8 flag off) and a character-oriented mode (utf8 | |
524 | flag on). | |
525 | .Sp | |
526 | Here is how Encode takes care of the utf8 flag. | |
527 | .IP "\(bu" 2 | |
528 | When you encode, the resulting utf8 flag is always off. | |
529 | .IP "\(bu" 2 | |
530 | When you decode, the resulting utf8 flag is on unless you can | |
531 | unambiguously represent data. Here is the definition of | |
532 | dis\-ambiguity. | |
533 | .Sp | |
534 | After \f(CW\*(C`$utf8 = decode('foo', $octet);\*(C'\fR, | |
535 | .Sp | |
536 | .Vb 6 | |
537 | \& When $octet is... The utf8 flag in $utf8 is | |
538 | \& --------------------------------------------- | |
539 | \& In ASCII only (or EBCDIC only) OFF | |
540 | \& In ISO-8859-1 ON | |
541 | \& In any other Encoding ON | |
542 | \& --------------------------------------------- | |
543 | .Ve | |
544 | .Sp | |
545 | As you see, there is one exception, In \s-1ASCII\s0. That way you can assue | |
546 | Goal #1. And with Encode Goal #2 is assumed but you still have to be | |
547 | careful in such cases mentioned in \fB\s-1CAVEAT\s0\fR paragraphs. | |
548 | .Sp | |
549 | This utf8 flag is not visible in perl scripts, exactly for the same | |
550 | reason you cannot (or you \fIdon't have to\fR) see if a scalar contains a | |
551 | string, integer, or floating point number. But you can still peek | |
552 | and poke these if you will. See the section below. | |
553 | .RE | |
554 | .RS 2 | |
555 | .Sh "Messing with Perl's Internals" | |
556 | .IX Subsection "Messing with Perl's Internals" | |
557 | The following \s-1API\s0 uses parts of Perl's internals in the current | |
558 | implementation. As such, they are efficient but may change. | |
559 | .IP "is_utf8(\s-1STRING\s0 [, \s-1CHECK\s0])" 2 | |
560 | .IX Item "is_utf8(STRING [, CHECK])" | |
561 | [\s-1INTERNAL\s0] Tests whether the \s-1UTF\-8\s0 flag is turned on in the \s-1STRING\s0. | |
562 | If \s-1CHECK\s0 is true, also checks the data in \s-1STRING\s0 for being well-formed | |
563 | \&\s-1UTF\-8\s0. Returns true if successful, false otherwise. | |
564 | .IP "_utf8_on(\s-1STRING\s0)" 2 | |
565 | .IX Item "_utf8_on(STRING)" | |
566 | [\s-1INTERNAL\s0] Turns on the \s-1UTF\-8\s0 flag in \s-1STRING\s0. The data in \s-1STRING\s0 is | |
567 | \&\fBnot\fR checked for being well-formed \s-1UTF\-8\s0. Do not use unless you | |
568 | \&\fBknow\fR that the \s-1STRING\s0 is well-formed \s-1UTF\-8\s0. Returns the previous | |
569 | state of the \s-1UTF\-8\s0 flag (so please don't treat the return value as | |
570 | indicating success or failure), or \f(CW\*(C`undef\*(C'\fR if \s-1STRING\s0 is not a string. | |
571 | .IP "_utf8_off(\s-1STRING\s0)" 2 | |
572 | .IX Item "_utf8_off(STRING)" | |
573 | [\s-1INTERNAL\s0] Turns off the \s-1UTF\-8\s0 flag in \s-1STRING\s0. Do not use frivolously. | |
574 | Returns the previous state of the \s-1UTF\-8\s0 flag (so please don't treat the | |
575 | return value as indicating success or failure), or \f(CW\*(C`undef\*(C'\fR if \s-1STRING\s0 is | |
576 | not a string. | |
577 | .RE | |
578 | .RS 2 | |
579 | .SH "SEE ALSO" | |
580 | .IX Header "SEE ALSO" | |
581 | Encode::Encoding, | |
582 | Encode::Supported, | |
583 | Encode::PerlIO, | |
584 | encoding, | |
585 | perlebcdic, | |
586 | \&\*(L"open\*(R" in perlfunc, | |
587 | perlunicode, | |
588 | utf8, | |
589 | the Perl Unicode Mailing List <perl\-unicode@perl.org> | |
590 | .SH "MAINTAINER" | |
591 | .IX Header "MAINTAINER" | |
592 | This project was originated by Nick Ing-Simmons and later maintained | |
593 | by Dan Kogai <dankogai@dan.co.jp>. See \s-1AUTHORS\s0 for a full | |
594 | list of people involved. For any questions, use | |
595 | <perl\-unicode@perl.org> so we can all share. |