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