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 | |
c349b1b9 |
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 |
14 | |
15 | =item Input and Output Disciplines |
16 | |
75daf61c |
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 |
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 |
25 | |
26 | =item Regular Expressions |
27 | |
c349b1b9 |
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 | |
c349b1b9 |
35 | As a compatibility measure, this pragma must be explicitly used to |
36 | enable recognition of UTF-8 in the Perl scripts themselves on ASCII |
3e4dbfed |
37 | based machines, or to recognize UTF-EBCDIC on EBCDIC based machines. |
c349b1b9 |
38 | B<NOTE: this should be the only place where an explicit C<use utf8> |
39 | is needed>. |
21bad921 |
40 | |
1768d7eb |
41 | You can also use the C<encoding> pragma to change the default encoding |
6ec9efec |
42 | of the data in your script; see L<encoding>. |
1768d7eb |
43 | |
21bad921 |
44 | =back |
45 | |
46 | =head2 Byte and Character semantics |
393fec97 |
47 | |
48 | Beginning with version 5.6, Perl uses logically wide characters to |
3e4dbfed |
49 | represent strings internally. |
393fec97 |
50 | |
75daf61c |
51 | In future, Perl-level operations can be expected to work with |
52 | characters rather than bytes, in general. |
393fec97 |
53 | |
75daf61c |
54 | However, as strictly an interim compatibility measure, Perl aims to |
55 | provide a safe migration path from byte semantics to character |
56 | semantics for programs. For operations where Perl can unambiguously |
57 | decide that the input data is characters, Perl now switches to |
58 | character semantics. For operations where this determination cannot |
59 | be made without additional information from the user, Perl decides in |
60 | favor of compatibility, and chooses to use byte semantics. |
8cbd9a7a |
61 | |
62 | This behavior preserves compatibility with earlier versions of Perl, |
63 | which allowed byte semantics in Perl operations, but only as long as |
64 | none of the program's inputs are marked as being as source of Unicode |
65 | character data. Such data may come from filehandles, from calls to |
66 | external programs, from information provided by the system (such as %ENV), |
21bad921 |
67 | or from literals and constants in the source text. |
8cbd9a7a |
68 | |
c349b1b9 |
69 | On Windows platforms, if the C<-C> command line switch is used, (or the |
75daf61c |
70 | ${^WIDE_SYSTEM_CALLS} global flag is set to C<1>), all system calls |
71 | will use the corresponding wide character APIs. Note that this is |
c349b1b9 |
72 | currently only implemented on Windows since other platforms lack an |
73 | API standard on this area. |
8cbd9a7a |
74 | |
75daf61c |
75 | Regardless of the above, the C<bytes> pragma can always be used to |
76 | force byte semantics in a particular lexical scope. See L<bytes>. |
8cbd9a7a |
77 | |
78 | The C<utf8> pragma is primarily a compatibility device that enables |
75daf61c |
79 | recognition of UTF-(8|EBCDIC) in literals encountered by the parser. |
7dedd01f |
80 | Note that this pragma is only required until a future version of Perl |
81 | in which character semantics will become the default. This pragma may |
82 | then become a no-op. See L<utf8>. |
8cbd9a7a |
83 | |
84 | Unless mentioned otherwise, Perl operators will use character semantics |
85 | when they are dealing with Unicode data, and byte semantics otherwise. |
86 | Thus, character semantics for these operations apply transparently; if |
87 | the input data came from a Unicode source (for example, by adding a |
88 | character encoding discipline to the filehandle whence it came, or a |
3e4dbfed |
89 | literal Unicode string constant in the program), character semantics |
8cbd9a7a |
90 | apply; otherwise, byte semantics are in effect. To force byte semantics |
8058d7ab |
91 | on Unicode data, the C<bytes> pragma should be used. |
393fec97 |
92 | |
0a378802 |
93 | Notice that if you concatenate strings with byte semantics and strings |
94 | with Unicode character data, the bytes will by default be upgraded |
95 | I<as if they were ISO 8859-1 (Latin-1)> (or if in EBCDIC, after a |
3e4dbfed |
96 | translation to ISO 8859-1). This is done without regard to the |
97 | system's native 8-bit encoding, so to change this for systems with |
98 | non-Latin-1 (or non-EBCDIC) native encodings, use the C<encoding> |
0a378802 |
99 | pragma, see L<encoding>. |
7dedd01f |
100 | |
feda178f |
101 | Under character semantics, many operations that formerly operated on |
102 | bytes change to operating on characters. A character in Perl is |
103 | logically just a number ranging from 0 to 2**31 or so. Larger |
104 | characters may encode to longer sequences of bytes internally, but |
105 | this is just an internal detail which is hidden at the Perl level. |
106 | See L<perluniintro> for more on this. |
393fec97 |
107 | |
8cbd9a7a |
108 | =head2 Effects of character semantics |
393fec97 |
109 | |
110 | Character semantics have the following effects: |
111 | |
112 | =over 4 |
113 | |
114 | =item * |
115 | |
574c8022 |
116 | Strings (including hash keys) and regular expression patterns may |
117 | contain characters that have an ordinal value larger than 255. |
393fec97 |
118 | |
feda178f |
119 | If you use a Unicode editor to edit your program, Unicode characters |
120 | may occur directly within the literal strings in one of the various |
121 | Unicode encodings (UTF-8, UTF-EBCDIC, UCS-2, etc.), but are recognized |
122 | as such (and converted to Perl's internal representation) only if the |
123 | appropriate L<encoding> is specified. |
3e4dbfed |
124 | |
125 | You can also get Unicode characters into a string by using the C<\x{...}> |
126 | notation, putting the Unicode code for the desired character, in |
127 | hexadecimal, into the curlies. For instance, a smiley face is C<\x{263A}>. |
128 | This works only for characters with a code 0x100 and above. |
129 | |
130 | Additionally, if you |
574c8022 |
131 | |
3e4dbfed |
132 | use charnames ':full'; |
574c8022 |
133 | |
3e4dbfed |
134 | you can use the C<\N{...}> notation, putting the official Unicode character |
135 | name within the curlies. For example, C<\N{WHITE SMILING FACE}>. |
136 | This works for all characters that have names. |
393fec97 |
137 | |
138 | =item * |
139 | |
3990cdf5 |
140 | If Unicode is used in hash keys, there is a subtle effect on the hashes. |
141 | The hash becomes "Unicode-sticky" so that keys retrieved from the hash |
142 | (either by %hash, each %hash, or keys %hash) will be in Unicode, not |
143 | in bytes, even when the keys were bytes went they "went in". This |
144 | "stickiness" persists unless the hash is completely emptied, either by |
145 | using delete() or clearing the with undef() or assigning an empty list |
146 | to the hash. Most of the time this difference is negligible, but |
147 | there are few places where it matters: for example the regular |
148 | expression character classes like C<\w> behave differently for |
149 | bytes and characters. |
150 | |
151 | =item * |
152 | |
574c8022 |
153 | If an appropriate L<encoding> is specified, identifiers within the |
154 | Perl script may contain Unicode alphanumeric characters, including |
155 | ideographs. (You are currently on your own when it comes to using the |
156 | canonical forms of characters--Perl doesn't (yet) attempt to |
157 | canonicalize variable names for you.) |
393fec97 |
158 | |
393fec97 |
159 | =item * |
160 | |
161 | Regular expressions match characters instead of bytes. For instance, |
162 | "." matches a character instead of a byte. (However, the C<\C> pattern |
75daf61c |
163 | is provided to force a match a single byte ("C<char>" in C, hence C<\C>).) |
393fec97 |
164 | |
393fec97 |
165 | =item * |
166 | |
167 | Character classes in regular expressions match characters instead of |
168 | bytes, and match against the character properties specified in the |
75daf61c |
169 | Unicode properties database. So C<\w> can be used to match an |
170 | ideograph, for instance. |
393fec97 |
171 | |
393fec97 |
172 | =item * |
173 | |
eb0cc9e3 |
174 | Named Unicode properties, scripts, and block ranges may be used like |
175 | character classes via the new C<\p{}> (matches property) and C<\P{}> |
176 | (doesn't match property) constructs. For instance, C<\p{Lu}> matches any |
feda178f |
177 | character with the Unicode "Lu" (Letter, uppercase) property, while |
178 | C<\p{M}> matches any character with a "M" (mark -- accents and such) |
eb0cc9e3 |
179 | property. Single letter properties may omit the brackets, so that can be |
180 | written C<\pM> also. Many predefined properties are available, such |
181 | as C<\p{Mirrored}> and C<\p{Tibetan}>. |
4193bef7 |
182 | |
cfc01aea |
183 | The official Unicode script and block names have spaces and dashes as |
eb0cc9e3 |
184 | separators, but for convenience you can have dashes, spaces, and underbars |
185 | at every word division, and you need not care about correct casing. It is |
186 | recommended, however, that for consistency you use the following naming: |
187 | the official Unicode script, block, or property name (see below for the |
188 | additional rules that apply to block names), with whitespace and dashes |
189 | removed, and the words "uppercase-first-lowercase-rest". That is, "Latin-1 |
190 | Supplement" becomes "Latin1Supplement". |
4193bef7 |
191 | |
a1cc1cb1 |
192 | You can also negate both C<\p{}> and C<\P{}> by introducing a caret |
eb0cc9e3 |
193 | (^) between the first curly and the property name: C<\p{^Tamil}> is |
194 | equal to C<\P{Tamil}>. |
4193bef7 |
195 | |
eb0cc9e3 |
196 | Here are the basic Unicode General Category properties, followed by their |
197 | long form (you can use either, e.g. C<\p{Lu}> and C<\p{LowercaseLetter}> |
198 | are identical). |
393fec97 |
199 | |
d73e5302 |
200 | Short Long |
201 | |
202 | L Letter |
eb0cc9e3 |
203 | Lu UppercaseLetter |
204 | Ll LowercaseLetter |
205 | Lt TitlecaseLetter |
206 | Lm ModifierLetter |
207 | Lo OtherLetter |
d73e5302 |
208 | |
209 | M Mark |
eb0cc9e3 |
210 | Mn NonspacingMark |
211 | Mc SpacingMark |
212 | Me EnclosingMark |
d73e5302 |
213 | |
214 | N Number |
eb0cc9e3 |
215 | Nd DecimalNumber |
216 | Nl LetterNumber |
217 | No OtherNumber |
d73e5302 |
218 | |
219 | P Punctuation |
eb0cc9e3 |
220 | Pc ConnectorPunctuation |
221 | Pd DashPunctuation |
222 | Ps OpenPunctuation |
223 | Pe ClosePunctuation |
224 | Pi InitialPunctuation |
d73e5302 |
225 | (may behave like Ps or Pe depending on usage) |
eb0cc9e3 |
226 | Pf FinalPunctuation |
d73e5302 |
227 | (may behave like Ps or Pe depending on usage) |
eb0cc9e3 |
228 | Po OtherPunctuation |
d73e5302 |
229 | |
230 | S Symbol |
eb0cc9e3 |
231 | Sm MathSymbol |
232 | Sc CurrencySymbol |
233 | Sk ModifierSymbol |
234 | So OtherSymbol |
d73e5302 |
235 | |
236 | Z Separator |
eb0cc9e3 |
237 | Zs SpaceSeparator |
238 | Zl LineSeparator |
239 | Zp ParagraphSeparator |
d73e5302 |
240 | |
241 | C Other |
e150c829 |
242 | Cc Control |
243 | Cf Format |
eb0cc9e3 |
244 | Cs Surrogate (not usable) |
245 | Co PrivateUse |
e150c829 |
246 | Cn Unassigned |
1ac13f9a |
247 | |
3e4dbfed |
248 | The single-letter properties match all characters in any of the |
249 | two-letter sub-properties starting with the same letter. |
1ac13f9a |
250 | There's also C<L&> which is an alias for C<Ll>, C<Lu>, and C<Lt>. |
32293815 |
251 | |
eb0cc9e3 |
252 | Because Perl hides the need for the user to understand the internal |
253 | representation of Unicode characters, it has no need to support the |
254 | somewhat messy concept of surrogates. Therefore, the C<Cs> property is not |
255 | supported. |
d73e5302 |
256 | |
eb0cc9e3 |
257 | Because scripts differ in their directionality (for example Hebrew is |
258 | written right to left), Unicode supplies these properties: |
32293815 |
259 | |
eb0cc9e3 |
260 | Property Meaning |
92e830a9 |
261 | |
d73e5302 |
262 | BidiL Left-to-Right |
263 | BidiLRE Left-to-Right Embedding |
264 | BidiLRO Left-to-Right Override |
265 | BidiR Right-to-Left |
266 | BidiAL Right-to-Left Arabic |
267 | BidiRLE Right-to-Left Embedding |
268 | BidiRLO Right-to-Left Override |
269 | BidiPDF Pop Directional Format |
270 | BidiEN European Number |
271 | BidiES European Number Separator |
272 | BidiET European Number Terminator |
273 | BidiAN Arabic Number |
274 | BidiCS Common Number Separator |
275 | BidiNSM Non-Spacing Mark |
276 | BidiBN Boundary Neutral |
277 | BidiB Paragraph Separator |
278 | BidiS Segment Separator |
279 | BidiWS Whitespace |
280 | BidiON Other Neutrals |
32293815 |
281 | |
eb0cc9e3 |
282 | For example, C<\p{BidiR}> matches all characters that are normally |
283 | written right to left. |
284 | |
210b36aa |
285 | =back |
286 | |
2796c109 |
287 | =head2 Scripts |
288 | |
eb0cc9e3 |
289 | The scripts available via C<\p{...}> and C<\P{...}>, for example |
290 | C<\p{Latin}> or \p{Cyrillic>, are as follows: |
2796c109 |
291 | |
1ac13f9a |
292 | Arabic |
e9ad1727 |
293 | Armenian |
1ac13f9a |
294 | Bengali |
e9ad1727 |
295 | Bopomofo |
1d81abf3 |
296 | Buhid |
eb0cc9e3 |
297 | CanadianAboriginal |
e9ad1727 |
298 | Cherokee |
299 | Cyrillic |
300 | Deseret |
301 | Devanagari |
302 | Ethiopic |
303 | Georgian |
304 | Gothic |
305 | Greek |
1ac13f9a |
306 | Gujarati |
e9ad1727 |
307 | Gurmukhi |
308 | Han |
309 | Hangul |
1d81abf3 |
310 | Hanunoo |
e9ad1727 |
311 | Hebrew |
312 | Hiragana |
313 | Inherited |
1ac13f9a |
314 | Kannada |
e9ad1727 |
315 | Katakana |
316 | Khmer |
1ac13f9a |
317 | Lao |
e9ad1727 |
318 | Latin |
319 | Malayalam |
320 | Mongolian |
1ac13f9a |
321 | Myanmar |
1ac13f9a |
322 | Ogham |
eb0cc9e3 |
323 | OldItalic |
e9ad1727 |
324 | Oriya |
1ac13f9a |
325 | Runic |
e9ad1727 |
326 | Sinhala |
327 | Syriac |
1d81abf3 |
328 | Tagalog |
329 | Tagbanwa |
e9ad1727 |
330 | Tamil |
331 | Telugu |
332 | Thaana |
333 | Thai |
334 | Tibetan |
1ac13f9a |
335 | Yi |
1ac13f9a |
336 | |
337 | There are also extended property classes that supplement the basic |
338 | properties, defined by the F<PropList> Unicode database: |
339 | |
1d81abf3 |
340 | ASCIIHexDigit |
eb0cc9e3 |
341 | BidiControl |
1ac13f9a |
342 | Dash |
1d81abf3 |
343 | Deprecated |
1ac13f9a |
344 | Diacritic |
345 | Extender |
1d81abf3 |
346 | GraphemeLink |
eb0cc9e3 |
347 | HexDigit |
e9ad1727 |
348 | Hyphen |
349 | Ideographic |
1d81abf3 |
350 | IDSBinaryOperator |
351 | IDSTrinaryOperator |
eb0cc9e3 |
352 | JoinControl |
1d81abf3 |
353 | LogicalOrderException |
eb0cc9e3 |
354 | NoncharacterCodePoint |
355 | OtherAlphabetic |
1d81abf3 |
356 | OtherDefaultIgnorableCodePoint |
357 | OtherGraphemeExtend |
eb0cc9e3 |
358 | OtherLowercase |
359 | OtherMath |
360 | OtherUppercase |
361 | QuotationMark |
1d81abf3 |
362 | Radical |
363 | SoftDotted |
364 | TerminalPunctuation |
365 | UnifiedIdeograph |
eb0cc9e3 |
366 | WhiteSpace |
1ac13f9a |
367 | |
368 | and further derived properties: |
369 | |
eb0cc9e3 |
370 | Alphabetic Lu + Ll + Lt + Lm + Lo + OtherAlphabetic |
371 | Lowercase Ll + OtherLowercase |
372 | Uppercase Lu + OtherUppercase |
373 | Math Sm + OtherMath |
1ac13f9a |
374 | |
375 | ID_Start Lu + Ll + Lt + Lm + Lo + Nl |
376 | ID_Continue ID_Start + Mn + Mc + Nd + Pc |
377 | |
378 | Any Any character |
eb0cc9e3 |
379 | Assigned Any non-Cn character (i.e. synonym for C<\P{Cn}>) |
380 | Unassigned Synonym for C<\p{Cn}> |
1ac13f9a |
381 | Common Any character (or unassigned code point) |
e150c829 |
382 | not explicitly assigned to a script |
2796c109 |
383 | |
eb0cc9e3 |
384 | For backward compatability, all properties mentioned so far may have C<Is> |
385 | prepended to their name (e.g. C<\P{IsLu}> is equal to C<\P{Lu}>). |
386 | |
2796c109 |
387 | =head2 Blocks |
388 | |
eb0cc9e3 |
389 | In addition to B<scripts>, Unicode also defines B<blocks> of characters. |
390 | The difference between scripts and blocks is that the scripts concept is |
391 | closer to natural languages, while the blocks concept is more an artificial |
392 | grouping based on groups of mostly 256 Unicode characters. For example, the |
393 | C<Latin> script contains letters from many blocks. On the other hand, the |
394 | C<Latin> script does not contain all the characters from those blocks. It |
395 | does not, for example, contain digits because digits are shared across many |
396 | scripts. Digits and other similar groups, like punctuation, are in a |
397 | category called C<Common>. |
2796c109 |
398 | |
cfc01aea |
399 | For more about scripts, see the UTR #24: |
400 | |
401 | http://www.unicode.org/unicode/reports/tr24/ |
402 | |
403 | For more about blocks, see: |
404 | |
405 | http://www.unicode.org/Public/UNIDATA/Blocks.txt |
2796c109 |
406 | |
eb0cc9e3 |
407 | Blocks names are given with the C<In> prefix. For example, the |
92e830a9 |
408 | Katakana block is referenced via C<\p{InKatakana}>. The C<In> |
eb0cc9e3 |
409 | prefix may be omitted if there is no nameing conflict with a script |
410 | or any other property, but it is recommended that C<In> always be used |
411 | to avoid confusion. |
412 | |
413 | These block names are supported: |
414 | |
1d81abf3 |
415 | InAlphabeticPresentationForms |
416 | InArabic |
417 | InArabicPresentationFormsA |
418 | InArabicPresentationFormsB |
419 | InArmenian |
420 | InArrows |
421 | InBasicLatin |
422 | InBengali |
423 | InBlockElements |
424 | InBopomofo |
425 | InBopomofoExtended |
426 | InBoxDrawing |
427 | InBraillePatterns |
428 | InBuhid |
429 | InByzantineMusicalSymbols |
430 | InCJKCompatibility |
431 | InCJKCompatibilityForms |
432 | InCJKCompatibilityIdeographs |
433 | InCJKCompatibilityIdeographsSupplement |
434 | InCJKRadicalsSupplement |
435 | InCJKSymbolsAndPunctuation |
436 | InCJKUnifiedIdeographs |
437 | InCJKUnifiedIdeographsExtensionA |
438 | InCJKUnifiedIdeographsExtensionB |
439 | InCherokee |
440 | InCombiningDiacriticalMarks |
441 | InCombiningDiacriticalMarksforSymbols |
442 | InCombiningHalfMarks |
443 | InControlPictures |
444 | InCurrencySymbols |
445 | InCyrillic |
446 | InCyrillicSupplementary |
447 | InDeseret |
448 | InDevanagari |
449 | InDingbats |
450 | InEnclosedAlphanumerics |
451 | InEnclosedCJKLettersAndMonths |
452 | InEthiopic |
453 | InGeneralPunctuation |
454 | InGeometricShapes |
455 | InGeorgian |
456 | InGothic |
457 | InGreekExtended |
458 | InGreekAndCoptic |
459 | InGujarati |
460 | InGurmukhi |
461 | InHalfwidthAndFullwidthForms |
462 | InHangulCompatibilityJamo |
463 | InHangulJamo |
464 | InHangulSyllables |
465 | InHanunoo |
466 | InHebrew |
467 | InHighPrivateUseSurrogates |
468 | InHighSurrogates |
469 | InHiragana |
470 | InIPAExtensions |
471 | InIdeographicDescriptionCharacters |
472 | InKanbun |
473 | InKangxiRadicals |
474 | InKannada |
475 | InKatakana |
476 | InKatakanaPhoneticExtensions |
477 | InKhmer |
478 | InLao |
479 | InLatin1Supplement |
480 | InLatinExtendedA |
481 | InLatinExtendedAdditional |
482 | InLatinExtendedB |
483 | InLetterlikeSymbols |
484 | InLowSurrogates |
485 | InMalayalam |
486 | InMathematicalAlphanumericSymbols |
487 | InMathematicalOperators |
488 | InMiscellaneousMathematicalSymbolsA |
489 | InMiscellaneousMathematicalSymbolsB |
490 | InMiscellaneousSymbols |
491 | InMiscellaneousTechnical |
492 | InMongolian |
493 | InMusicalSymbols |
494 | InMyanmar |
495 | InNumberForms |
496 | InOgham |
497 | InOldItalic |
498 | InOpticalCharacterRecognition |
499 | InOriya |
500 | InPrivateUseArea |
501 | InRunic |
502 | InSinhala |
503 | InSmallFormVariants |
504 | InSpacingModifierLetters |
505 | InSpecials |
506 | InSuperscriptsAndSubscripts |
507 | InSupplementalArrowsA |
508 | InSupplementalArrowsB |
509 | InSupplementalMathematicalOperators |
510 | InSupplementaryPrivateUseAreaA |
511 | InSupplementaryPrivateUseAreaB |
512 | InSyriac |
513 | InTagalog |
514 | InTagbanwa |
515 | InTags |
516 | InTamil |
517 | InTelugu |
518 | InThaana |
519 | InThai |
520 | InTibetan |
521 | InUnifiedCanadianAboriginalSyllabics |
522 | InVariationSelectors |
523 | InYiRadicals |
524 | InYiSyllables |
32293815 |
525 | |
210b36aa |
526 | =over 4 |
527 | |
393fec97 |
528 | =item * |
529 | |
c29a771d |
530 | The special pattern C<\X> matches any extended Unicode sequence |
393fec97 |
531 | (a "combining character sequence" in Standardese), where the first |
532 | character is a base character and subsequent characters are mark |
533 | characters that apply to the base character. It is equivalent to |
534 | C<(?:\PM\pM*)>. |
535 | |
393fec97 |
536 | =item * |
537 | |
383e7cdd |
538 | The C<tr///> operator translates characters instead of bytes. Note |
539 | that the C<tr///CU> functionality has been removed, as the interface |
540 | was a mistake. For similar functionality see pack('U0', ...) and |
541 | pack('C0', ...). |
393fec97 |
542 | |
393fec97 |
543 | =item * |
544 | |
545 | Case translation operators use the Unicode case translation tables |
44bc797b |
546 | when provided character input. Note that C<uc()> (also known as C<\U> |
547 | in doublequoted strings) translates to uppercase, while C<ucfirst> |
548 | (also known as C<\u> in doublequoted strings) translates to titlecase |
549 | (for languages that make the distinction). Naturally the |
550 | corresponding backslash sequences have the same semantics. |
393fec97 |
551 | |
552 | =item * |
553 | |
554 | Most operators that deal with positions or lengths in the string will |
75daf61c |
555 | automatically switch to using character positions, including |
556 | C<chop()>, C<substr()>, C<pos()>, C<index()>, C<rindex()>, |
557 | C<sprintf()>, C<write()>, and C<length()>. Operators that |
558 | specifically don't switch include C<vec()>, C<pack()>, and |
559 | C<unpack()>. Operators that really don't care include C<chomp()>, as |
560 | well as any other operator that treats a string as a bucket of bits, |
561 | such as C<sort()>, and the operators dealing with filenames. |
393fec97 |
562 | |
563 | =item * |
564 | |
565 | The C<pack()>/C<unpack()> letters "C<c>" and "C<C>" do I<not> change, |
566 | since they're often used for byte-oriented formats. (Again, think |
567 | "C<char>" in the C language.) However, there is a new "C<U>" specifier |
3e4dbfed |
568 | that will convert between Unicode characters and integers. |
393fec97 |
569 | |
570 | =item * |
571 | |
572 | The C<chr()> and C<ord()> functions work on characters. This is like |
573 | C<pack("U")> and C<unpack("U")>, not like C<pack("C")> and |
574 | C<unpack("C")>. In fact, the latter are how you now emulate |
35bcd338 |
575 | byte-oriented C<chr()> and C<ord()> for Unicode strings. |
3e4dbfed |
576 | (Note that this reveals the internal encoding of Unicode strings, |
577 | which is not something one normally needs to care about at all.) |
393fec97 |
578 | |
579 | =item * |
580 | |
a1ca4561 |
581 | The bit string operators C<& | ^ ~> can operate on character data. |
582 | However, for backward compatibility reasons (bit string operations |
75daf61c |
583 | when the characters all are less than 256 in ordinal value) one should |
584 | not mix C<~> (the bit complement) and characters both less than 256 and |
a1ca4561 |
585 | equal or greater than 256. Most importantly, the DeMorgan's laws |
586 | (C<~($x|$y) eq ~$x&~$y>, C<~($x&$y) eq ~$x|~$y>) won't hold. |
587 | Another way to look at this is that the complement cannot return |
75daf61c |
588 | B<both> the 8-bit (byte) wide bit complement B<and> the full character |
a1ca4561 |
589 | wide bit complement. |
590 | |
591 | =item * |
592 | |
983ffd37 |
593 | lc(), uc(), lcfirst(), and ucfirst() work for the following cases: |
594 | |
595 | =over 8 |
596 | |
597 | =item * |
598 | |
599 | the case mapping is from a single Unicode character to another |
600 | single Unicode character |
601 | |
602 | =item * |
603 | |
604 | the case mapping is from a single Unicode character to more |
605 | than one Unicode character |
606 | |
607 | =back |
608 | |
210b36aa |
609 | What doesn't yet work are the following cases: |
983ffd37 |
610 | |
611 | =over 8 |
612 | |
613 | =item * |
614 | |
615 | the "final sigma" (Greek) |
616 | |
617 | =item * |
618 | |
619 | anything to with locales (Lithuanian, Turkish, Azeri) |
620 | |
621 | =back |
622 | |
623 | See the Unicode Technical Report #21, Case Mappings, for more details. |
ac1256e8 |
624 | |
625 | =item * |
626 | |
393fec97 |
627 | And finally, C<scalar reverse()> reverses by character rather than by byte. |
628 | |
629 | =back |
630 | |
8cbd9a7a |
631 | =head2 Character encodings for input and output |
632 | |
7221edc9 |
633 | See L<Encode>. |
8cbd9a7a |
634 | |
c29a771d |
635 | =head2 Unicode Regular Expression Support Level |
776f8809 |
636 | |
637 | The following list of Unicode regular expression support describes |
638 | feature by feature the Unicode support implemented in Perl as of Perl |
639 | 5.8.0. The "Level N" and the section numbers refer to the Unicode |
640 | Technical Report 18, "Unicode Regular Expression Guidelines". |
641 | |
642 | =over 4 |
643 | |
644 | =item * |
645 | |
646 | Level 1 - Basic Unicode Support |
647 | |
648 | 2.1 Hex Notation - done [1] |
3bfdc84c |
649 | Named Notation - done [2] |
776f8809 |
650 | 2.2 Categories - done [3][4] |
651 | 2.3 Subtraction - MISSING [5][6] |
652 | 2.4 Simple Word Boundaries - done [7] |
78d3e1bf |
653 | 2.5 Simple Loose Matches - done [8] |
776f8809 |
654 | 2.6 End of Line - MISSING [9][10] |
655 | |
656 | [ 1] \x{...} |
657 | [ 2] \N{...} |
eb0cc9e3 |
658 | [ 3] . \p{...} \P{...} |
29bdacb8 |
659 | [ 4] now scripts (see UTR#24 Script Names) in addition to blocks |
776f8809 |
660 | [ 5] have negation |
29bdacb8 |
661 | [ 6] can use look-ahead to emulate subtraction (*) |
776f8809 |
662 | [ 7] include Letters in word characters |
e0f9d4a8 |
663 | [ 8] note that perl does Full casefolding in matching, not Simple: |
664 | for example U+1F88 is equivalent with U+1F000 U+03B9, |
665 | not with 1F80. This difference matters for certain Greek |
666 | capital letters with certain modifiers: the Full casefolding |
667 | decomposes the letter, while the Simple casefolding would map |
668 | it to a single character. |
776f8809 |
669 | [ 9] see UTR#13 Unicode Newline Guidelines |
ec83e909 |
670 | [10] should do ^ and $ also on \x{85}, \x{2028} and \x{2029}) |
671 | (should also affect <>, $., and script line numbers) |
3bfdc84c |
672 | (the \x{85}, \x{2028} and \x{2029} do match \s) |
7207e29d |
673 | |
dbe420b4 |
674 | (*) You can mimic class subtraction using lookahead. |
675 | For example, what TR18 might write as |
29bdacb8 |
676 | |
dbe420b4 |
677 | [{Greek}-[{UNASSIGNED}]] |
678 | |
679 | in Perl can be written as: |
680 | |
1d81abf3 |
681 | (?!\p{Unassigned})\p{InGreekAndCoptic} |
682 | (?=\p{Assigned})\p{InGreekAndCoptic} |
dbe420b4 |
683 | |
684 | But in this particular example, you probably really want |
685 | |
686 | \p{Greek} |
687 | |
688 | which will match assigned characters known to be part of the Greek script. |
29bdacb8 |
689 | |
776f8809 |
690 | =item * |
691 | |
692 | Level 2 - Extended Unicode Support |
693 | |
694 | 3.1 Surrogates - MISSING |
695 | 3.2 Canonical Equivalents - MISSING [11][12] |
696 | 3.3 Locale-Independent Graphemes - MISSING [13] |
697 | 3.4 Locale-Independent Words - MISSING [14] |
698 | 3.5 Locale-Independent Loose Matches - MISSING [15] |
699 | |
700 | [11] see UTR#15 Unicode Normalization |
701 | [12] have Unicode::Normalize but not integrated to regexes |
702 | [13] have \X but at this level . should equal that |
703 | [14] need three classes, not just \w and \W |
704 | [15] see UTR#21 Case Mappings |
705 | |
706 | =item * |
707 | |
708 | Level 3 - Locale-Sensitive Support |
709 | |
710 | 4.1 Locale-Dependent Categories - MISSING |
711 | 4.2 Locale-Dependent Graphemes - MISSING [16][17] |
712 | 4.3 Locale-Dependent Words - MISSING |
713 | 4.4 Locale-Dependent Loose Matches - MISSING |
714 | 4.5 Locale-Dependent Ranges - MISSING |
715 | |
716 | [16] see UTR#10 Unicode Collation Algorithms |
717 | [17] have Unicode::Collate but not integrated to regexes |
718 | |
719 | =back |
720 | |
c349b1b9 |
721 | =head2 Unicode Encodings |
722 | |
723 | Unicode characters are assigned to I<code points> which are abstract |
86bbd6d1 |
724 | numbers. To use these numbers various encodings are needed. |
c349b1b9 |
725 | |
726 | =over 4 |
727 | |
c29a771d |
728 | =item * |
5cb3728c |
729 | |
730 | UTF-8 |
c349b1b9 |
731 | |
3e4dbfed |
732 | UTF-8 is a variable-length (1 to 6 bytes, current character allocations |
733 | require 4 bytes), byteorder independent encoding. For ASCII, UTF-8 is |
734 | transparent (and we really do mean 7-bit ASCII, not another 8-bit encoding). |
c349b1b9 |
735 | |
8c007b5a |
736 | The following table is from Unicode 3.2. |
05632f9a |
737 | |
738 | Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte |
739 | |
8c007b5a |
740 | U+0000..U+007F 00..7F |
741 | U+0080..U+07FF C2..DF 80..BF |
05632f9a |
742 | U+0800..U+0FFF E0 A0..BF 80..BFÂ Â |
8c007b5a |
743 | U+1000..U+CFFF E1..EC 80..BF 80..BFÂ Â |
744 | U+D000..U+D7FF ED 80..9F 80..BFÂ Â |
745 | U+D800..U+DFFF ******* ill-formed ******* |
746 | U+E000..U+FFFF EE..EF 80..BF 80..BFÂ Â |
05632f9a |
747 | U+10000..U+3FFFF F0 90..BF 80..BF 80..BF |
748 | U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF |
749 | U+100000..U+10FFFF F4 80..8F 80..BF 80..BF |
750 | |
8c007b5a |
751 | Note the A0..BF in U+0800..U+0FFF, the 80..9F in U+D000...U+D7FF, |
752 | the 90..BF in U+10000..U+3FFFF, and the 80...8F in U+100000..U+10FFFF. |
05632f9a |
753 | Or, another way to look at it, as bits: |
754 | |
755 | Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte |
756 | |
757 | 0aaaaaaa 0aaaaaaa |
758 | 00000bbbbbaaaaaa 110bbbbb 10aaaaaa |
759 | ccccbbbbbbaaaaaa 1110cccc 10bbbbbb 10aaaaaa |
760 | 00000dddccccccbbbbbbaaaaaa 11110ddd 10cccccc 10bbbbbb 10aaaaaa |
761 | |
762 | As you can see, the continuation bytes all begin with C<10>, and the |
8c007b5a |
763 | leading bits of the start byte tell how many bytes the are in the |
05632f9a |
764 | encoded character. |
765 | |
c29a771d |
766 | =item * |
5cb3728c |
767 | |
768 | UTF-EBCDIC |
dbe420b4 |
769 | |
fe854a6f |
770 | Like UTF-8, but EBCDIC-safe, as UTF-8 is ASCII-safe. |
dbe420b4 |
771 | |
c29a771d |
772 | =item * |
5cb3728c |
773 | |
774 | UTF-16, UTF-16BE, UTF16-LE, Surrogates, and BOMs (Byte Order Marks) |
c349b1b9 |
775 | |
dbe420b4 |
776 | (The followings items are mostly for reference, Perl doesn't |
777 | use them internally.) |
778 | |
c349b1b9 |
779 | UTF-16 is a 2 or 4 byte encoding. The Unicode code points |
780 | 0x0000..0xFFFF are stored in two 16-bit units, and the code points |
dbe420b4 |
781 | 0x010000..0x10FFFF in two 16-bit units. The latter case is |
c349b1b9 |
782 | using I<surrogates>, the first 16-bit unit being the I<high |
783 | surrogate>, and the second being the I<low surrogate>. |
784 | |
785 | Surrogates are code points set aside to encode the 0x01000..0x10FFFF |
786 | range of Unicode code points in pairs of 16-bit units. The I<high |
787 | surrogates> are the range 0xD800..0xDBFF, and the I<low surrogates> |
788 | are the range 0xDC00..0xDFFFF. The surrogate encoding is |
789 | |
790 | $hi = ($uni - 0x10000) / 0x400 + 0xD800; |
791 | $lo = ($uni - 0x10000) % 0x400 + 0xDC00; |
792 | |
793 | and the decoding is |
794 | |
795 | $uni = 0x10000 + ($hi - 0xD8000) * 0x400 + ($lo - 0xDC00); |
796 | |
feda178f |
797 | If you try to generate surrogates (for example by using chr()), you |
798 | will get a warning if warnings are turned on (C<-w> or C<use |
799 | warnings;>) because those code points are not valid for a Unicode |
800 | character. |
9466bab6 |
801 | |
86bbd6d1 |
802 | Because of the 16-bitness, UTF-16 is byteorder dependent. UTF-16 |
c349b1b9 |
803 | itself can be used for in-memory computations, but if storage or |
86bbd6d1 |
804 | transfer is required, either UTF-16BE (Big Endian) or UTF-16LE |
c349b1b9 |
805 | (Little Endian) must be chosen. |
806 | |
807 | This introduces another problem: what if you just know that your data |
808 | is UTF-16, but you don't know which endianness? Byte Order Marks |
809 | (BOMs) are a solution to this. A special character has been reserved |
86bbd6d1 |
810 | in Unicode to function as a byte order marker: the character with the |
811 | code point 0xFEFF is the BOM. |
042da322 |
812 | |
c349b1b9 |
813 | The trick is that if you read a BOM, you will know the byte order, |
814 | since if it was written on a big endian platform, you will read the |
86bbd6d1 |
815 | bytes 0xFE 0xFF, but if it was written on a little endian platform, |
816 | you will read the bytes 0xFF 0xFE. (And if the originating platform |
817 | was writing in UTF-8, you will read the bytes 0xEF 0xBB 0xBF.) |
042da322 |
818 | |
86bbd6d1 |
819 | The way this trick works is that the character with the code point |
820 | 0xFFFE is guaranteed not to be a valid Unicode character, so the |
821 | sequence of bytes 0xFF 0xFE is unambiguously "BOM, represented in |
042da322 |
822 | little-endian format" and cannot be "0xFFFE, represented in big-endian |
823 | format". |
c349b1b9 |
824 | |
c29a771d |
825 | =item * |
5cb3728c |
826 | |
827 | UTF-32, UTF-32BE, UTF32-LE |
c349b1b9 |
828 | |
829 | The UTF-32 family is pretty much like the UTF-16 family, expect that |
042da322 |
830 | the units are 32-bit, and therefore the surrogate scheme is not |
831 | needed. The BOM signatures will be 0x00 0x00 0xFE 0xFF for BE and |
832 | 0xFF 0xFE 0x00 0x00 for LE. |
c349b1b9 |
833 | |
c29a771d |
834 | =item * |
5cb3728c |
835 | |
836 | UCS-2, UCS-4 |
c349b1b9 |
837 | |
86bbd6d1 |
838 | Encodings defined by the ISO 10646 standard. UCS-2 is a 16-bit |
839 | encoding, UCS-4 is a 32-bit encoding. Unlike UTF-16, UCS-2 |
840 | is not extensible beyond 0xFFFF, because it does not use surrogates. |
c349b1b9 |
841 | |
c29a771d |
842 | =item * |
5cb3728c |
843 | |
844 | UTF-7 |
c349b1b9 |
845 | |
846 | A seven-bit safe (non-eight-bit) encoding, useful if the |
847 | transport/storage is not eight-bit safe. Defined by RFC 2152. |
848 | |
95a1a48b |
849 | =back |
850 | |
bf0fa0b2 |
851 | =head2 Security Implications of Malformed UTF-8 |
852 | |
853 | Unfortunately, the specification of UTF-8 leaves some room for |
854 | interpretation of how many bytes of encoded output one should generate |
855 | from one input Unicode character. Strictly speaking, one is supposed |
856 | to always generate the shortest possible sequence of UTF-8 bytes, |
feda178f |
857 | because otherwise there is potential for input buffer overflow at |
858 | the receiving end of a UTF-8 connection. Perl always generates the |
859 | shortest length UTF-8, and with warnings on (C<-w> or C<use |
860 | warnings;>) Perl will warn about non-shortest length UTF-8 (and other |
861 | malformations, too, such as the surrogates, which are not real |
862 | Unicode code points.) |
bf0fa0b2 |
863 | |
c349b1b9 |
864 | =head2 Unicode in Perl on EBCDIC |
865 | |
866 | The way Unicode is handled on EBCDIC platforms is still rather |
86bbd6d1 |
867 | experimental. On such a platform, references to UTF-8 encoding in this |
c349b1b9 |
868 | document and elsewhere should be read as meaning UTF-EBCDIC as |
869 | specified in Unicode Technical Report 16 unless ASCII vs EBCDIC issues |
870 | are specifically discussed. There is no C<utfebcdic> pragma or |
86bbd6d1 |
871 | ":utfebcdic" layer, rather, "utf8" and ":utf8" are re-used to mean |
872 | the platform's "natural" 8-bit encoding of Unicode. See L<perlebcdic> |
873 | for more discussion of the issues. |
c349b1b9 |
874 | |
b310b053 |
875 | =head2 Locales |
876 | |
4616122b |
877 | Usually locale settings and Unicode do not affect each other, but |
b310b053 |
878 | there are a couple of exceptions: |
879 | |
880 | =over 4 |
881 | |
882 | =item * |
883 | |
884 | If your locale environment variables (LANGUAGE, LC_ALL, LC_CTYPE, LANG) |
885 | contain the strings 'UTF-8' or 'UTF8' (case-insensitive matching), |
886 | the default encoding of your STDIN, STDOUT, and STDERR, and of |
887 | B<any subsequent file open>, is UTF-8. |
888 | |
889 | =item * |
890 | |
891 | Perl tries really hard to work both with Unicode and the old byte |
892 | oriented world: most often this is nice, but sometimes this causes |
574c8022 |
893 | problems. |
b310b053 |
894 | |
895 | =back |
896 | |
95a1a48b |
897 | =head2 Using Unicode in XS |
898 | |
899 | If you want to handle Perl Unicode in XS extensions, you may find |
90f968e0 |
900 | the following C APIs useful (see perlapi for details): |
95a1a48b |
901 | |
902 | =over 4 |
903 | |
904 | =item * |
905 | |
f1e62f77 |
906 | DO_UTF8(sv) returns true if the UTF8 flag is on and the bytes pragma |
907 | is not in effect. SvUTF8(sv) returns true is the UTF8 flag is on, the |
908 | bytes pragma is ignored. The UTF8 flag being on does B<not> mean that |
b31c5e31 |
909 | there are any characters of code points greater than 255 (or 127) in |
910 | the scalar, or that there even are any characters in the scalar. |
911 | What the UTF8 flag means is that the sequence of octets in the |
912 | representation of the scalar is the sequence of UTF-8 encoded |
913 | code points of the characters of a string. The UTF8 flag being |
914 | off means that each octet in this representation encodes a single |
915 | character with codepoint 0..255 within the string. Perl's Unicode |
916 | model is not to use UTF-8 until it's really necessary. |
95a1a48b |
917 | |
918 | =item * |
919 | |
920 | uvuni_to_utf8(buf, chr) writes a Unicode character code point into a |
cfc01aea |
921 | buffer encoding the code point as UTF-8, and returns a pointer |
95a1a48b |
922 | pointing after the UTF-8 bytes. |
923 | |
924 | =item * |
925 | |
926 | utf8_to_uvuni(buf, lenp) reads UTF-8 encoded bytes from a buffer and |
927 | returns the Unicode character code point (and optionally the length of |
928 | the UTF-8 byte sequence). |
929 | |
930 | =item * |
931 | |
90f968e0 |
932 | utf8_length(start, end) returns the length of the UTF-8 encoded buffer |
933 | in characters. sv_len_utf8(sv) returns the length of the UTF-8 encoded |
95a1a48b |
934 | scalar. |
935 | |
936 | =item * |
937 | |
938 | sv_utf8_upgrade(sv) converts the string of the scalar to its UTF-8 |
939 | encoded form. sv_utf8_downgrade(sv) does the opposite (if possible). |
940 | sv_utf8_encode(sv) is like sv_utf8_upgrade but the UTF8 flag does not |
941 | get turned on. sv_utf8_decode() does the opposite of sv_utf8_encode(). |
13a6c0e0 |
942 | Note that none of these are to be used as general purpose encoding/decoding |
943 | interfaces: use Encode for that. sv_utf8_upgrade() is affected by the |
944 | encoding pragma, but sv_utf8_downgrade() is not (since the encoding |
945 | pragma is designed to be a one-way street). |
95a1a48b |
946 | |
947 | =item * |
948 | |
90f968e0 |
949 | is_utf8_char(s) returns true if the pointer points to a valid UTF-8 |
950 | character. |
95a1a48b |
951 | |
952 | =item * |
953 | |
954 | is_utf8_string(buf, len) returns true if the len bytes of the buffer |
955 | are valid UTF-8. |
956 | |
957 | =item * |
958 | |
959 | UTF8SKIP(buf) will return the number of bytes in the UTF-8 encoded |
960 | character in the buffer. UNISKIP(chr) will return the number of bytes |
90f968e0 |
961 | required to UTF-8-encode the Unicode character code point. UTF8SKIP() |
962 | is useful for example for iterating over the characters of a UTF-8 |
963 | encoded buffer; UNISKIP() is useful for example in computing |
964 | the size required for a UTF-8 encoded buffer. |
95a1a48b |
965 | |
966 | =item * |
967 | |
968 | utf8_distance(a, b) will tell the distance in characters between the |
969 | two pointers pointing to the same UTF-8 encoded buffer. |
970 | |
971 | =item * |
972 | |
973 | utf8_hop(s, off) will return a pointer to an UTF-8 encoded buffer that |
974 | is C<off> (positive or negative) Unicode characters displaced from the |
90f968e0 |
975 | UTF-8 buffer C<s>. Be careful not to overstep the buffer: utf8_hop() |
976 | will merrily run off the end or the beginning if told to do so. |
95a1a48b |
977 | |
d2cc3551 |
978 | =item * |
979 | |
980 | pv_uni_display(dsv, spv, len, pvlim, flags) and sv_uni_display(dsv, |
981 | ssv, pvlim, flags) are useful for debug output of Unicode strings and |
90f968e0 |
982 | scalars. By default they are useful only for debug: they display |
983 | B<all> characters as hexadecimal code points, but with the flags |
984 | UNI_DISPLAY_ISPRINT and UNI_DISPLAY_BACKSLASH you can make the output |
985 | more readable. |
d2cc3551 |
986 | |
987 | =item * |
988 | |
90f968e0 |
989 | ibcmp_utf8(s1, pe1, u1, l1, u1, s2, pe2, l2, u2) can be used to |
990 | compare two strings case-insensitively in Unicode. |
991 | (For case-sensitive comparisons you can just use memEQ() and memNE() |
992 | as usual.) |
d2cc3551 |
993 | |
c349b1b9 |
994 | =back |
995 | |
95a1a48b |
996 | For more information, see L<perlapi>, and F<utf8.c> and F<utf8.h> |
997 | in the Perl source code distribution. |
998 | |
c29a771d |
999 | =head1 BUGS |
1000 | |
1001 | Use of locales with Unicode data may lead to odd results. Currently |
1002 | there is some attempt to apply 8-bit locale info to characters in the |
1003 | range 0..255, but this is demonstrably incorrect for locales that use |
1004 | characters above that range when mapped into Unicode. It will also |
574c8022 |
1005 | tend to run slower. Use of locales with Unicode is discouraged. |
c29a771d |
1006 | |
1007 | Some functions are slower when working on UTF-8 encoded strings than |
574c8022 |
1008 | on byte encoded strings. All functions that need to hop over |
c29a771d |
1009 | characters such as length(), substr() or index() can work B<much> |
1010 | faster when the underlying data are byte-encoded. Witness the |
1011 | following benchmark: |
666f95b9 |
1012 | |
c29a771d |
1013 | % perl -e ' |
1014 | use Benchmark; |
1015 | use strict; |
1016 | our $l = 10000; |
1017 | our $u = our $b = "x" x $l; |
1018 | substr($u,0,1) = "\x{100}"; |
1019 | timethese(-2,{ |
1020 | LENGTH_B => q{ length($b) }, |
1021 | LENGTH_U => q{ length($u) }, |
1022 | SUBSTR_B => q{ substr($b, $l/4, $l/2) }, |
1023 | SUBSTR_U => q{ substr($u, $l/4, $l/2) }, |
1024 | }); |
1025 | ' |
1026 | Benchmark: running LENGTH_B, LENGTH_U, SUBSTR_B, SUBSTR_U for at least 2 CPU seconds... |
1027 | LENGTH_B: 2 wallclock secs ( 2.36 usr + 0.00 sys = 2.36 CPU) @ 5649983.05/s (n=13333960) |
1028 | LENGTH_U: 2 wallclock secs ( 2.11 usr + 0.00 sys = 2.11 CPU) @ 12155.45/s (n=25648) |
1029 | SUBSTR_B: 3 wallclock secs ( 2.16 usr + 0.00 sys = 2.16 CPU) @ 374480.09/s (n=808877) |
1030 | SUBSTR_U: 2 wallclock secs ( 2.11 usr + 0.00 sys = 2.11 CPU) @ 6791.00/s (n=14329) |
666f95b9 |
1031 | |
c29a771d |
1032 | The numbers show an incredible slowness on long UTF-8 strings and you |
1033 | should carefully avoid to use these functions within tight loops. For |
1034 | example if you want to iterate over characters, it is infinitely |
1035 | better to split into an array than to use substr, as the following |
1036 | benchmark shows: |
1037 | |
1038 | % perl -e ' |
1039 | use Benchmark; |
1040 | use strict; |
1041 | our $l = 10000; |
1042 | our $u = our $b = "x" x $l; |
1043 | substr($u,0,1) = "\x{100}"; |
1044 | timethese(-5,{ |
1045 | SPLIT_B => q{ for my $c (split //, $b){} }, |
1046 | SPLIT_U => q{ for my $c (split //, $u){} }, |
1047 | SUBSTR_B => q{ for my $i (0..length($b)-1){my $c = substr($b,$i,1);} }, |
1048 | SUBSTR_U => q{ for my $i (0..length($u)-1){my $c = substr($u,$i,1);} }, |
1049 | }); |
1050 | ' |
1051 | Benchmark: running SPLIT_B, SPLIT_U, SUBSTR_B, SUBSTR_U for at least 5 CPU seconds... |
1052 | SPLIT_B: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 56.14/s (n=297) |
1053 | SPLIT_U: 5 wallclock secs ( 5.17 usr + 0.01 sys = 5.18 CPU) @ 55.21/s (n=286) |
1054 | SUBSTR_B: 5 wallclock secs ( 5.34 usr + 0.00 sys = 5.34 CPU) @ 123.22/s (n=658) |
1055 | SUBSTR_U: 7 wallclock secs ( 6.20 usr + 0.00 sys = 6.20 CPU) @ 0.81/s (n=5) |
1056 | |
1057 | You see, the algorithm based on substr() was faster with byte encoded |
1058 | data but it is pathologically slow with UTF-8 data. |
666f95b9 |
1059 | |
393fec97 |
1060 | =head1 SEE ALSO |
1061 | |
72ff2908 |
1062 | L<perluniintro>, L<encoding>, L<Encode>, L<open>, L<utf8>, L<bytes>, |
1063 | L<perlretut>, L<perlvar/"${^WIDE_SYSTEM_CALLS}"> |
393fec97 |
1064 | |
1065 | =cut |