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