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