Commit | Line | Data |
393fec97 |
1 | =head1 NAME |
2 | |
3 | perlunicode - Unicode support in Perl |
4 | |
5 | =head1 DESCRIPTION |
6 | |
0a1f2d14 |
7 | =head2 Important Caveats |
21bad921 |
8 | |
c349b1b9 |
9 | Unicode support is an extensive requirement. While perl does not |
10 | implement the Unicode standard or the accompanying technical reports |
11 | from cover to cover, Perl does support many Unicode features. |
21bad921 |
12 | |
13a2d996 |
13 | =over 4 |
21bad921 |
14 | |
15 | =item Input and Output Disciplines |
16 | |
75daf61c |
17 | A filehandle can be marked as containing perl's internal Unicode |
18 | encoding (UTF-8 or UTF-EBCDIC) by opening it with the ":utf8" layer. |
0a1f2d14 |
19 | Other encodings can be converted to perl's encoding on input, or from |
c349b1b9 |
20 | perl's encoding on output by use of the ":encoding(...)" layer. |
21 | See L<open>. |
22 | |
d1be9408 |
23 | To mark the Perl source itself as being in a particular encoding, |
c349b1b9 |
24 | see L<encoding>. |
21bad921 |
25 | |
26 | =item Regular Expressions |
27 | |
c349b1b9 |
28 | The regular expression compiler produces polymorphic opcodes. That is, |
29 | the pattern adapts to the data and automatically switch to the Unicode |
30 | character scheme when presented with Unicode data, or a traditional |
31 | byte scheme when presented with byte data. |
21bad921 |
32 | |
ad0029c4 |
33 | =item C<use utf8> still needed to enable UTF-8/UTF-EBCDIC in scripts |
21bad921 |
34 | |
75daf61c |
35 | The C<utf8> pragma implements the tables used for Unicode support. |
c349b1b9 |
36 | However, these tables are automatically loaded on demand, so the |
37 | C<utf8> pragma should not normally be used. |
21bad921 |
38 | |
c349b1b9 |
39 | As a compatibility measure, this pragma must be explicitly used to |
40 | enable recognition of UTF-8 in the Perl scripts themselves on ASCII |
41 | based machines or recognize UTF-EBCDIC on EBCDIC based machines. |
42 | B<NOTE: this should be the only place where an explicit C<use utf8> |
43 | is needed>. |
21bad921 |
44 | |
1768d7eb |
45 | You can also use the C<encoding> pragma to change the default encoding |
6ec9efec |
46 | of the data in your script; see L<encoding>. |
1768d7eb |
47 | |
21bad921 |
48 | =back |
49 | |
50 | =head2 Byte and Character semantics |
393fec97 |
51 | |
52 | Beginning with version 5.6, Perl uses logically wide characters to |
53 | represent strings internally. This internal representation of strings |
b3419ed8 |
54 | uses either the UTF-8 or the UTF-EBCDIC encoding. |
393fec97 |
55 | |
75daf61c |
56 | In future, Perl-level operations can be expected to work with |
57 | characters rather than bytes, in general. |
393fec97 |
58 | |
75daf61c |
59 | However, as strictly an interim compatibility measure, Perl aims to |
60 | provide a safe migration path from byte semantics to character |
61 | semantics for programs. For operations where Perl can unambiguously |
62 | decide that the input data is characters, Perl now switches to |
63 | character semantics. For operations where this determination cannot |
64 | be made without additional information from the user, Perl decides in |
65 | favor of compatibility, and chooses to use byte semantics. |
8cbd9a7a |
66 | |
67 | This behavior preserves compatibility with earlier versions of Perl, |
68 | which allowed byte semantics in Perl operations, but only as long as |
69 | none of the program's inputs are marked as being as source of Unicode |
70 | character data. Such data may come from filehandles, from calls to |
71 | external programs, from information provided by the system (such as %ENV), |
21bad921 |
72 | or from literals and constants in the source text. |
8cbd9a7a |
73 | |
c349b1b9 |
74 | On Windows platforms, if the C<-C> command line switch is used, (or the |
75daf61c |
75 | ${^WIDE_SYSTEM_CALLS} global flag is set to C<1>), all system calls |
76 | will use the corresponding wide character APIs. Note that this is |
c349b1b9 |
77 | currently only implemented on Windows since other platforms lack an |
78 | API standard on this area. |
8cbd9a7a |
79 | |
75daf61c |
80 | Regardless of the above, the C<bytes> pragma can always be used to |
81 | force byte semantics in a particular lexical scope. See L<bytes>. |
8cbd9a7a |
82 | |
83 | The C<utf8> pragma is primarily a compatibility device that enables |
75daf61c |
84 | recognition of UTF-(8|EBCDIC) in literals encountered by the parser. |
7dedd01f |
85 | Note that this pragma is only required until a future version of Perl |
86 | in which character semantics will become the default. This pragma may |
87 | then become a no-op. See L<utf8>. |
8cbd9a7a |
88 | |
89 | Unless mentioned otherwise, Perl operators will use character semantics |
90 | when they are dealing with Unicode data, and byte semantics otherwise. |
91 | Thus, character semantics for these operations apply transparently; if |
92 | the input data came from a Unicode source (for example, by adding a |
93 | character encoding discipline to the filehandle whence it came, or a |
94 | literal UTF-8 string constant in the program), character semantics |
95 | apply; otherwise, byte semantics are in effect. To force byte semantics |
8058d7ab |
96 | on Unicode data, the C<bytes> pragma should be used. |
393fec97 |
97 | |
0a378802 |
98 | Notice that if you concatenate strings with byte semantics and strings |
99 | with Unicode character data, the bytes will by default be upgraded |
100 | I<as if they were ISO 8859-1 (Latin-1)> (or if in EBCDIC, after a |
101 | translation to ISO 8859-1). To change this, use the C<encoding> |
102 | pragma, see L<encoding>. |
7dedd01f |
103 | |
393fec97 |
104 | Under character semantics, many operations that formerly operated on |
75daf61c |
105 | bytes change to operating on characters. For ASCII data this makes no |
106 | difference, because UTF-8 stores ASCII in single bytes, but for any |
107 | character greater than C<chr(127)>, the character B<may> be stored in |
393fec97 |
108 | a sequence of two or more bytes, all of which have the high bit set. |
2796c109 |
109 | |
110 | For C1 controls or Latin 1 characters on an EBCDIC platform the |
111 | character may be stored in a UTF-EBCDIC multi byte sequence. But by |
112 | and large, the user need not worry about this, because Perl hides it |
113 | from the user. A character in Perl is logically just a number ranging |
114 | from 0 to 2**32 or so. Larger characters encode to longer sequences |
115 | of bytes internally, but again, this is just an internal detail which |
116 | is hidden at the Perl level. |
393fec97 |
117 | |
8cbd9a7a |
118 | =head2 Effects of character semantics |
393fec97 |
119 | |
120 | Character semantics have the following effects: |
121 | |
122 | =over 4 |
123 | |
124 | =item * |
125 | |
126 | Strings and patterns may contain characters that have an ordinal value |
21bad921 |
127 | larger than 255. |
393fec97 |
128 | |
75daf61c |
129 | Presuming you use a Unicode editor to edit your program, such |
130 | characters will typically occur directly within the literal strings as |
131 | UTF-8 (or UTF-EBCDIC on EBCDIC platforms) characters, but you can also |
132 | specify a particular character with an extension of the C<\x> |
133 | notation. UTF-X characters are specified by putting the hexadecimal |
134 | code within curlies after the C<\x>. For instance, a Unicode smiley |
135 | face is C<\x{263A}>. |
393fec97 |
136 | |
137 | =item * |
138 | |
139 | Identifiers within the Perl script may contain Unicode alphanumeric |
140 | characters, including ideographs. (You are currently on your own when |
75daf61c |
141 | it comes to using the canonical forms of characters--Perl doesn't |
142 | (yet) attempt to canonicalize variable names for you.) |
393fec97 |
143 | |
393fec97 |
144 | =item * |
145 | |
146 | Regular expressions match characters instead of bytes. For instance, |
147 | "." matches a character instead of a byte. (However, the C<\C> pattern |
75daf61c |
148 | is provided to force a match a single byte ("C<char>" in C, hence C<\C>).) |
393fec97 |
149 | |
393fec97 |
150 | =item * |
151 | |
152 | Character classes in regular expressions match characters instead of |
153 | bytes, and match against the character properties specified in the |
75daf61c |
154 | Unicode properties database. So C<\w> can be used to match an |
155 | ideograph, for instance. |
393fec97 |
156 | |
393fec97 |
157 | =item * |
158 | |
159 | Named Unicode properties and block ranges make be used as character |
160 | classes via the new C<\p{}> (matches property) and C<\P{}> (doesn't |
161 | match property) constructs. For instance, C<\p{Lu}> matches any |
162 | character with the Unicode uppercase property, while C<\p{M}> matches |
9fdf68be |
163 | any mark character. Single letter properties may omit the brackets, |
164 | so that can be written C<\pM> also. Many predefined character classes |
a1cc1cb1 |
165 | are available, such as C<\p{IsMirrored}> and C<\p{InTibetan}>. |
4193bef7 |
166 | |
167 | The C<\p{Is...}> test for "general properties" such as "letter", |
168 | "digit", while the C<\p{In...}> test for Unicode scripts and blocks. |
169 | |
e150c829 |
170 | The official Unicode script and block names have spaces and dashes and |
171 | separators, but for convenience you can have dashes, spaces, and |
172 | underbars at every word division, and you need not care about correct |
173 | casing. It is recommended, however, that for consistency you use the |
174 | following naming: the official Unicode script, block, or property name |
175 | (see below for the additional rules that apply to block names), |
176 | with whitespace and dashes replaced with underbar, and the words |
177 | "uppercase-first-lowercase-rest". That is, "Latin-1 Supplement" |
178 | becomes "Latin_1_Supplement". |
4193bef7 |
179 | |
a1cc1cb1 |
180 | You can also negate both C<\p{}> and C<\P{}> by introducing a caret |
e150c829 |
181 | (^) between the first curly and the property name: C<\p{^In_Tamil}> is |
182 | equal to C<\P{In_Tamil}>. |
4193bef7 |
183 | |
61247495 |
184 | The C<In> and C<Is> can be left out: C<\p{Greek}> is equal to |
e150c829 |
185 | C<\p{In_Greek}>, C<\P{Pd}> is equal to C<\P{Pd}>. |
393fec97 |
186 | |
d73e5302 |
187 | Short Long |
188 | |
189 | L Letter |
e150c829 |
190 | Lu Uppercase_Letter |
191 | Ll Lowercase_Letter |
192 | Lt Titlecase_Letter |
193 | Lm Modifier_Letter |
194 | Lo Other_Letter |
d73e5302 |
195 | |
196 | M Mark |
e150c829 |
197 | Mn Nonspacing_Mark |
198 | Mc Spacing_Mark |
199 | Me Enclosing_Mark |
d73e5302 |
200 | |
201 | N Number |
e150c829 |
202 | Nd Decimal_Number |
203 | Nl Letter_Number |
204 | No Other_Number |
d73e5302 |
205 | |
206 | P Punctuation |
e150c829 |
207 | Pc Connector_Punctuation |
208 | Pd Dash_Punctuation |
209 | Ps Open_Punctuation |
210 | Pe Close_Punctuation |
211 | Pi Initial_Punctuation |
d73e5302 |
212 | (may behave like Ps or Pe depending on usage) |
e150c829 |
213 | Pf Final_Punctuation |
d73e5302 |
214 | (may behave like Ps or Pe depending on usage) |
e150c829 |
215 | Po Other_Punctuation |
d73e5302 |
216 | |
217 | S Symbol |
e150c829 |
218 | Sm Math_Symbol |
219 | Sc Currency_Symbol |
220 | Sk Modifier_Symbol |
221 | So Other_Symbol |
d73e5302 |
222 | |
223 | Z Separator |
e150c829 |
224 | Zs Space_Separator |
225 | Zl Line_Separator |
226 | Zp Paragraph_Separator |
d73e5302 |
227 | |
228 | C Other |
e150c829 |
229 | Cc Control |
230 | Cf Format |
231 | Cs Surrogate |
232 | Co Private_Use |
233 | Cn Unassigned |
1ac13f9a |
234 | |
235 | There's also C<L&> which is an alias for C<Ll>, C<Lu>, and C<Lt>. |
32293815 |
236 | |
d73e5302 |
237 | The following reserved ranges have C<In> tests: |
238 | |
e150c829 |
239 | CJK_Ideograph_Extension_A |
240 | CJK_Ideograph |
241 | Hangul_Syllable |
242 | Non_Private_Use_High_Surrogate |
243 | Private_Use_High_Surrogate |
244 | Low_Surrogate |
245 | Private_Surrogate |
246 | CJK_Ideograph_Extension_B |
247 | Plane_15_Private_Use |
248 | Plane_16_Private_Use |
d73e5302 |
249 | |
250 | For example C<"\x{AC00}" =~ \p{HangulSyllable}> will test true. |
e9ad1727 |
251 | (Handling of surrogates is not implemented yet, because Perl |
252 | uses UTF-8 and not UTF-16 internally to represent Unicode.) |
d73e5302 |
253 | |
32293815 |
254 | Additionally, because scripts differ in their directionality |
255 | (for example Hebrew is written right to left), all characters |
256 | have their directionality defined: |
257 | |
d73e5302 |
258 | BidiL Left-to-Right |
259 | BidiLRE Left-to-Right Embedding |
260 | BidiLRO Left-to-Right Override |
261 | BidiR Right-to-Left |
262 | BidiAL Right-to-Left Arabic |
263 | BidiRLE Right-to-Left Embedding |
264 | BidiRLO Right-to-Left Override |
265 | BidiPDF Pop Directional Format |
266 | BidiEN European Number |
267 | BidiES European Number Separator |
268 | BidiET European Number Terminator |
269 | BidiAN Arabic Number |
270 | BidiCS Common Number Separator |
271 | BidiNSM Non-Spacing Mark |
272 | BidiBN Boundary Neutral |
273 | BidiB Paragraph Separator |
274 | BidiS Segment Separator |
275 | BidiWS Whitespace |
276 | BidiON Other Neutrals |
32293815 |
277 | |
210b36aa |
278 | =back |
279 | |
2796c109 |
280 | =head2 Scripts |
281 | |
75daf61c |
282 | The scripts available for C<\p{In...}> and C<\P{In...}>, for example |
283 | \p{InCyrillic>, are as follows, for example C<\p{InLatin}> or C<\P{InHan}>: |
2796c109 |
284 | |
1ac13f9a |
285 | Arabic |
e9ad1727 |
286 | Armenian |
1ac13f9a |
287 | Bengali |
e9ad1727 |
288 | Bopomofo |
289 | Canadian-Aboriginal |
290 | Cherokee |
291 | Cyrillic |
292 | Deseret |
293 | Devanagari |
294 | Ethiopic |
295 | Georgian |
296 | Gothic |
297 | Greek |
1ac13f9a |
298 | Gujarati |
e9ad1727 |
299 | Gurmukhi |
300 | Han |
301 | Hangul |
302 | Hebrew |
303 | Hiragana |
304 | Inherited |
1ac13f9a |
305 | Kannada |
e9ad1727 |
306 | Katakana |
307 | Khmer |
1ac13f9a |
308 | Lao |
e9ad1727 |
309 | Latin |
310 | Malayalam |
311 | Mongolian |
1ac13f9a |
312 | Myanmar |
1ac13f9a |
313 | Ogham |
e9ad1727 |
314 | Old-Italic |
315 | Oriya |
1ac13f9a |
316 | Runic |
e9ad1727 |
317 | Sinhala |
318 | Syriac |
319 | Tamil |
320 | Telugu |
321 | Thaana |
322 | Thai |
323 | Tibetan |
1ac13f9a |
324 | Yi |
1ac13f9a |
325 | |
326 | There are also extended property classes that supplement the basic |
327 | properties, defined by the F<PropList> Unicode database: |
328 | |
e9ad1727 |
329 | ASCII_Hex_Digit |
1ac13f9a |
330 | Bidi_Control |
1ac13f9a |
331 | Dash |
1ac13f9a |
332 | Diacritic |
333 | Extender |
e9ad1727 |
334 | Hex_Digit |
335 | Hyphen |
336 | Ideographic |
337 | Join_Control |
338 | Noncharacter_Code_Point |
339 | Other_Alphabetic |
1ac13f9a |
340 | Other_Lowercase |
e9ad1727 |
341 | Other_Math |
1ac13f9a |
342 | Other_Uppercase |
e9ad1727 |
343 | Quotation_Mark |
e150c829 |
344 | White_Space |
1ac13f9a |
345 | |
346 | and further derived properties: |
347 | |
348 | Alphabetic Lu + Ll + Lt + Lm + Lo + Other_Alphabetic |
349 | Lowercase Ll + Other_Lowercase |
350 | Uppercase Lu + Other_Uppercase |
351 | Math Sm + Other_Math |
352 | |
353 | ID_Start Lu + Ll + Lt + Lm + Lo + Nl |
354 | ID_Continue ID_Start + Mn + Mc + Nd + Pc |
355 | |
356 | Any Any character |
357 | Assigned Any non-Cn character |
358 | Common Any character (or unassigned code point) |
e150c829 |
359 | not explicitly assigned to a script |
2796c109 |
360 | |
361 | =head2 Blocks |
362 | |
363 | In addition to B<scripts>, Unicode also defines B<blocks> of |
364 | characters. The difference between scripts and blocks is that the |
e9ad1727 |
365 | scripts concept is closer to natural languages, while the blocks |
2796c109 |
366 | concept is more an artificial grouping based on groups of 256 Unicode |
367 | characters. For example, the C<Latin> script contains letters from |
e9ad1727 |
368 | many blocks. On the other hand, the C<Latin> script does not contain |
369 | all the characters from those blocks, it does not for example contain |
370 | digits because digits are shared across many scripts. Digits and |
371 | other similar groups, like punctuation, are in a category called |
372 | C<Common>. |
2796c109 |
373 | |
374 | For more about scripts see the UTR #24: |
375 | http://www.unicode.org/unicode/reports/tr24/ |
376 | For more about blocks see |
377 | http://www.unicode.org/Public/UNIDATA/Blocks.txt |
378 | |
379 | Because there are overlaps in naming (there are, for example, both |
380 | a script called C<Katakana> and a block called C<Katakana>, the block |
381 | version has C<Block> appended to its name, C<\p{InKatakanaBlock}>. |
382 | |
383 | Notice that this definition was introduced in Perl 5.8.0: in Perl |
e150c829 |
384 | 5.6 only the blocks were used; in Perl 5.8.0 scripts became the |
61247495 |
385 | preferential Unicode character class definition; this meant that |
386 | the definitions of some character classes changed (the ones in the |
2796c109 |
387 | below list that have the C<Block> appended). |
388 | |
e9ad1727 |
389 | Alphabetic Presentation Forms |
390 | Arabic Block |
391 | Arabic Presentation Forms-A |
392 | Arabic Presentation Forms-B |
393 | Armenian Block |
394 | Arrows |
71d929cb |
395 | Basic Latin |
e9ad1727 |
396 | Bengali Block |
397 | Block Elements |
398 | Bopomofo Block |
399 | Bopomofo Extended |
400 | Box Drawing |
401 | Braille Patterns |
402 | Byzantine Musical Symbols |
403 | CJK Compatibility |
404 | CJK Compatibility Forms |
405 | CJK Compatibility Ideographs |
406 | CJK Compatibility Ideographs Supplement |
407 | CJK Radicals Supplement |
408 | CJK Symbols and Punctuation |
409 | CJK Unified Ideographs |
410 | CJK Unified Ideographs Extension A |
411 | CJK Unified Ideographs Extension B |
412 | Cherokee Block |
71d929cb |
413 | Combining Diacritical Marks |
e9ad1727 |
414 | Combining Half Marks |
415 | Combining Marks for Symbols |
416 | Control Pictures |
417 | Currency Symbols |
71d929cb |
418 | Cyrillic Block |
e9ad1727 |
419 | Deseret Block |
71d929cb |
420 | Devanagari Block |
e9ad1727 |
421 | Dingbats |
422 | Enclosed Alphanumerics |
423 | Enclosed CJK Letters and Months |
424 | Ethiopic Block |
425 | General Punctuation |
426 | Geometric Shapes |
71d929cb |
427 | Georgian Block |
e9ad1727 |
428 | Gothic Block |
429 | Greek Block |
430 | Greek Extended |
431 | Gujarati Block |
432 | Gurmukhi Block |
433 | Halfwidth and Fullwidth Forms |
434 | Hangul Compatibility Jamo |
71d929cb |
435 | Hangul Jamo |
e9ad1727 |
436 | Hangul Syllables |
437 | Hebrew Block |
438 | High Private Use Surrogates |
439 | High Surrogates |
440 | Hiragana Block |
441 | IPA Extensions |
442 | Ideographic Description Characters |
443 | Kanbun |
444 | Kangxi Radicals |
445 | Kannada Block |
446 | Katakana Block |
71d929cb |
447 | Khmer Block |
e9ad1727 |
448 | Lao Block |
449 | Latin 1 Supplement |
71d929cb |
450 | Latin Extended Additional |
e9ad1727 |
451 | Latin Extended-A |
452 | Latin Extended-B |
71d929cb |
453 | Letterlike Symbols |
e9ad1727 |
454 | Low Surrogates |
455 | Malayalam Block |
456 | Mathematical Alphanumeric Symbols |
71d929cb |
457 | Mathematical Operators |
e9ad1727 |
458 | Miscellaneous Symbols |
71d929cb |
459 | Miscellaneous Technical |
e9ad1727 |
460 | Mongolian Block |
461 | Musical Symbols |
462 | Myanmar Block |
463 | Number Forms |
464 | Ogham Block |
465 | Old Italic Block |
71d929cb |
466 | Optical Character Recognition |
e9ad1727 |
467 | Oriya Block |
71d929cb |
468 | Private Use |
e9ad1727 |
469 | Runic Block |
470 | Sinhala Block |
71d929cb |
471 | Small Form Variants |
e9ad1727 |
472 | Spacing Modifier Letters |
2796c109 |
473 | Specials |
e9ad1727 |
474 | Superscripts and Subscripts |
475 | Syriac Block |
2796c109 |
476 | Tags |
e9ad1727 |
477 | Tamil Block |
478 | Telugu Block |
479 | Thaana Block |
480 | Thai Block |
481 | Tibetan Block |
482 | Unified Canadian Aboriginal Syllabics |
483 | Yi Radicals |
484 | Yi Syllables |
32293815 |
485 | |
210b36aa |
486 | =over 4 |
487 | |
393fec97 |
488 | =item * |
489 | |
490 | The special pattern C<\X> match matches any extended Unicode sequence |
491 | (a "combining character sequence" in Standardese), where the first |
492 | character is a base character and subsequent characters are mark |
493 | characters that apply to the base character. It is equivalent to |
494 | C<(?:\PM\pM*)>. |
495 | |
393fec97 |
496 | =item * |
497 | |
383e7cdd |
498 | The C<tr///> operator translates characters instead of bytes. Note |
499 | that the C<tr///CU> functionality has been removed, as the interface |
500 | was a mistake. For similar functionality see pack('U0', ...) and |
501 | pack('C0', ...). |
393fec97 |
502 | |
393fec97 |
503 | =item * |
504 | |
505 | Case translation operators use the Unicode case translation tables |
44bc797b |
506 | when provided character input. Note that C<uc()> (also known as C<\U> |
507 | in doublequoted strings) translates to uppercase, while C<ucfirst> |
508 | (also known as C<\u> in doublequoted strings) translates to titlecase |
509 | (for languages that make the distinction). Naturally the |
510 | corresponding backslash sequences have the same semantics. |
393fec97 |
511 | |
512 | =item * |
513 | |
514 | Most operators that deal with positions or lengths in the string will |
75daf61c |
515 | automatically switch to using character positions, including |
516 | C<chop()>, C<substr()>, C<pos()>, C<index()>, C<rindex()>, |
517 | C<sprintf()>, C<write()>, and C<length()>. Operators that |
518 | specifically don't switch include C<vec()>, C<pack()>, and |
519 | C<unpack()>. Operators that really don't care include C<chomp()>, as |
520 | well as any other operator that treats a string as a bucket of bits, |
521 | such as C<sort()>, and the operators dealing with filenames. |
393fec97 |
522 | |
523 | =item * |
524 | |
525 | The C<pack()>/C<unpack()> letters "C<c>" and "C<C>" do I<not> change, |
526 | since they're often used for byte-oriented formats. (Again, think |
527 | "C<char>" in the C language.) However, there is a new "C<U>" specifier |
528 | that will convert between UTF-8 characters and integers. (It works |
529 | outside of the utf8 pragma too.) |
530 | |
531 | =item * |
532 | |
533 | The C<chr()> and C<ord()> functions work on characters. This is like |
534 | C<pack("U")> and C<unpack("U")>, not like C<pack("C")> and |
535 | C<unpack("C")>. In fact, the latter are how you now emulate |
35bcd338 |
536 | byte-oriented C<chr()> and C<ord()> for Unicode strings. |
537 | (Note that this reveals the internal UTF-8 encoding of strings and |
538 | you are not supposed to do that unless you know what you are doing.) |
393fec97 |
539 | |
540 | =item * |
541 | |
a1ca4561 |
542 | The bit string operators C<& | ^ ~> can operate on character data. |
543 | However, for backward compatibility reasons (bit string operations |
75daf61c |
544 | when the characters all are less than 256 in ordinal value) one should |
545 | not mix C<~> (the bit complement) and characters both less than 256 and |
a1ca4561 |
546 | equal or greater than 256. Most importantly, the DeMorgan's laws |
547 | (C<~($x|$y) eq ~$x&~$y>, C<~($x&$y) eq ~$x|~$y>) won't hold. |
548 | Another way to look at this is that the complement cannot return |
75daf61c |
549 | B<both> the 8-bit (byte) wide bit complement B<and> the full character |
a1ca4561 |
550 | wide bit complement. |
551 | |
552 | =item * |
553 | |
983ffd37 |
554 | lc(), uc(), lcfirst(), and ucfirst() work for the following cases: |
555 | |
556 | =over 8 |
557 | |
558 | =item * |
559 | |
560 | the case mapping is from a single Unicode character to another |
561 | single Unicode character |
562 | |
563 | =item * |
564 | |
565 | the case mapping is from a single Unicode character to more |
566 | than one Unicode character |
567 | |
568 | =back |
569 | |
210b36aa |
570 | What doesn't yet work are the following cases: |
983ffd37 |
571 | |
572 | =over 8 |
573 | |
574 | =item * |
575 | |
576 | the "final sigma" (Greek) |
577 | |
578 | =item * |
579 | |
580 | anything to with locales (Lithuanian, Turkish, Azeri) |
581 | |
582 | =back |
583 | |
584 | See the Unicode Technical Report #21, Case Mappings, for more details. |
ac1256e8 |
585 | |
586 | =item * |
587 | |
393fec97 |
588 | And finally, C<scalar reverse()> reverses by character rather than by byte. |
589 | |
590 | =back |
591 | |
8cbd9a7a |
592 | =head2 Character encodings for input and output |
593 | |
7221edc9 |
594 | See L<Encode>. |
8cbd9a7a |
595 | |
393fec97 |
596 | =head1 CAVEATS |
597 | |
598 | As of yet, there is no method for automatically coercing input and |
b3419ed8 |
599 | output to some encoding other than UTF-8 or UTF-EBCDIC. This is planned |
600 | in the near future, however. |
393fec97 |
601 | |
8cbd9a7a |
602 | Whether an arbitrary piece of data will be treated as "characters" or |
603 | "bytes" by internal operations cannot be divined at the current time. |
393fec97 |
604 | |
605 | Use of locales with utf8 may lead to odd results. Currently there is |
606 | some attempt to apply 8-bit locale info to characters in the range |
607 | 0..255, but this is demonstrably incorrect for locales that use |
608 | characters above that range (when mapped into Unicode). It will also |
609 | tend to run slower. Avoidance of locales is strongly encouraged. |
610 | |
776f8809 |
611 | =head1 UNICODE REGULAR EXPRESSION SUPPORT LEVEL |
612 | |
613 | The following list of Unicode regular expression support describes |
614 | feature by feature the Unicode support implemented in Perl as of Perl |
615 | 5.8.0. The "Level N" and the section numbers refer to the Unicode |
616 | Technical Report 18, "Unicode Regular Expression Guidelines". |
617 | |
618 | =over 4 |
619 | |
620 | =item * |
621 | |
622 | Level 1 - Basic Unicode Support |
623 | |
624 | 2.1 Hex Notation - done [1] |
625 | Named Notation - done [2] |
626 | 2.2 Categories - done [3][4] |
627 | 2.3 Subtraction - MISSING [5][6] |
628 | 2.4 Simple Word Boundaries - done [7] |
90a59240 |
629 | 2.5 Simple Loose Matches - done [8] |
776f8809 |
630 | 2.6 End of Line - MISSING [9][10] |
631 | |
632 | [ 1] \x{...} |
633 | [ 2] \N{...} |
634 | [ 3] . \p{Is...} \P{Is...} |
29bdacb8 |
635 | [ 4] now scripts (see UTR#24 Script Names) in addition to blocks |
776f8809 |
636 | [ 5] have negation |
29bdacb8 |
637 | [ 6] can use look-ahead to emulate subtraction (*) |
776f8809 |
638 | [ 7] include Letters in word characters |
90a59240 |
639 | [ 8] see UTR#21 Case Mappings: Perl implements 1:1 mappings |
776f8809 |
640 | [ 9] see UTR#13 Unicode Newline Guidelines |
641 | [10] should do ^ and $ also on \x{2028} and \x{2029} |
642 | |
29bdacb8 |
643 | (*) Instead of [\u0370-\u03FF-[{UNASSIGNED}]] as suggested by the TR |
644 | 18 you can use negated lookahead: to match currently assigned modern |
645 | Greek characters use for example |
646 | |
647 | /(?!\p{Cn})[\x{0370}-\x{03ff}]/ |
648 | |
649 | In other words: the matched character must not be a non-assigned |
650 | character, but it must be in the block of modern Greek characters. |
651 | |
776f8809 |
652 | =item * |
653 | |
654 | Level 2 - Extended Unicode Support |
655 | |
656 | 3.1 Surrogates - MISSING |
657 | 3.2 Canonical Equivalents - MISSING [11][12] |
658 | 3.3 Locale-Independent Graphemes - MISSING [13] |
659 | 3.4 Locale-Independent Words - MISSING [14] |
660 | 3.5 Locale-Independent Loose Matches - MISSING [15] |
661 | |
662 | [11] see UTR#15 Unicode Normalization |
663 | [12] have Unicode::Normalize but not integrated to regexes |
664 | [13] have \X but at this level . should equal that |
665 | [14] need three classes, not just \w and \W |
666 | [15] see UTR#21 Case Mappings |
667 | |
668 | =item * |
669 | |
670 | Level 3 - Locale-Sensitive Support |
671 | |
672 | 4.1 Locale-Dependent Categories - MISSING |
673 | 4.2 Locale-Dependent Graphemes - MISSING [16][17] |
674 | 4.3 Locale-Dependent Words - MISSING |
675 | 4.4 Locale-Dependent Loose Matches - MISSING |
676 | 4.5 Locale-Dependent Ranges - MISSING |
677 | |
678 | [16] see UTR#10 Unicode Collation Algorithms |
679 | [17] have Unicode::Collate but not integrated to regexes |
680 | |
681 | =back |
682 | |
c349b1b9 |
683 | =head2 Unicode Encodings |
684 | |
685 | Unicode characters are assigned to I<code points> which are abstract |
86bbd6d1 |
686 | numbers. To use these numbers various encodings are needed. |
c349b1b9 |
687 | |
688 | =over 4 |
689 | |
690 | =item UTF-8 |
691 | |
86bbd6d1 |
692 | UTF-8 is the encoding used internally by Perl. UTF-8 is a variable |
c349b1b9 |
693 | length (1 to 6 bytes, current character allocations require 4 bytes), |
86bbd6d1 |
694 | byteorder independent encoding. For ASCII, UTF-8 is transparent |
695 | (and we really do mean 7-bit ASCII, not any 8-bit encoding). |
c349b1b9 |
696 | |
05632f9a |
697 | The following table is from Unicode 3.1. |
698 | |
699 | Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte |
700 | |
701 | U+0000..U+007F 00..7F |
702 | U+0080..U+07FF C2..DF 80..BF |
703 | U+0800..U+0FFF E0 A0..BF 80..BF |
704 | U+1000..U+FFFF E1..EF 80..BF 80..BF |
705 | U+10000..U+3FFFF F0 90..BF 80..BF 80..BF |
706 | U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF |
707 | U+100000..U+10FFFF F4 80..8F 80..BF 80..BF |
708 | |
709 | Or, another way to look at it, as bits: |
710 | |
711 | Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte |
712 | |
713 | 0aaaaaaa 0aaaaaaa |
714 | 00000bbbbbaaaaaa 110bbbbb 10aaaaaa |
715 | ccccbbbbbbaaaaaa 1110cccc 10bbbbbb 10aaaaaa |
716 | 00000dddccccccbbbbbbaaaaaa 11110ddd 10cccccc 10bbbbbb 10aaaaaa |
717 | |
718 | As you can see, the continuation bytes all begin with C<10>, and the |
719 | leading bits of the start byte tells how many bytes the are in the |
720 | encoded character. |
721 | |
c349b1b9 |
722 | =item UTF-16, UTF-16BE, UTF16-LE, Surrogates, and BOMs (Byte Order Marks) |
723 | |
724 | UTF-16 is a 2 or 4 byte encoding. The Unicode code points |
725 | 0x0000..0xFFFF are stored in two 16-bit units, and the code points |
726 | 0x010000..0x10FFFF in four 16-bit units. The latter case is |
727 | using I<surrogates>, the first 16-bit unit being the I<high |
728 | surrogate>, and the second being the I<low surrogate>. |
729 | |
730 | Surrogates are code points set aside to encode the 0x01000..0x10FFFF |
731 | range of Unicode code points in pairs of 16-bit units. The I<high |
732 | surrogates> are the range 0xD800..0xDBFF, and the I<low surrogates> |
733 | are the range 0xDC00..0xDFFFF. The surrogate encoding is |
734 | |
735 | $hi = ($uni - 0x10000) / 0x400 + 0xD800; |
736 | $lo = ($uni - 0x10000) % 0x400 + 0xDC00; |
737 | |
738 | and the decoding is |
739 | |
740 | $uni = 0x10000 + ($hi - 0xD8000) * 0x400 + ($lo - 0xDC00); |
741 | |
86bbd6d1 |
742 | Because of the 16-bitness, UTF-16 is byteorder dependent. UTF-16 |
c349b1b9 |
743 | itself can be used for in-memory computations, but if storage or |
86bbd6d1 |
744 | transfer is required, either UTF-16BE (Big Endian) or UTF-16LE |
c349b1b9 |
745 | (Little Endian) must be chosen. |
746 | |
747 | This introduces another problem: what if you just know that your data |
748 | is UTF-16, but you don't know which endianness? Byte Order Marks |
749 | (BOMs) are a solution to this. A special character has been reserved |
86bbd6d1 |
750 | in Unicode to function as a byte order marker: the character with the |
751 | code point 0xFEFF is the BOM. |
042da322 |
752 | |
c349b1b9 |
753 | The trick is that if you read a BOM, you will know the byte order, |
754 | since if it was written on a big endian platform, you will read the |
86bbd6d1 |
755 | bytes 0xFE 0xFF, but if it was written on a little endian platform, |
756 | you will read the bytes 0xFF 0xFE. (And if the originating platform |
757 | was writing in UTF-8, you will read the bytes 0xEF 0xBB 0xBF.) |
042da322 |
758 | |
86bbd6d1 |
759 | The way this trick works is that the character with the code point |
760 | 0xFFFE is guaranteed not to be a valid Unicode character, so the |
761 | sequence of bytes 0xFF 0xFE is unambiguously "BOM, represented in |
042da322 |
762 | little-endian format" and cannot be "0xFFFE, represented in big-endian |
763 | format". |
c349b1b9 |
764 | |
765 | =item UTF-32, UTF-32BE, UTF32-LE |
766 | |
767 | The UTF-32 family is pretty much like the UTF-16 family, expect that |
042da322 |
768 | the units are 32-bit, and therefore the surrogate scheme is not |
769 | needed. The BOM signatures will be 0x00 0x00 0xFE 0xFF for BE and |
770 | 0xFF 0xFE 0x00 0x00 for LE. |
c349b1b9 |
771 | |
772 | =item UCS-2, UCS-4 |
773 | |
86bbd6d1 |
774 | Encodings defined by the ISO 10646 standard. UCS-2 is a 16-bit |
775 | encoding, UCS-4 is a 32-bit encoding. Unlike UTF-16, UCS-2 |
776 | is not extensible beyond 0xFFFF, because it does not use surrogates. |
c349b1b9 |
777 | |
778 | =item UTF-7 |
779 | |
780 | A seven-bit safe (non-eight-bit) encoding, useful if the |
781 | transport/storage is not eight-bit safe. Defined by RFC 2152. |
782 | |
bf0fa0b2 |
783 | =head2 Security Implications of Malformed UTF-8 |
784 | |
785 | Unfortunately, the specification of UTF-8 leaves some room for |
786 | interpretation of how many bytes of encoded output one should generate |
787 | from one input Unicode character. Strictly speaking, one is supposed |
788 | to always generate the shortest possible sequence of UTF-8 bytes, |
789 | because otherwise there is potential for input buffer overflow at the |
790 | receiving end of a UTF-8 connection. Perl always generates the shortest |
791 | length UTF-8, and with warnings on (C<-w> or C<use warnings;>) Perl will |
792 | warn about non-shortest length UTF-8 (and other malformations, too, |
793 | such as the surrogates, which are not real character code points.) |
794 | |
c349b1b9 |
795 | =head2 Unicode in Perl on EBCDIC |
796 | |
797 | The way Unicode is handled on EBCDIC platforms is still rather |
86bbd6d1 |
798 | experimental. On such a platform, references to UTF-8 encoding in this |
c349b1b9 |
799 | document and elsewhere should be read as meaning UTF-EBCDIC as |
800 | specified in Unicode Technical Report 16 unless ASCII vs EBCDIC issues |
801 | are specifically discussed. There is no C<utfebcdic> pragma or |
86bbd6d1 |
802 | ":utfebcdic" layer, rather, "utf8" and ":utf8" are re-used to mean |
803 | the platform's "natural" 8-bit encoding of Unicode. See L<perlebcdic> |
804 | for more discussion of the issues. |
c349b1b9 |
805 | |
806 | =back |
807 | |
393fec97 |
808 | =head1 SEE ALSO |
809 | |
72ff2908 |
810 | L<perluniintro>, L<encoding>, L<Encode>, L<open>, L<utf8>, L<bytes>, |
811 | L<perlretut>, L<perlvar/"${^WIDE_SYSTEM_CALLS}"> |
393fec97 |
812 | |
813 | =cut |