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 | |
0a1f2d14 |
9 | WARNING: While the implementation of Unicode support in Perl is now fairly |
10 | complete it is still evolving to some extent. |
21bad921 |
11 | |
0a1f2d14 |
12 | In particular the way Unicode is handled on EBCDIC platforms is still rather |
13 | experimental. On such a platform references to UTF-8 encoding in this |
14 | document and elsewhere should be read as meaning UTF-EBCDIC as specified |
15 | in Unicode Technical Report 16 unless ASCII vs EBCDIC issues are specifically |
16 | discussed. There is no C<utfebcdic> pragma or ":utfebcdic" layer, rather |
17 | "utf8" and ":utf8" are re-used to mean platform's "natural" 8-bit encoding |
18 | of Unicode. See L<perlebcdic> for more discussion of the issues. |
19 | |
20 | The following areas are still under development. |
21bad921 |
21 | |
13a2d996 |
22 | =over 4 |
21bad921 |
23 | |
24 | =item Input and Output Disciplines |
25 | |
0a1f2d14 |
26 | A filehandle can be marked as containing perl's internal Unicode encoding |
27 | (UTF-8 or UTF-EBCDIC) by opening it with the ":utf8" layer. |
28 | Other encodings can be converted to perl's encoding on input, or from |
29 | perl's encoding on output by use of the ":encoding()" layer. |
30 | There is not yet a clean way to mark the perl source itself as being |
31 | in an particular encoding. |
21bad921 |
32 | |
33 | =item Regular Expressions |
34 | |
e6739005 |
35 | The regular expression compiler does now attempt to produce |
36 | polymorphic opcodes. That is the pattern should now adapt to the data |
37 | and automatically switch to the Unicode character scheme when presented |
38 | with Unicode data, or a traditional byte scheme when presented with |
39 | byte data. The implementation is still new and (particularly on |
40 | EBCDIC platforms) may need further work. |
21bad921 |
41 | |
42 | =item C<use utf8> still needed to enable a few features |
43 | |
44 | The C<utf8> pragma implements the tables used for Unicode support. These |
45 | tables are automatically loaded on demand, so the C<utf8> pragma need not |
46 | normally be used. |
47 | |
48 | However, as a compatibility measure, this pragma must be explicitly used |
49 | to enable recognition of UTF-8 encoded literals and identifiers in the |
b3419ed8 |
50 | source text on ASCII based machines or recognize UTF-EBCDIC encoded literals |
51 | and identifiers on EBCDIC based machines. |
21bad921 |
52 | |
53 | =back |
54 | |
55 | =head2 Byte and Character semantics |
393fec97 |
56 | |
57 | Beginning with version 5.6, Perl uses logically wide characters to |
58 | represent strings internally. This internal representation of strings |
b3419ed8 |
59 | uses either the UTF-8 or the UTF-EBCDIC encoding. |
393fec97 |
60 | |
21bad921 |
61 | In future, Perl-level operations can be expected to work with characters |
393fec97 |
62 | rather than bytes, in general. |
63 | |
8cbd9a7a |
64 | However, as strictly an interim compatibility measure, Perl v5.6 aims to |
65 | provide a safe migration path from byte semantics to character semantics |
66 | for programs. For operations where Perl can unambiguously decide that the |
67 | input data is characters, Perl now switches to character semantics. |
68 | For operations where this determination cannot be made without additional |
69 | information from the user, Perl decides in favor of compatibility, and |
70 | chooses to use byte semantics. |
71 | |
72 | This behavior preserves compatibility with earlier versions of Perl, |
73 | which allowed byte semantics in Perl operations, but only as long as |
74 | none of the program's inputs are marked as being as source of Unicode |
75 | character data. Such data may come from filehandles, from calls to |
76 | external programs, from information provided by the system (such as %ENV), |
21bad921 |
77 | or from literals and constants in the source text. |
8cbd9a7a |
78 | |
46487f74 |
79 | If the C<-C> command line switch is used, (or the ${^WIDE_SYSTEM_CALLS} |
80 | global flag is set to C<1>), all system calls will use the |
3969a896 |
81 | corresponding wide character APIs. This is currently only implemented |
e6739005 |
82 | on Windows since UNIXes lack API standard on this area. |
8cbd9a7a |
83 | |
8058d7ab |
84 | Regardless of the above, the C<bytes> pragma can always be used to force |
85 | byte semantics in a particular lexical scope. See L<bytes>. |
8cbd9a7a |
86 | |
87 | The C<utf8> pragma is primarily a compatibility device that enables |
b3419ed8 |
88 | recognition of UTF-(8|EBCDIC) in literals encountered by the parser. It may also |
21bad921 |
89 | be used for enabling some of the more experimental Unicode support features. |
8cbd9a7a |
90 | Note that this pragma is only required until a future version of Perl |
91 | in which character semantics will become the default. This pragma may |
92 | then become a no-op. See L<utf8>. |
93 | |
94 | Unless mentioned otherwise, Perl operators will use character semantics |
95 | when they are dealing with Unicode data, and byte semantics otherwise. |
96 | Thus, character semantics for these operations apply transparently; if |
97 | the input data came from a Unicode source (for example, by adding a |
98 | character encoding discipline to the filehandle whence it came, or a |
99 | literal UTF-8 string constant in the program), character semantics |
100 | apply; otherwise, byte semantics are in effect. To force byte semantics |
8058d7ab |
101 | on Unicode data, the C<bytes> pragma should be used. |
393fec97 |
102 | |
103 | Under character semantics, many operations that formerly operated on |
104 | bytes change to operating on characters. For ASCII data this makes |
105 | no difference, because UTF-8 stores ASCII in single bytes, but for |
21bad921 |
106 | any character greater than C<chr(127)>, the character may be stored in |
393fec97 |
107 | a sequence of two or more bytes, all of which have the high bit set. |
b3419ed8 |
108 | For C1 controls or Latin 1 characters on an EBCDIC platform the character |
109 | may be stored in a UTF-EBCDIC multi byte sequence. |
393fec97 |
110 | But by and large, the user need not worry about this, because Perl |
111 | hides it from the user. A character in Perl is logically just a number |
112 | ranging from 0 to 2**32 or so. Larger characters encode to longer |
113 | sequences of bytes internally, but again, this is just an internal |
114 | detail which is hidden at the Perl level. |
115 | |
8cbd9a7a |
116 | =head2 Effects of character semantics |
393fec97 |
117 | |
118 | Character semantics have the following effects: |
119 | |
120 | =over 4 |
121 | |
122 | =item * |
123 | |
124 | Strings and patterns may contain characters that have an ordinal value |
21bad921 |
125 | larger than 255. |
393fec97 |
126 | |
127 | Presuming you use a Unicode editor to edit your program, such characters |
b3419ed8 |
128 | will typically occur directly within the literal strings as UTF-(8|EBCDIC) |
393fec97 |
129 | characters, but you can also specify a particular character with an |
b3419ed8 |
130 | extension of the C<\x> notation. UTF-X characters are specified by |
393fec97 |
131 | putting the hexadecimal code within curlies after the C<\x>. For instance, |
4375e838 |
132 | a Unicode smiley face is C<\x{263A}>. |
393fec97 |
133 | |
134 | =item * |
135 | |
136 | Identifiers within the Perl script may contain Unicode alphanumeric |
137 | characters, including ideographs. (You are currently on your own when |
138 | it comes to using the canonical forms of characters--Perl doesn't (yet) |
139 | attempt to canonicalize variable names for you.) |
140 | |
393fec97 |
141 | =item * |
142 | |
143 | Regular expressions match characters instead of bytes. For instance, |
144 | "." matches a character instead of a byte. (However, the C<\C> pattern |
945c54fd |
145 | is provided to force a match a single byte ("C<char>" in C, hence |
146 | C<\C>).) |
393fec97 |
147 | |
393fec97 |
148 | =item * |
149 | |
150 | Character classes in regular expressions match characters instead of |
151 | bytes, and match against the character properties specified in the |
152 | Unicode properties database. So C<\w> can be used to match an ideograph, |
153 | for instance. |
154 | |
393fec97 |
155 | =item * |
156 | |
157 | Named Unicode properties and block ranges make be used as character |
158 | classes via the new C<\p{}> (matches property) and C<\P{}> (doesn't |
159 | match property) constructs. For instance, C<\p{Lu}> matches any |
160 | character with the Unicode uppercase property, while C<\p{M}> matches |
9fdf68be |
161 | any mark character. Single letter properties may omit the brackets, |
162 | so that can be written C<\pM> also. Many predefined character classes |
163 | are available, such as C<\p{IsMirrored}> and C<\p{InTibetan}>. The |
164 | names of the C<In> classes are the official Unicode block names but |
165 | with all non-alphanumeric characters removed, for example the block |
166 | name C<"Latin-1 Supplement"> becomes C<\p{InLatin1Supplement}>. |
393fec97 |
167 | |
32293815 |
168 | Here is the list as of Unicode 3.1.0 (the two-letter classes) and |
169 | Perl 5.8.0 (the one-letter classes): |
170 | |
171 | L Letter |
172 | Lu Letter, Uppercase |
173 | Ll Letter, Lowercase |
174 | Lt Letter, Titlecase |
175 | Lm Letter, Modifier |
176 | Lo Letter, Other |
177 | M Mark |
178 | Mn Mark, Non-Spacing |
179 | Mc Mark, Spacing Combining |
180 | Me Mark, Enclosing |
181 | N Number |
182 | Nd Number, Decimal Digit |
183 | Nl Number, Letter |
184 | No Number, Other |
185 | P Punctuation |
186 | Pc Punctuation, Connector |
187 | Pd Punctuation, Dash |
188 | Ps Punctuation, Open |
189 | Pe Punctuation, Close |
190 | Pi Punctuation, Initial quote |
191 | (may behave like Ps or Pe depending on usage) |
192 | Pf Punctuation, Final quote |
193 | (may behave like Ps or Pe depending on usage) |
194 | Po Punctuation, Other |
195 | S Symbol |
196 | Sm Symbol, Math |
197 | Sc Symbol, Currency |
198 | Sk Symbol, Modifier |
199 | So Symbol, Other |
200 | Z Separator |
201 | Zs Separator, Space |
202 | Zl Separator, Line |
203 | Zp Separator, Paragraph |
204 | C Other |
205 | Cc Other, Control |
206 | Cf Other, Format |
207 | Cs Other, Surrogate |
208 | Co Other, Private Use |
209 | Cn Other, Not Assigned (Unicode defines no Cn characters) |
210 | |
211 | Additionally, because scripts differ in their directionality |
212 | (for example Hebrew is written right to left), all characters |
213 | have their directionality defined: |
214 | |
215 | BidiL Left-to-Right |
216 | BidiLRE Left-to-Right Embedding |
217 | BidiLRO Left-to-Right Override |
218 | BidiR Right-to-Left |
219 | BidiAL Right-to-Left Arabic |
220 | BidiRLE Right-to-Left Embedding |
221 | BidiRLO Right-to-Left Override |
222 | BidiPDF Pop Directional Format |
223 | BidiEN European Number |
224 | BidiES European Number Separator |
225 | BidiET European Number Terminator |
226 | BidiAN Arabic Number |
227 | BidiCS Common Number Separator |
228 | BidiNSM Non-Spacing Mark |
229 | BidiBN Boundary Neutral |
230 | BidiB Paragraph Separator |
231 | BidiS Segment Separator |
232 | BidiWS Whitespace |
233 | BidiON Other Neutrals |
234 | |
235 | The blocks available for C<\p{InBlock}> and C<\P{InBlock}>, for |
236 | example \p{InCyrillic>, are as follows: |
237 | |
238 | BasicLatin |
239 | Latin1Supplement |
240 | LatinExtendedA |
241 | LatinExtendedB |
242 | IPAExtensions |
243 | SpacingModifierLetters |
244 | CombiningDiacriticalMarks |
245 | Greek |
246 | Cyrillic |
247 | Armenian |
248 | Hebrew |
249 | Arabic |
250 | Syriac |
251 | Thaana |
252 | Devanagari |
253 | Bengali |
254 | Gurmukhi |
255 | Gujarati |
256 | Oriya |
257 | Tamil |
258 | Telugu |
259 | Kannada |
260 | Malayalam |
261 | Sinhala |
262 | Thai |
263 | Lao |
264 | Tibetan |
265 | Myanmar |
266 | Georgian |
267 | HangulJamo |
268 | Ethiopic |
269 | Cherokee |
270 | UnifiedCanadianAboriginalSyllabics |
271 | Ogham |
272 | Runic |
273 | Khmer |
274 | Mongolian |
275 | LatinExtendedAdditional |
276 | GreekExtended |
277 | GeneralPunctuation |
278 | SuperscriptsandSubscripts |
279 | CurrencySymbols |
280 | CombiningMarksforSymbols |
281 | LetterlikeSymbols |
282 | NumberForms |
283 | Arrows |
284 | MathematicalOperators |
285 | MiscellaneousTechnical |
286 | ControlPictures |
287 | OpticalCharacterRecognition |
288 | EnclosedAlphanumerics |
289 | BoxDrawing |
290 | BlockElements |
291 | GeometricShapes |
292 | MiscellaneousSymbols |
293 | Dingbats |
294 | BraillePatterns |
295 | CJKRadicalsSupplement |
296 | KangxiRadicals |
297 | IdeographicDescriptionCharacters |
298 | CJKSymbolsandPunctuation |
299 | Hiragana |
300 | Katakana |
301 | Bopomofo |
302 | HangulCompatibilityJamo |
303 | Kanbun |
304 | BopomofoExtended |
305 | EnclosedCJKLettersandMonths |
306 | CJKCompatibility |
307 | CJKUnifiedIdeographsExtensionA |
308 | CJKUnifiedIdeographs |
309 | YiSyllables |
310 | YiRadicals |
311 | HangulSyllables |
312 | HighSurrogates |
313 | HighPrivateUseSurrogates |
314 | LowSurrogates |
315 | PrivateUse |
316 | CJKCompatibilityIdeographs |
317 | AlphabeticPresentationForms |
318 | ArabicPresentationFormsA |
319 | CombiningHalfMarks |
320 | CJKCompatibilityForms |
321 | SmallFormVariants |
322 | ArabicPresentationFormsB |
323 | Specials |
324 | HalfwidthandFullwidthForms |
325 | OldItalic |
326 | Gothic |
327 | Deseret |
328 | ByzantineMusicalSymbols |
329 | MusicalSymbols |
330 | MathematicalAlphanumericSymbols |
331 | CJKUnifiedIdeographsExtensionB |
332 | CJKCompatibilityIdeographsSupplement |
333 | Tags |
334 | |
393fec97 |
335 | =item * |
336 | |
337 | The special pattern C<\X> match matches any extended Unicode sequence |
338 | (a "combining character sequence" in Standardese), where the first |
339 | character is a base character and subsequent characters are mark |
340 | characters that apply to the base character. It is equivalent to |
341 | C<(?:\PM\pM*)>. |
342 | |
393fec97 |
343 | =item * |
344 | |
383e7cdd |
345 | The C<tr///> operator translates characters instead of bytes. Note |
346 | that the C<tr///CU> functionality has been removed, as the interface |
347 | was a mistake. For similar functionality see pack('U0', ...) and |
348 | pack('C0', ...). |
393fec97 |
349 | |
393fec97 |
350 | =item * |
351 | |
352 | Case translation operators use the Unicode case translation tables |
353 | when provided character input. Note that C<uc()> translates to |
354 | uppercase, while C<ucfirst> translates to titlecase (for languages |
355 | that make the distinction). Naturally the corresponding backslash |
356 | sequences have the same semantics. |
357 | |
358 | =item * |
359 | |
360 | Most operators that deal with positions or lengths in the string will |
361 | automatically switch to using character positions, including C<chop()>, |
362 | C<substr()>, C<pos()>, C<index()>, C<rindex()>, C<sprintf()>, |
363 | C<write()>, and C<length()>. Operators that specifically don't switch |
364 | include C<vec()>, C<pack()>, and C<unpack()>. Operators that really |
365 | don't care include C<chomp()>, as well as any other operator that |
366 | treats a string as a bucket of bits, such as C<sort()>, and the |
367 | operators dealing with filenames. |
368 | |
369 | =item * |
370 | |
371 | The C<pack()>/C<unpack()> letters "C<c>" and "C<C>" do I<not> change, |
372 | since they're often used for byte-oriented formats. (Again, think |
373 | "C<char>" in the C language.) However, there is a new "C<U>" specifier |
374 | that will convert between UTF-8 characters and integers. (It works |
375 | outside of the utf8 pragma too.) |
376 | |
377 | =item * |
378 | |
379 | The C<chr()> and C<ord()> functions work on characters. This is like |
380 | C<pack("U")> and C<unpack("U")>, not like C<pack("C")> and |
381 | C<unpack("C")>. In fact, the latter are how you now emulate |
382 | byte-oriented C<chr()> and C<ord()> under utf8. |
383 | |
384 | =item * |
385 | |
a1ca4561 |
386 | The bit string operators C<& | ^ ~> can operate on character data. |
387 | However, for backward compatibility reasons (bit string operations |
388 | when the characters all are less than 256 in ordinal value) one cannot |
389 | mix C<~> (the bit complement) and characters both less than 256 and |
390 | equal or greater than 256. Most importantly, the DeMorgan's laws |
391 | (C<~($x|$y) eq ~$x&~$y>, C<~($x&$y) eq ~$x|~$y>) won't hold. |
392 | Another way to look at this is that the complement cannot return |
393 | B<both> the 8-bit (byte) wide bit complement, and the full character |
394 | wide bit complement. |
395 | |
396 | =item * |
397 | |
393fec97 |
398 | And finally, C<scalar reverse()> reverses by character rather than by byte. |
399 | |
400 | =back |
401 | |
8cbd9a7a |
402 | =head2 Character encodings for input and output |
403 | |
7221edc9 |
404 | See L<Encode>. |
8cbd9a7a |
405 | |
393fec97 |
406 | =head1 CAVEATS |
407 | |
408 | As of yet, there is no method for automatically coercing input and |
b3419ed8 |
409 | output to some encoding other than UTF-8 or UTF-EBCDIC. This is planned |
410 | in the near future, however. |
393fec97 |
411 | |
8cbd9a7a |
412 | Whether an arbitrary piece of data will be treated as "characters" or |
413 | "bytes" by internal operations cannot be divined at the current time. |
393fec97 |
414 | |
415 | Use of locales with utf8 may lead to odd results. Currently there is |
416 | some attempt to apply 8-bit locale info to characters in the range |
417 | 0..255, but this is demonstrably incorrect for locales that use |
418 | characters above that range (when mapped into Unicode). It will also |
419 | tend to run slower. Avoidance of locales is strongly encouraged. |
420 | |
421 | =head1 SEE ALSO |
422 | |
32293815 |
423 | L<bytes>, L<utf8>, L<perlretut>, L<perlvar/"${^WIDE_SYSTEM_CALLS}"> |
393fec97 |
424 | |
425 | =cut |