Commit | Line | Data |
---|---|---|
393fec97 GS |
1 | =head1 NAME |
2 | ||
3 | perlunicode - Unicode support in Perl | |
4 | ||
5 | =head1 DESCRIPTION | |
6 | ||
0a1f2d14 | 7 | =head2 Important Caveats |
21bad921 | 8 | |
c349b1b9 JH |
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 GS |
14 | |
15 | =item Input and Output Disciplines | |
16 | ||
75daf61c JH |
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 JH |
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 GS |
25 | |
26 | =item Regular Expressions | |
27 | ||
c349b1b9 JH |
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 JH |
36 | However, these tables are automatically loaded on demand, so the |
37 | C<utf8> pragma should not normally be used. | |
21bad921 | 38 | |
c349b1b9 JH |
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 GS |
48 | =back |
49 | ||
50 | =head2 Byte and Character semantics | |
393fec97 GS |
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 JH |
56 | In future, Perl-level operations can be expected to work with |
57 | characters rather than bytes, in general. | |
393fec97 | 58 | |
75daf61c JH |
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 GS |
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 JH |
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 JH |
77 | currently only implemented on Windows since other platforms lack an |
78 | API standard on this area. | |
8cbd9a7a | 79 | |
75daf61c JH |
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 GS |
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 JH |
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 GS |
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 JH |
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 JH |
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 JH |
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 GS |
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 JH |
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 GS |
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 JH |
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 GS |
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 GS |
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 JH |
154 | Unicode properties database. So C<\w> can be used to match an |
155 | ideograph, for instance. | |
393fec97 | 156 | |
393fec97 GS |
157 | =item * |
158 | ||
cfc01aea | 159 | Named Unicode properties and block ranges may be used as character |
393fec97 GS |
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 JH |
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 JH |
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 | ||
cfc01aea | 170 | The official Unicode script and block names have spaces and dashes as |
e150c829 JH |
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 JH |
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 JH |
187 | Short Long |
188 | ||
189 | L Letter | |
e150c829 JH |
190 | Lu Uppercase_Letter |
191 | Ll Lowercase_Letter | |
192 | Lt Titlecase_Letter | |
193 | Lm Modifier_Letter | |
194 | Lo Other_Letter | |
d73e5302 JH |
195 | |
196 | M Mark | |
e150c829 JH |
197 | Mn Nonspacing_Mark |
198 | Mc Spacing_Mark | |
199 | Me Enclosing_Mark | |
d73e5302 JH |
200 | |
201 | N Number | |
e150c829 JH |
202 | Nd Decimal_Number |
203 | Nl Letter_Number | |
204 | No Other_Number | |
d73e5302 JH |
205 | |
206 | P Punctuation | |
e150c829 JH |
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 JH |
216 | |
217 | S Symbol | |
e150c829 JH |
218 | Sm Math_Symbol |
219 | Sc Currency_Symbol | |
220 | Sk Modifier_Symbol | |
221 | So Other_Symbol | |
d73e5302 JH |
222 | |
223 | Z Separator | |
e150c829 JH |
224 | Zs Space_Separator |
225 | Zl Line_Separator | |
226 | Zp Paragraph_Separator | |
d73e5302 JH |
227 | |
228 | C Other | |
e150c829 JH |
229 | Cc Control |
230 | Cf Format | |
231 | Cs Surrogate | |
232 | Co Private_Use | |
233 | Cn Unassigned | |
1ac13f9a JH |
234 | |
235 | There's also C<L&> which is an alias for C<Ll>, C<Lu>, and C<Lt>. | |
32293815 | 236 | |
d73e5302 JH |
237 | The following reserved ranges have C<In> tests: |
238 | ||
e150c829 JH |
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 JH |
249 | |
250 | For example C<"\x{AC00}" =~ \p{HangulSyllable}> will test true. | |
e9ad1727 | 251 | (Handling of surrogates is not implemented yet, because Perl |
0675fa52 JH |
252 | uses UTF-8 and not UTF-16 internally to represent Unicode. |
253 | So you really can't use the "Cs" category.) | |
d73e5302 | 254 | |
32293815 JH |
255 | Additionally, because scripts differ in their directionality |
256 | (for example Hebrew is written right to left), all characters | |
257 | have their directionality defined: | |
258 | ||
d73e5302 JH |
259 | BidiL Left-to-Right |
260 | BidiLRE Left-to-Right Embedding | |
261 | BidiLRO Left-to-Right Override | |
262 | BidiR Right-to-Left | |
263 | BidiAL Right-to-Left Arabic | |
264 | BidiRLE Right-to-Left Embedding | |
265 | BidiRLO Right-to-Left Override | |
266 | BidiPDF Pop Directional Format | |
267 | BidiEN European Number | |
268 | BidiES European Number Separator | |
269 | BidiET European Number Terminator | |
270 | BidiAN Arabic Number | |
271 | BidiCS Common Number Separator | |
272 | BidiNSM Non-Spacing Mark | |
273 | BidiBN Boundary Neutral | |
274 | BidiB Paragraph Separator | |
275 | BidiS Segment Separator | |
276 | BidiWS Whitespace | |
277 | BidiON Other Neutrals | |
32293815 | 278 | |
210b36aa AMS |
279 | =back |
280 | ||
2796c109 JH |
281 | =head2 Scripts |
282 | ||
75daf61c | 283 | The scripts available for C<\p{In...}> and C<\P{In...}>, for example |
cfc01aea | 284 | C<\p{InLatin}> or \p{InCyrillic>, are as follows: |
2796c109 | 285 | |
1ac13f9a | 286 | Arabic |
e9ad1727 | 287 | Armenian |
1ac13f9a | 288 | Bengali |
e9ad1727 JH |
289 | Bopomofo |
290 | Canadian-Aboriginal | |
291 | Cherokee | |
292 | Cyrillic | |
293 | Deseret | |
294 | Devanagari | |
295 | Ethiopic | |
296 | Georgian | |
297 | Gothic | |
298 | Greek | |
1ac13f9a | 299 | Gujarati |
e9ad1727 JH |
300 | Gurmukhi |
301 | Han | |
302 | Hangul | |
303 | Hebrew | |
304 | Hiragana | |
305 | Inherited | |
1ac13f9a | 306 | Kannada |
e9ad1727 JH |
307 | Katakana |
308 | Khmer | |
1ac13f9a | 309 | Lao |
e9ad1727 JH |
310 | Latin |
311 | Malayalam | |
312 | Mongolian | |
1ac13f9a | 313 | Myanmar |
1ac13f9a | 314 | Ogham |
e9ad1727 JH |
315 | Old-Italic |
316 | Oriya | |
1ac13f9a | 317 | Runic |
e9ad1727 JH |
318 | Sinhala |
319 | Syriac | |
320 | Tamil | |
321 | Telugu | |
322 | Thaana | |
323 | Thai | |
324 | Tibetan | |
1ac13f9a | 325 | Yi |
1ac13f9a JH |
326 | |
327 | There are also extended property classes that supplement the basic | |
328 | properties, defined by the F<PropList> Unicode database: | |
329 | ||
e9ad1727 | 330 | ASCII_Hex_Digit |
1ac13f9a | 331 | Bidi_Control |
1ac13f9a | 332 | Dash |
1ac13f9a JH |
333 | Diacritic |
334 | Extender | |
e9ad1727 JH |
335 | Hex_Digit |
336 | Hyphen | |
337 | Ideographic | |
338 | Join_Control | |
339 | Noncharacter_Code_Point | |
340 | Other_Alphabetic | |
1ac13f9a | 341 | Other_Lowercase |
e9ad1727 | 342 | Other_Math |
1ac13f9a | 343 | Other_Uppercase |
e9ad1727 | 344 | Quotation_Mark |
e150c829 | 345 | White_Space |
1ac13f9a JH |
346 | |
347 | and further derived properties: | |
348 | ||
349 | Alphabetic Lu + Ll + Lt + Lm + Lo + Other_Alphabetic | |
350 | Lowercase Ll + Other_Lowercase | |
351 | Uppercase Lu + Other_Uppercase | |
352 | Math Sm + Other_Math | |
353 | ||
354 | ID_Start Lu + Ll + Lt + Lm + Lo + Nl | |
355 | ID_Continue ID_Start + Mn + Mc + Nd + Pc | |
356 | ||
357 | Any Any character | |
358 | Assigned Any non-Cn character | |
359 | Common Any character (or unassigned code point) | |
e150c829 | 360 | not explicitly assigned to a script |
2796c109 JH |
361 | |
362 | =head2 Blocks | |
363 | ||
364 | In addition to B<scripts>, Unicode also defines B<blocks> of | |
365 | characters. The difference between scripts and blocks is that the | |
e9ad1727 | 366 | scripts concept is closer to natural languages, while the blocks |
2796c109 JH |
367 | concept is more an artificial grouping based on groups of 256 Unicode |
368 | characters. For example, the C<Latin> script contains letters from | |
e9ad1727 | 369 | many blocks. On the other hand, the C<Latin> script does not contain |
cfc01aea | 370 | all the characters from those blocks. It does not, for example, contain |
e9ad1727 JH |
371 | digits because digits are shared across many scripts. Digits and |
372 | other similar groups, like punctuation, are in a category called | |
373 | C<Common>. | |
2796c109 | 374 | |
cfc01aea JF |
375 | For more about scripts, see the UTR #24: |
376 | ||
377 | http://www.unicode.org/unicode/reports/tr24/ | |
378 | ||
379 | For more about blocks, see: | |
380 | ||
381 | http://www.unicode.org/Public/UNIDATA/Blocks.txt | |
2796c109 JH |
382 | |
383 | Because there are overlaps in naming (there are, for example, both | |
384 | a script called C<Katakana> and a block called C<Katakana>, the block | |
385 | version has C<Block> appended to its name, C<\p{InKatakanaBlock}>. | |
386 | ||
387 | Notice that this definition was introduced in Perl 5.8.0: in Perl | |
e150c829 | 388 | 5.6 only the blocks were used; in Perl 5.8.0 scripts became the |
61247495 JH |
389 | preferential Unicode character class definition; this meant that |
390 | the definitions of some character classes changed (the ones in the | |
2796c109 JH |
391 | below list that have the C<Block> appended). |
392 | ||
e9ad1727 JH |
393 | Alphabetic Presentation Forms |
394 | Arabic Block | |
395 | Arabic Presentation Forms-A | |
396 | Arabic Presentation Forms-B | |
397 | Armenian Block | |
398 | Arrows | |
71d929cb | 399 | Basic Latin |
e9ad1727 JH |
400 | Bengali Block |
401 | Block Elements | |
402 | Bopomofo Block | |
403 | Bopomofo Extended | |
404 | Box Drawing | |
405 | Braille Patterns | |
406 | Byzantine Musical Symbols | |
407 | CJK Compatibility | |
408 | CJK Compatibility Forms | |
409 | CJK Compatibility Ideographs | |
410 | CJK Compatibility Ideographs Supplement | |
411 | CJK Radicals Supplement | |
412 | CJK Symbols and Punctuation | |
413 | CJK Unified Ideographs | |
414 | CJK Unified Ideographs Extension A | |
415 | CJK Unified Ideographs Extension B | |
416 | Cherokee Block | |
71d929cb | 417 | Combining Diacritical Marks |
e9ad1727 JH |
418 | Combining Half Marks |
419 | Combining Marks for Symbols | |
420 | Control Pictures | |
421 | Currency Symbols | |
71d929cb | 422 | Cyrillic Block |
e9ad1727 | 423 | Deseret Block |
71d929cb | 424 | Devanagari Block |
e9ad1727 JH |
425 | Dingbats |
426 | Enclosed Alphanumerics | |
427 | Enclosed CJK Letters and Months | |
428 | Ethiopic Block | |
429 | General Punctuation | |
430 | Geometric Shapes | |
71d929cb | 431 | Georgian Block |
e9ad1727 JH |
432 | Gothic Block |
433 | Greek Block | |
434 | Greek Extended | |
435 | Gujarati Block | |
436 | Gurmukhi Block | |
437 | Halfwidth and Fullwidth Forms | |
438 | Hangul Compatibility Jamo | |
71d929cb | 439 | Hangul Jamo |
e9ad1727 JH |
440 | Hangul Syllables |
441 | Hebrew Block | |
442 | High Private Use Surrogates | |
443 | High Surrogates | |
444 | Hiragana Block | |
445 | IPA Extensions | |
446 | Ideographic Description Characters | |
447 | Kanbun | |
448 | Kangxi Radicals | |
449 | Kannada Block | |
450 | Katakana Block | |
71d929cb | 451 | Khmer Block |
e9ad1727 JH |
452 | Lao Block |
453 | Latin 1 Supplement | |
71d929cb | 454 | Latin Extended Additional |
e9ad1727 JH |
455 | Latin Extended-A |
456 | Latin Extended-B | |
71d929cb | 457 | Letterlike Symbols |
e9ad1727 JH |
458 | Low Surrogates |
459 | Malayalam Block | |
460 | Mathematical Alphanumeric Symbols | |
71d929cb | 461 | Mathematical Operators |
e9ad1727 | 462 | Miscellaneous Symbols |
71d929cb | 463 | Miscellaneous Technical |
e9ad1727 JH |
464 | Mongolian Block |
465 | Musical Symbols | |
466 | Myanmar Block | |
467 | Number Forms | |
468 | Ogham Block | |
469 | Old Italic Block | |
71d929cb | 470 | Optical Character Recognition |
e9ad1727 | 471 | Oriya Block |
71d929cb | 472 | Private Use |
e9ad1727 JH |
473 | Runic Block |
474 | Sinhala Block | |
71d929cb | 475 | Small Form Variants |
e9ad1727 | 476 | Spacing Modifier Letters |
2796c109 | 477 | Specials |
e9ad1727 JH |
478 | Superscripts and Subscripts |
479 | Syriac Block | |
2796c109 | 480 | Tags |
e9ad1727 JH |
481 | Tamil Block |
482 | Telugu Block | |
483 | Thaana Block | |
484 | Thai Block | |
485 | Tibetan Block | |
486 | Unified Canadian Aboriginal Syllabics | |
487 | Yi Radicals | |
488 | Yi Syllables | |
32293815 | 489 | |
210b36aa AMS |
490 | =over 4 |
491 | ||
393fec97 GS |
492 | =item * |
493 | ||
494 | The special pattern C<\X> match matches any extended Unicode sequence | |
495 | (a "combining character sequence" in Standardese), where the first | |
496 | character is a base character and subsequent characters are mark | |
497 | characters that apply to the base character. It is equivalent to | |
498 | C<(?:\PM\pM*)>. | |
499 | ||
393fec97 GS |
500 | =item * |
501 | ||
383e7cdd JH |
502 | The C<tr///> operator translates characters instead of bytes. Note |
503 | that the C<tr///CU> functionality has been removed, as the interface | |
504 | was a mistake. For similar functionality see pack('U0', ...) and | |
505 | pack('C0', ...). | |
393fec97 | 506 | |
393fec97 GS |
507 | =item * |
508 | ||
509 | Case translation operators use the Unicode case translation tables | |
44bc797b JH |
510 | when provided character input. Note that C<uc()> (also known as C<\U> |
511 | in doublequoted strings) translates to uppercase, while C<ucfirst> | |
512 | (also known as C<\u> in doublequoted strings) translates to titlecase | |
513 | (for languages that make the distinction). Naturally the | |
514 | corresponding backslash sequences have the same semantics. | |
393fec97 GS |
515 | |
516 | =item * | |
517 | ||
518 | Most operators that deal with positions or lengths in the string will | |
75daf61c JH |
519 | automatically switch to using character positions, including |
520 | C<chop()>, C<substr()>, C<pos()>, C<index()>, C<rindex()>, | |
521 | C<sprintf()>, C<write()>, and C<length()>. Operators that | |
522 | specifically don't switch include C<vec()>, C<pack()>, and | |
523 | C<unpack()>. Operators that really don't care include C<chomp()>, as | |
524 | well as any other operator that treats a string as a bucket of bits, | |
525 | such as C<sort()>, and the operators dealing with filenames. | |
393fec97 GS |
526 | |
527 | =item * | |
528 | ||
529 | The C<pack()>/C<unpack()> letters "C<c>" and "C<C>" do I<not> change, | |
530 | since they're often used for byte-oriented formats. (Again, think | |
531 | "C<char>" in the C language.) However, there is a new "C<U>" specifier | |
532 | that will convert between UTF-8 characters and integers. (It works | |
533 | outside of the utf8 pragma too.) | |
534 | ||
535 | =item * | |
536 | ||
537 | The C<chr()> and C<ord()> functions work on characters. This is like | |
538 | C<pack("U")> and C<unpack("U")>, not like C<pack("C")> and | |
539 | C<unpack("C")>. In fact, the latter are how you now emulate | |
35bcd338 JH |
540 | byte-oriented C<chr()> and C<ord()> for Unicode strings. |
541 | (Note that this reveals the internal UTF-8 encoding of strings and | |
542 | you are not supposed to do that unless you know what you are doing.) | |
393fec97 GS |
543 | |
544 | =item * | |
545 | ||
a1ca4561 YST |
546 | The bit string operators C<& | ^ ~> can operate on character data. |
547 | However, for backward compatibility reasons (bit string operations | |
75daf61c JH |
548 | when the characters all are less than 256 in ordinal value) one should |
549 | not mix C<~> (the bit complement) and characters both less than 256 and | |
a1ca4561 YST |
550 | equal or greater than 256. Most importantly, the DeMorgan's laws |
551 | (C<~($x|$y) eq ~$x&~$y>, C<~($x&$y) eq ~$x|~$y>) won't hold. | |
552 | Another way to look at this is that the complement cannot return | |
75daf61c | 553 | B<both> the 8-bit (byte) wide bit complement B<and> the full character |
a1ca4561 YST |
554 | wide bit complement. |
555 | ||
556 | =item * | |
557 | ||
983ffd37 JH |
558 | lc(), uc(), lcfirst(), and ucfirst() work for the following cases: |
559 | ||
560 | =over 8 | |
561 | ||
562 | =item * | |
563 | ||
564 | the case mapping is from a single Unicode character to another | |
565 | single Unicode character | |
566 | ||
567 | =item * | |
568 | ||
569 | the case mapping is from a single Unicode character to more | |
570 | than one Unicode character | |
571 | ||
572 | =back | |
573 | ||
210b36aa | 574 | What doesn't yet work are the following cases: |
983ffd37 JH |
575 | |
576 | =over 8 | |
577 | ||
578 | =item * | |
579 | ||
580 | the "final sigma" (Greek) | |
581 | ||
582 | =item * | |
583 | ||
584 | anything to with locales (Lithuanian, Turkish, Azeri) | |
585 | ||
586 | =back | |
587 | ||
588 | See the Unicode Technical Report #21, Case Mappings, for more details. | |
ac1256e8 JH |
589 | |
590 | =item * | |
591 | ||
393fec97 GS |
592 | And finally, C<scalar reverse()> reverses by character rather than by byte. |
593 | ||
594 | =back | |
595 | ||
8cbd9a7a GS |
596 | =head2 Character encodings for input and output |
597 | ||
7221edc9 | 598 | See L<Encode>. |
8cbd9a7a | 599 | |
393fec97 GS |
600 | =head1 CAVEATS |
601 | ||
602 | As of yet, there is no method for automatically coercing input and | |
b3419ed8 PK |
603 | output to some encoding other than UTF-8 or UTF-EBCDIC. This is planned |
604 | in the near future, however. | |
393fec97 | 605 | |
8cbd9a7a GS |
606 | Whether an arbitrary piece of data will be treated as "characters" or |
607 | "bytes" by internal operations cannot be divined at the current time. | |
393fec97 GS |
608 | |
609 | Use of locales with utf8 may lead to odd results. Currently there is | |
610 | some attempt to apply 8-bit locale info to characters in the range | |
611 | 0..255, but this is demonstrably incorrect for locales that use | |
612 | characters above that range (when mapped into Unicode). It will also | |
613 | tend to run slower. Avoidance of locales is strongly encouraged. | |
614 | ||
776f8809 JH |
615 | =head1 UNICODE REGULAR EXPRESSION SUPPORT LEVEL |
616 | ||
617 | The following list of Unicode regular expression support describes | |
618 | feature by feature the Unicode support implemented in Perl as of Perl | |
619 | 5.8.0. The "Level N" and the section numbers refer to the Unicode | |
620 | Technical Report 18, "Unicode Regular Expression Guidelines". | |
621 | ||
622 | =over 4 | |
623 | ||
624 | =item * | |
625 | ||
626 | Level 1 - Basic Unicode Support | |
627 | ||
628 | 2.1 Hex Notation - done [1] | |
629 | Named Notation - done [2] | |
630 | 2.2 Categories - done [3][4] | |
631 | 2.3 Subtraction - MISSING [5][6] | |
632 | 2.4 Simple Word Boundaries - done [7] | |
90a59240 | 633 | 2.5 Simple Loose Matches - done [8] |
776f8809 JH |
634 | 2.6 End of Line - MISSING [9][10] |
635 | ||
636 | [ 1] \x{...} | |
637 | [ 2] \N{...} | |
638 | [ 3] . \p{Is...} \P{Is...} | |
29bdacb8 | 639 | [ 4] now scripts (see UTR#24 Script Names) in addition to blocks |
776f8809 | 640 | [ 5] have negation |
29bdacb8 | 641 | [ 6] can use look-ahead to emulate subtraction (*) |
776f8809 | 642 | [ 7] include Letters in word characters |
90a59240 | 643 | [ 8] see UTR#21 Case Mappings: Perl implements 1:1 mappings |
776f8809 | 644 | [ 9] see UTR#13 Unicode Newline Guidelines |
ec83e909 JH |
645 | [10] should do ^ and $ also on \x{85}, \x{2028} and \x{2029}) |
646 | (should also affect <>, $., and script line numbers) | |
647 | ||
29bdacb8 JH |
648 | (*) Instead of [\u0370-\u03FF-[{UNASSIGNED}]] as suggested by the TR |
649 | 18 you can use negated lookahead: to match currently assigned modern | |
650 | Greek characters use for example | |
651 | ||
652 | /(?!\p{Cn})[\x{0370}-\x{03ff}]/ | |
653 | ||
654 | In other words: the matched character must not be a non-assigned | |
655 | character, but it must be in the block of modern Greek characters. | |
656 | ||
776f8809 JH |
657 | =item * |
658 | ||
659 | Level 2 - Extended Unicode Support | |
660 | ||
661 | 3.1 Surrogates - MISSING | |
662 | 3.2 Canonical Equivalents - MISSING [11][12] | |
663 | 3.3 Locale-Independent Graphemes - MISSING [13] | |
664 | 3.4 Locale-Independent Words - MISSING [14] | |
665 | 3.5 Locale-Independent Loose Matches - MISSING [15] | |
666 | ||
667 | [11] see UTR#15 Unicode Normalization | |
668 | [12] have Unicode::Normalize but not integrated to regexes | |
669 | [13] have \X but at this level . should equal that | |
670 | [14] need three classes, not just \w and \W | |
671 | [15] see UTR#21 Case Mappings | |
672 | ||
673 | =item * | |
674 | ||
675 | Level 3 - Locale-Sensitive Support | |
676 | ||
677 | 4.1 Locale-Dependent Categories - MISSING | |
678 | 4.2 Locale-Dependent Graphemes - MISSING [16][17] | |
679 | 4.3 Locale-Dependent Words - MISSING | |
680 | 4.4 Locale-Dependent Loose Matches - MISSING | |
681 | 4.5 Locale-Dependent Ranges - MISSING | |
682 | ||
683 | [16] see UTR#10 Unicode Collation Algorithms | |
684 | [17] have Unicode::Collate but not integrated to regexes | |
685 | ||
686 | =back | |
687 | ||
c349b1b9 JH |
688 | =head2 Unicode Encodings |
689 | ||
690 | Unicode characters are assigned to I<code points> which are abstract | |
86bbd6d1 | 691 | numbers. To use these numbers various encodings are needed. |
c349b1b9 JH |
692 | |
693 | =over 4 | |
694 | ||
695 | =item UTF-8 | |
696 | ||
86bbd6d1 | 697 | UTF-8 is the encoding used internally by Perl. UTF-8 is a variable |
c349b1b9 | 698 | length (1 to 6 bytes, current character allocations require 4 bytes), |
86bbd6d1 PN |
699 | byteorder independent encoding. For ASCII, UTF-8 is transparent |
700 | (and we really do mean 7-bit ASCII, not any 8-bit encoding). | |
c349b1b9 | 701 | |
05632f9a JH |
702 | The following table is from Unicode 3.1. |
703 | ||
704 | Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte | |
705 | ||
706 | U+0000..U+007F 00..7F | |
707 | U+0080..U+07FF C2..DF 80..BF | |
708 | U+0800..U+0FFF E0 A0..BF 80..BF | |
709 | U+1000..U+FFFF E1..EF 80..BF 80..BF | |
710 | U+10000..U+3FFFF F0 90..BF 80..BF 80..BF | |
711 | U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF | |
712 | U+100000..U+10FFFF F4 80..8F 80..BF 80..BF | |
713 | ||
714 | Or, another way to look at it, as bits: | |
715 | ||
716 | Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte | |
717 | ||
718 | 0aaaaaaa 0aaaaaaa | |
719 | 00000bbbbbaaaaaa 110bbbbb 10aaaaaa | |
720 | ccccbbbbbbaaaaaa 1110cccc 10bbbbbb 10aaaaaa | |
721 | 00000dddccccccbbbbbbaaaaaa 11110ddd 10cccccc 10bbbbbb 10aaaaaa | |
722 | ||
723 | As you can see, the continuation bytes all begin with C<10>, and the | |
724 | leading bits of the start byte tells how many bytes the are in the | |
725 | encoded character. | |
726 | ||
c349b1b9 JH |
727 | =item UTF-16, UTF-16BE, UTF16-LE, Surrogates, and BOMs (Byte Order Marks) |
728 | ||
729 | UTF-16 is a 2 or 4 byte encoding. The Unicode code points | |
730 | 0x0000..0xFFFF are stored in two 16-bit units, and the code points | |
731 | 0x010000..0x10FFFF in four 16-bit units. The latter case is | |
732 | using I<surrogates>, the first 16-bit unit being the I<high | |
733 | surrogate>, and the second being the I<low surrogate>. | |
734 | ||
735 | Surrogates are code points set aside to encode the 0x01000..0x10FFFF | |
736 | range of Unicode code points in pairs of 16-bit units. The I<high | |
737 | surrogates> are the range 0xD800..0xDBFF, and the I<low surrogates> | |
738 | are the range 0xDC00..0xDFFFF. The surrogate encoding is | |
739 | ||
740 | $hi = ($uni - 0x10000) / 0x400 + 0xD800; | |
741 | $lo = ($uni - 0x10000) % 0x400 + 0xDC00; | |
742 | ||
743 | and the decoding is | |
744 | ||
745 | $uni = 0x10000 + ($hi - 0xD8000) * 0x400 + ($lo - 0xDC00); | |
746 | ||
9466bab6 JH |
747 | If you try to generate surrogates (for example by using chr()), you |
748 | will get an error because firstly a surrogate on its own is | |
749 | meaningless, and secondly because Perl encodes its Unicode characters | |
750 | in UTF-8 (not 16-bit numbers), which makes the encoded character doubly | |
751 | illegal. | |
752 | ||
86bbd6d1 | 753 | Because of the 16-bitness, UTF-16 is byteorder dependent. UTF-16 |
c349b1b9 | 754 | itself can be used for in-memory computations, but if storage or |
86bbd6d1 | 755 | transfer is required, either UTF-16BE (Big Endian) or UTF-16LE |
c349b1b9 JH |
756 | (Little Endian) must be chosen. |
757 | ||
758 | This introduces another problem: what if you just know that your data | |
759 | is UTF-16, but you don't know which endianness? Byte Order Marks | |
760 | (BOMs) are a solution to this. A special character has been reserved | |
86bbd6d1 PN |
761 | in Unicode to function as a byte order marker: the character with the |
762 | code point 0xFEFF is the BOM. | |
042da322 | 763 | |
c349b1b9 JH |
764 | The trick is that if you read a BOM, you will know the byte order, |
765 | since if it was written on a big endian platform, you will read the | |
86bbd6d1 PN |
766 | bytes 0xFE 0xFF, but if it was written on a little endian platform, |
767 | you will read the bytes 0xFF 0xFE. (And if the originating platform | |
768 | was writing in UTF-8, you will read the bytes 0xEF 0xBB 0xBF.) | |
042da322 | 769 | |
86bbd6d1 PN |
770 | The way this trick works is that the character with the code point |
771 | 0xFFFE is guaranteed not to be a valid Unicode character, so the | |
772 | sequence of bytes 0xFF 0xFE is unambiguously "BOM, represented in | |
042da322 JH |
773 | little-endian format" and cannot be "0xFFFE, represented in big-endian |
774 | format". | |
c349b1b9 JH |
775 | |
776 | =item UTF-32, UTF-32BE, UTF32-LE | |
777 | ||
778 | The UTF-32 family is pretty much like the UTF-16 family, expect that | |
042da322 JH |
779 | the units are 32-bit, and therefore the surrogate scheme is not |
780 | needed. The BOM signatures will be 0x00 0x00 0xFE 0xFF for BE and | |
781 | 0xFF 0xFE 0x00 0x00 for LE. | |
c349b1b9 JH |
782 | |
783 | =item UCS-2, UCS-4 | |
784 | ||
86bbd6d1 PN |
785 | Encodings defined by the ISO 10646 standard. UCS-2 is a 16-bit |
786 | encoding, UCS-4 is a 32-bit encoding. Unlike UTF-16, UCS-2 | |
787 | is not extensible beyond 0xFFFF, because it does not use surrogates. | |
c349b1b9 JH |
788 | |
789 | =item UTF-7 | |
790 | ||
791 | A seven-bit safe (non-eight-bit) encoding, useful if the | |
792 | transport/storage is not eight-bit safe. Defined by RFC 2152. | |
793 | ||
95a1a48b JH |
794 | =back |
795 | ||
bf0fa0b2 JH |
796 | =head2 Security Implications of Malformed UTF-8 |
797 | ||
798 | Unfortunately, the specification of UTF-8 leaves some room for | |
799 | interpretation of how many bytes of encoded output one should generate | |
800 | from one input Unicode character. Strictly speaking, one is supposed | |
801 | to always generate the shortest possible sequence of UTF-8 bytes, | |
802 | because otherwise there is potential for input buffer overflow at the | |
803 | receiving end of a UTF-8 connection. Perl always generates the shortest | |
804 | length UTF-8, and with warnings on (C<-w> or C<use warnings;>) Perl will | |
805 | warn about non-shortest length UTF-8 (and other malformations, too, | |
806 | such as the surrogates, which are not real character code points.) | |
807 | ||
c349b1b9 JH |
808 | =head2 Unicode in Perl on EBCDIC |
809 | ||
810 | The way Unicode is handled on EBCDIC platforms is still rather | |
86bbd6d1 | 811 | experimental. On such a platform, references to UTF-8 encoding in this |
c349b1b9 JH |
812 | document and elsewhere should be read as meaning UTF-EBCDIC as |
813 | specified in Unicode Technical Report 16 unless ASCII vs EBCDIC issues | |
814 | are specifically discussed. There is no C<utfebcdic> pragma or | |
86bbd6d1 PN |
815 | ":utfebcdic" layer, rather, "utf8" and ":utf8" are re-used to mean |
816 | the platform's "natural" 8-bit encoding of Unicode. See L<perlebcdic> | |
817 | for more discussion of the issues. | |
c349b1b9 | 818 | |
95a1a48b JH |
819 | =head2 Using Unicode in XS |
820 | ||
821 | If you want to handle Perl Unicode in XS extensions, you may find | |
822 | the following C APIs useful: | |
823 | ||
824 | =over 4 | |
825 | ||
826 | =item * | |
827 | ||
828 | DO_UTF8(sv) returns true if the UTF8 flag is on and the bytes | |
829 | pragma is not in effect. SvUTF8(sv) returns true is the UTF8 | |
830 | flag is on, the bytes pragma is ignored. Remember that UTF8 | |
831 | flag being on does not mean that there would be any characters | |
832 | of code points greater than 255 or 127 in the scalar, or that | |
833 | there even are any characters in the scalar. The UTF8 flag | |
834 | means that any characters added to the string will be encoded | |
835 | in UTF8 if the code points of the characters are greater than | |
836 | 255. Not "if greater than 127", since Perl's Unicode model | |
837 | is not to use UTF-8 until it's really necessary. | |
838 | ||
839 | =item * | |
840 | ||
841 | uvuni_to_utf8(buf, chr) writes a Unicode character code point into a | |
cfc01aea | 842 | buffer encoding the code point as UTF-8, and returns a pointer |
95a1a48b JH |
843 | pointing after the UTF-8 bytes. |
844 | ||
845 | =item * | |
846 | ||
847 | utf8_to_uvuni(buf, lenp) reads UTF-8 encoded bytes from a buffer and | |
848 | returns the Unicode character code point (and optionally the length of | |
849 | the UTF-8 byte sequence). | |
850 | ||
851 | =item * | |
852 | ||
853 | utf8_length(s, len) returns the length of the UTF-8 encoded buffer in | |
854 | characters. sv_len_utf8(sv) returns the length of the UTF-8 encoded | |
855 | scalar. | |
856 | ||
857 | =item * | |
858 | ||
859 | sv_utf8_upgrade(sv) converts the string of the scalar to its UTF-8 | |
860 | encoded form. sv_utf8_downgrade(sv) does the opposite (if possible). | |
861 | sv_utf8_encode(sv) is like sv_utf8_upgrade but the UTF8 flag does not | |
862 | get turned on. sv_utf8_decode() does the opposite of sv_utf8_encode(). | |
863 | ||
864 | =item * | |
865 | ||
866 | is_utf8_char(buf) returns true if the buffer points to valid UTF-8. | |
867 | ||
868 | =item * | |
869 | ||
870 | is_utf8_string(buf, len) returns true if the len bytes of the buffer | |
871 | are valid UTF-8. | |
872 | ||
873 | =item * | |
874 | ||
875 | UTF8SKIP(buf) will return the number of bytes in the UTF-8 encoded | |
876 | character in the buffer. UNISKIP(chr) will return the number of bytes | |
877 | required to UTF-8-encode the Unicode character code point. | |
878 | ||
879 | =item * | |
880 | ||
881 | utf8_distance(a, b) will tell the distance in characters between the | |
882 | two pointers pointing to the same UTF-8 encoded buffer. | |
883 | ||
884 | =item * | |
885 | ||
886 | utf8_hop(s, off) will return a pointer to an UTF-8 encoded buffer that | |
887 | is C<off> (positive or negative) Unicode characters displaced from the | |
888 | UTF-8 buffer C<s>. | |
889 | ||
d2cc3551 JH |
890 | =item * |
891 | ||
892 | pv_uni_display(dsv, spv, len, pvlim, flags) and sv_uni_display(dsv, | |
893 | ssv, pvlim, flags) are useful for debug output of Unicode strings and | |
894 | scalars (only for debug: they display B<all> characters as hexadecimal | |
895 | code points). | |
896 | ||
897 | =item * | |
898 | ||
332ddc25 JH |
899 | ibcmp_utf8(s1, u1, len1, s2, u2, len2) can be used to compare two |
900 | strings case-insensitively in Unicode. (For case-sensitive | |
901 | comparisons you can just use memEQ() and memNE() as usual.) | |
d2cc3551 | 902 | |
c349b1b9 JH |
903 | =back |
904 | ||
95a1a48b JH |
905 | For more information, see L<perlapi>, and F<utf8.c> and F<utf8.h> |
906 | in the Perl source code distribution. | |
907 | ||
393fec97 GS |
908 | =head1 SEE ALSO |
909 | ||
72ff2908 JH |
910 | L<perluniintro>, L<encoding>, L<Encode>, L<open>, L<utf8>, L<bytes>, |
911 | L<perlretut>, L<perlvar/"${^WIDE_SYSTEM_CALLS}"> | |
393fec97 GS |
912 | |
913 | =cut |