.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 .\" .\" Standard preamble: .\" ======================================================================== .de Sh \" Subsection heading .br .if t .Sp .ne 5 .PP \fB\\$1\fR .PP .. .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. | will give a .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' .\" expand to `' in nroff, nothing in troff, for use with C<>. .tr \(*W-|\(bv\*(Tr .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' 'br\} .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . nr % 0 . rr F .\} .\" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .hy 0 .if n .na .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "PERLUNICODE 1" .TH PERLUNICODE 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" .SH "NAME" perlunicode \- Unicode support in Perl .SH "DESCRIPTION" .IX Header "DESCRIPTION" .Sh "Important Caveats" .IX Subsection "Important Caveats" Unicode support is an extensive requirement. While Perl does not implement the Unicode standard or the accompanying technical reports from cover to cover, Perl does support many Unicode features. .IP "Input and Output Layers" 4 .IX Item "Input and Output Layers" Perl knows when a filehandle uses Perl's internal Unicode encodings (\s-1UTF\-8\s0, or UTF-EBCDIC if in \s-1EBCDIC\s0) if the filehandle is opened with the \*(L":utf8\*(R" layer. Other encodings can be converted to Perl's encoding on input or from Perl's encoding on output by use of the \&\*(L":encoding(...)\*(R" layer. See open. .Sp To indicate that Perl source itself is using a particular encoding, see encoding. .IP "Regular Expressions" 4 .IX Item "Regular Expressions" The regular expression compiler produces polymorphic opcodes. That is, the pattern adapts to the data and automatically switches to the Unicode character scheme when presented with Unicode data\*(--or instead uses a traditional byte scheme when presented with byte data. .ie n .IP """use utf8"" still needed to enable \s-1UTF\-8/UTF\-EBCDIC\s0 in scripts" 4 .el .IP "\f(CWuse utf8\fR still needed to enable \s-1UTF\-8/UTF\-EBCDIC\s0 in scripts" 4 .IX Item "use utf8 still needed to enable UTF-8/UTF-EBCDIC in scripts" As a compatibility measure, the \f(CW\*(C`use utf8\*(C'\fR pragma must be explicitly included to enable recognition of \s-1UTF\-8\s0 in the Perl scripts themselves (in string or regular expression literals, or in identifier names) on ASCII-based machines or to recognize UTF-EBCDIC on EBCDIC-based machines. \fBThese are the only times when an explicit \f(CB\*(C`use utf8\*(C'\fB is needed.\fR See utf8. .Sp You can also use the \f(CW\*(C`encoding\*(C'\fR pragma to change the default encoding of the data in your script; see encoding. .IP "BOM-marked scripts and \s-1UTF\-16\s0 scripts autodetected" 4 .IX Item "BOM-marked scripts and UTF-16 scripts autodetected" If a Perl script begins marked with the Unicode \s-1BOM\s0 (\s-1UTF\-16LE\s0, \s-1UTF16\-BE\s0, or \s-1UTF\-8\s0), or if the script looks like non-BOM-marked \s-1UTF\-16\s0 of either endianness, Perl will correctly read in the script as Unicode. (BOMless \s-1UTF\-8\s0 cannot be effectively recognized or differentiated from \&\s-1ISO\s0 8859\-1 or other eight-bit encodings.) .ie n .IP """use encoding"" needed to upgrade non\-Latin\-1 byte strings" 4 .el .IP "\f(CWuse encoding\fR needed to upgrade non\-Latin\-1 byte strings" 4 .IX Item "use encoding needed to upgrade non-Latin-1 byte strings" By default, there is a fundamental asymmetry in Perl's unicode model: implicit upgrading from byte strings to Unicode strings assumes that they were encoded in \fI\s-1ISO\s0 8859\-1 (Latin\-1)\fR, but Unicode strings are downgraded with \s-1UTF\-8\s0 encoding. This happens because the first 256 codepoints in Unicode happens to agree with Latin\-1. .Sp If you wish to interpret byte strings as \s-1UTF\-8\s0 instead, use the \&\f(CW\*(C`encoding\*(C'\fR pragma: .Sp .Vb 1 \& use encoding 'utf8'; .Ve .Sp See \*(L"Byte and Character Semantics\*(R" for more details. .Sh "Byte and Character Semantics" .IX Subsection "Byte and Character Semantics" Beginning with version 5.6, Perl uses logically-wide characters to represent strings internally. .PP In future, Perl-level operations will be expected to work with characters rather than bytes. .PP However, as an interim compatibility measure, Perl aims to provide a safe migration path from byte semantics to character semantics for programs. For operations where Perl can unambiguously decide that the input data are characters, Perl switches to character semantics. For operations where this determination cannot be made without additional information from the user, Perl decides in favor of compatibility and chooses to use byte semantics. .PP This behavior preserves compatibility with earlier versions of Perl, which allowed byte semantics in Perl operations only if none of the program's inputs were marked as being as source of Unicode character data. Such data may come from filehandles, from calls to external programs, from information provided by the system (such as \f(CW%ENV\fR), or from literals and constants in the source text. .PP The \f(CW\*(C`bytes\*(C'\fR pragma will always, regardless of platform, force byte semantics in a particular lexical scope. See bytes. .PP The \f(CW\*(C`utf8\*(C'\fR pragma is primarily a compatibility device that enables recognition of \s-1UTF\-\s0(8|EBCDIC) in literals encountered by the parser. Note that this pragma is only required while Perl defaults to byte semantics; when character semantics become the default, this pragma may become a no\-op. See utf8. .PP Unless explicitly stated, Perl operators use character semantics for Unicode data and byte semantics for non-Unicode data. The decision to use character semantics is made transparently. If input data comes from a Unicode source\*(--for example, if a character encoding layer is added to a filehandle or a literal Unicode string constant appears in a program\*(--character semantics apply. Otherwise, byte semantics are in effect. The \f(CW\*(C`bytes\*(C'\fR pragma should be used to force byte semantics on Unicode data. .PP If strings operating under byte semantics and strings with Unicode character data are concatenated, the new string will be created by decoding the byte strings as \fI\s-1ISO\s0 8859\-1 (Latin\-1)\fR, even if the old Unicode string used \s-1EBCDIC\s0. This translation is done without regard to the system's native 8\-bit encoding. To change this for systems with non\-Latin\-1 and non-EBCDIC native encodings, use the \&\f(CW\*(C`encoding\*(C'\fR pragma. See encoding. .PP Under character semantics, many operations that formerly operated on bytes now operate on characters. A character in Perl is logically just a number ranging from 0 to 2**31 or so. Larger characters may encode into longer sequences of bytes internally, but this internal detail is mostly hidden for Perl code. See perluniintro for more. .Sh "Effects of Character Semantics" .IX Subsection "Effects of Character Semantics" Character semantics have the following effects: .IP "\(bu" 4 Strings\*(--including hash keys\*(--and regular expression patterns may contain characters that have an ordinal value larger than 255. .Sp If you use a Unicode editor to edit your program, Unicode characters may occur directly within the literal strings in one of the various Unicode encodings (\s-1UTF\-8\s0, \s-1UTF\-EBCDIC\s0, \s-1UCS\-2\s0, etc.), but will be recognized as such and converted to Perl's internal representation only if the appropriate encoding is specified. .Sp Unicode characters can also be added to a string by using the \&\f(CW\*(C`\ex{...}\*(C'\fR notation. The Unicode code for the desired character, in hexadecimal, should be placed in the braces. For instance, a smiley face is \f(CW\*(C`\ex{263A}\*(C'\fR. This encoding scheme only works for characters with a code of 0x100 or above. .Sp Additionally, if you .Sp .Vb 1 \& use charnames ':full'; .Ve .Sp you can use the \f(CW\*(C`\eN{...}\*(C'\fR notation and put the official Unicode character name within the braces, such as \f(CW\*(C`\eN{WHITE SMILING FACE}\*(C'\fR. .IP "\(bu" 4 If an appropriate encoding is specified, identifiers within the Perl script may contain Unicode alphanumeric characters, including ideographs. Perl does not currently attempt to canonicalize variable names. .IP "\(bu" 4 Regular expressions match characters instead of bytes. \*(L".\*(R" matches a character instead of a byte. The \f(CW\*(C`\eC\*(C'\fR pattern is provided to force a match a single byte\*(--a \f(CW\*(C`char\*(C'\fR in C, hence \f(CW\*(C`\eC\*(C'\fR. .IP "\(bu" 4 Character classes in regular expressions match characters instead of bytes and match against the character properties specified in the Unicode properties database. \f(CW\*(C`\ew\*(C'\fR can be used to match a Japanese ideograph, for instance. .Sp (However, and as a limitation of the current implementation, using \&\f(CW\*(C`\ew\*(C'\fR or \f(CW\*(C`\eW\*(C'\fR \fIinside\fR a \f(CW\*(C`[...]\*(C'\fR character class will still match with byte semantics.) .IP "\(bu" 4 Named Unicode properties, scripts, and block ranges may be used like character classes via the \f(CW\*(C`\ep{}\*(C'\fR \*(L"matches property\*(R" construct and the \f(CW\*(C`\eP{}\*(C'\fR negation, \*(L"doesn't match property\*(R". .Sp For instance, \f(CW\*(C`\ep{Lu}\*(C'\fR matches any character with the Unicode \*(L"Lu\*(R" (Letter, uppercase) property, while \f(CW\*(C`\ep{M}\*(C'\fR matches any character with an \*(L"M\*(R" (mark\*(--accents and such) property. Brackets are not required for single letter properties, so \f(CW\*(C`\ep{M}\*(C'\fR is equivalent to \&\f(CW\*(C`\epM\*(C'\fR. Many predefined properties are available, such as \&\f(CW\*(C`\ep{Mirrored}\*(C'\fR and \f(CW\*(C`\ep{Tibetan}\*(C'\fR. .Sp The official Unicode script and block names have spaces and dashes as separators, but for convenience you can use dashes, spaces, or underbars, and case is unimportant. It is recommended, however, that for consistency you use the following naming: the official Unicode script, property, or block name (see below for the additional rules that apply to block names) with whitespace and dashes removed, and the words \*(L"uppercase\-first\-lowercase\-rest\*(R". \f(CW\*(C`Latin\-1 Supplement\*(C'\fR thus becomes \f(CW\*(C`Latin1Supplement\*(C'\fR. .Sp You can also use negation in both \f(CW\*(C`\ep{}\*(C'\fR and \f(CW\*(C`\eP{}\*(C'\fR by introducing a caret (^) between the first brace and the property name: \f(CW\*(C`\ep{^Tamil}\*(C'\fR is equal to \f(CW\*(C`\eP{Tamil}\*(C'\fR. .Sp \&\fB\s-1NOTE:\s0 the properties, scripts, and blocks listed here are as of Unicode 3.2.0, March 2002, or Perl 5.8.0, July 2002. Unicode 4.0.0 came out in April 2003, and Perl 5.8.1 in September 2003.\fR .Sp Here are the basic Unicode General Category properties, followed by their long form. You can use either; \f(CW\*(C`\ep{Lu}\*(C'\fR and \f(CW\*(C`\ep{UppercaseLetter}\*(C'\fR, for instance, are identical. .Sp .Vb 1 \& Short Long .Ve .Sp .Vb 7 \& L Letter \& LC CasedLetter \& Lu UppercaseLetter \& Ll LowercaseLetter \& Lt TitlecaseLetter \& Lm ModifierLetter \& Lo OtherLetter .Ve .Sp .Vb 4 \& M Mark \& Mn NonspacingMark \& Mc SpacingMark \& Me EnclosingMark .Ve .Sp .Vb 4 \& N Number \& Nd DecimalNumber \& Nl LetterNumber \& No OtherNumber .Ve .Sp .Vb 10 \& P Punctuation \& Pc ConnectorPunctuation \& Pd DashPunctuation \& Ps OpenPunctuation \& Pe ClosePunctuation \& Pi InitialPunctuation \& (may behave like Ps or Pe depending on usage) \& Pf FinalPunctuation \& (may behave like Ps or Pe depending on usage) \& Po OtherPunctuation .Ve .Sp .Vb 5 \& S Symbol \& Sm MathSymbol \& Sc CurrencySymbol \& Sk ModifierSymbol \& So OtherSymbol .Ve .Sp .Vb 4 \& Z Separator \& Zs SpaceSeparator \& Zl LineSeparator \& Zp ParagraphSeparator .Ve .Sp .Vb 6 \& C Other \& Cc Control \& Cf Format \& Cs Surrogate (not usable) \& Co PrivateUse \& Cn Unassigned .Ve .Sp Single-letter properties match all characters in any of the two-letter sub-properties starting with the same letter. \&\f(CW\*(C`LC\*(C'\fR and \f(CW\*(C`L&\*(C'\fR are special cases, which are aliases for the set of \&\f(CW\*(C`Ll\*(C'\fR, \f(CW\*(C`Lu\*(C'\fR, and \f(CW\*(C`Lt\*(C'\fR. .Sp Because Perl hides the need for the user to understand the internal representation of Unicode characters, there is no need to implement the somewhat messy concept of surrogates. \f(CW\*(C`Cs\*(C'\fR is therefore not supported. .Sp Because scripts differ in their directionality\*(--Hebrew is written right to left, for example\*(--Unicode supplies these properties in the BidiClass class: .Sp .Vb 1 \& Property Meaning .Ve .Sp .Vb 19 \& L Left-to-Right \& LRE Left-to-Right Embedding \& LRO Left-to-Right Override \& R Right-to-Left \& AL Right-to-Left Arabic \& RLE Right-to-Left Embedding \& RLO Right-to-Left Override \& PDF Pop Directional Format \& EN European Number \& ES European Number Separator \& ET European Number Terminator \& AN Arabic Number \& CS Common Number Separator \& NSM Non-Spacing Mark \& BN Boundary Neutral \& B Paragraph Separator \& S Segment Separator \& WS Whitespace \& ON Other Neutrals .Ve .Sp For example, \f(CW\*(C`\ep{BidiClass:R}\*(C'\fR matches characters that are normally written right to left. .Sh "Scripts" .IX Subsection "Scripts" The script names which can be used by \f(CW\*(C`\ep{...}\*(C'\fR and \f(CW\*(C`\eP{...}\*(C'\fR, such as in \f(CW\*(C`\ep{Latin}\*(C'\fR or \f(CW\*(C`\ep{Cyrillic}\*(C'\fR, are as follows: .PP .Vb 44 \& Arabic \& Armenian \& Bengali \& Bopomofo \& Buhid \& CanadianAboriginal \& Cherokee \& Cyrillic \& Deseret \& Devanagari \& Ethiopic \& Georgian \& Gothic \& Greek \& Gujarati \& Gurmukhi \& Han \& Hangul \& Hanunoo \& Hebrew \& Hiragana \& Inherited \& Kannada \& Katakana \& Khmer \& Lao \& Latin \& Malayalam \& Mongolian \& Myanmar \& Ogham \& OldItalic \& Oriya \& Runic \& Sinhala \& Syriac \& Tagalog \& Tagbanwa \& Tamil \& Telugu \& Thaana \& Thai \& Tibetan \& Yi .Ve .PP Extended property classes can supplement the basic properties, defined by the \fIPropList\fR Unicode database: .PP .Vb 27 \& ASCIIHexDigit \& BidiControl \& Dash \& Deprecated \& Diacritic \& Extender \& GraphemeLink \& HexDigit \& Hyphen \& Ideographic \& IDSBinaryOperator \& IDSTrinaryOperator \& JoinControl \& LogicalOrderException \& NoncharacterCodePoint \& OtherAlphabetic \& OtherDefaultIgnorableCodePoint \& OtherGraphemeExtend \& OtherLowercase \& OtherMath \& OtherUppercase \& QuotationMark \& Radical \& SoftDotted \& TerminalPunctuation \& UnifiedIdeograph \& WhiteSpace .Ve .PP and there are further derived properties: .PP .Vb 4 \& Alphabetic Lu + Ll + Lt + Lm + Lo + OtherAlphabetic \& Lowercase Ll + OtherLowercase \& Uppercase Lu + OtherUppercase \& Math Sm + OtherMath .Ve .PP .Vb 2 \& ID_Start Lu + Ll + Lt + Lm + Lo + Nl \& ID_Continue ID_Start + Mn + Mc + Nd + Pc .Ve .PP .Vb 5 \& Any Any character \& Assigned Any non-Cn character (i.e. synonym for \eP{Cn}) \& Unassigned Synonym for \ep{Cn} \& Common Any character (or unassigned code point) \& not explicitly assigned to a script .Ve .PP For backward compatibility (with Perl 5.6), all properties mentioned so far may have \f(CW\*(C`Is\*(C'\fR prepended to their name, so \f(CW\*(C`\eP{IsLu}\*(C'\fR, for example, is equal to \f(CW\*(C`\eP{Lu}\*(C'\fR. .Sh "Blocks" .IX Subsection "Blocks" In addition to \fBscripts\fR, Unicode also defines \fBblocks\fR of characters. The difference between scripts and blocks is that the concept of scripts is closer to natural languages, while the concept of blocks is more of an artificial grouping based on groups of 256 Unicode characters. For example, the \f(CW\*(C`Latin\*(C'\fR script contains letters from many blocks but does not contain all the characters from those blocks. It does not, for example, contain digits, because digits are shared across many scripts. Digits and similar groups, like punctuation, are in a category called \f(CW\*(C`Common\*(C'\fR. .PP For more about scripts, see the \s-1UTR\s0 #24: .PP .Vb 1 \& http://www.unicode.org/unicode/reports/tr24/ .Ve .PP For more about blocks, see: .PP .Vb 1 \& http://www.unicode.org/Public/UNIDATA/Blocks.txt .Ve .PP Block names are given with the \f(CW\*(C`In\*(C'\fR prefix. For example, the Katakana block is referenced via \f(CW\*(C`\ep{InKatakana}\*(C'\fR. The \f(CW\*(C`In\*(C'\fR prefix may be omitted if there is no naming conflict with a script or any other property, but it is recommended that \f(CW\*(C`In\*(C'\fR always be used for block tests to avoid confusion. .PP These block names are supported: .PP .Vb 110 \& InAlphabeticPresentationForms \& InArabic \& InArabicPresentationFormsA \& InArabicPresentationFormsB \& InArmenian \& InArrows \& InBasicLatin \& InBengali \& InBlockElements \& InBopomofo \& InBopomofoExtended \& InBoxDrawing \& InBraillePatterns \& InBuhid \& InByzantineMusicalSymbols \& InCJKCompatibility \& InCJKCompatibilityForms \& InCJKCompatibilityIdeographs \& InCJKCompatibilityIdeographsSupplement \& InCJKRadicalsSupplement \& InCJKSymbolsAndPunctuation \& InCJKUnifiedIdeographs \& InCJKUnifiedIdeographsExtensionA \& InCJKUnifiedIdeographsExtensionB \& InCherokee \& InCombiningDiacriticalMarks \& InCombiningDiacriticalMarksforSymbols \& InCombiningHalfMarks \& InControlPictures \& InCurrencySymbols \& InCyrillic \& InCyrillicSupplementary \& InDeseret \& InDevanagari \& InDingbats \& InEnclosedAlphanumerics \& InEnclosedCJKLettersAndMonths \& InEthiopic \& InGeneralPunctuation \& InGeometricShapes \& InGeorgian \& InGothic \& InGreekExtended \& InGreekAndCoptic \& InGujarati \& InGurmukhi \& InHalfwidthAndFullwidthForms \& InHangulCompatibilityJamo \& InHangulJamo \& InHangulSyllables \& InHanunoo \& InHebrew \& InHighPrivateUseSurrogates \& InHighSurrogates \& InHiragana \& InIPAExtensions \& InIdeographicDescriptionCharacters \& InKanbun \& InKangxiRadicals \& InKannada \& InKatakana \& InKatakanaPhoneticExtensions \& InKhmer \& InLao \& InLatin1Supplement \& InLatinExtendedA \& InLatinExtendedAdditional \& InLatinExtendedB \& InLetterlikeSymbols \& InLowSurrogates \& InMalayalam \& InMathematicalAlphanumericSymbols \& InMathematicalOperators \& InMiscellaneousMathematicalSymbolsA \& InMiscellaneousMathematicalSymbolsB \& InMiscellaneousSymbols \& InMiscellaneousTechnical \& InMongolian \& InMusicalSymbols \& InMyanmar \& InNumberForms \& InOgham \& InOldItalic \& InOpticalCharacterRecognition \& InOriya \& InPrivateUseArea \& InRunic \& InSinhala \& InSmallFormVariants \& InSpacingModifierLetters \& InSpecials \& InSuperscriptsAndSubscripts \& InSupplementalArrowsA \& InSupplementalArrowsB \& InSupplementalMathematicalOperators \& InSupplementaryPrivateUseAreaA \& InSupplementaryPrivateUseAreaB \& InSyriac \& InTagalog \& InTagbanwa \& InTags \& InTamil \& InTelugu \& InThaana \& InThai \& InTibetan \& InUnifiedCanadianAboriginalSyllabics \& InVariationSelectors \& InYiRadicals \& InYiSyllables .Ve .IP "\(bu" 4 The special pattern \f(CW\*(C`\eX\*(C'\fR matches any extended Unicode sequence\-\-\*(L"a combining character sequence\*(R" in Standardese\*(--where the first character is a base character and subsequent characters are mark characters that apply to the base character. \f(CW\*(C`\eX\*(C'\fR is equivalent to \&\f(CW\*(C`(?:\ePM\epM*)\*(C'\fR. .IP "\(bu" 4 The \f(CW\*(C`tr///\*(C'\fR operator translates characters instead of bytes. Note that the \f(CW\*(C`tr///CU\*(C'\fR functionality has been removed. For similar functionality see pack('U0', ...) and pack('C0', ...). .IP "\(bu" 4 Case translation operators use the Unicode case translation tables when character input is provided. Note that \f(CW\*(C`uc()\*(C'\fR, or \f(CW\*(C`\eU\*(C'\fR in interpolated strings, translates to uppercase, while \f(CW\*(C`ucfirst\*(C'\fR, or \f(CW\*(C`\eu\*(C'\fR in interpolated strings, translates to titlecase in languages that make the distinction. .IP "\(bu" 4 Most operators that deal with positions or lengths in a string will automatically switch to using character positions, including \&\f(CW\*(C`chop()\*(C'\fR, \f(CW\*(C`chomp()\*(C'\fR, \f(CW\*(C`substr()\*(C'\fR, \f(CW\*(C`pos()\*(C'\fR, \f(CW\*(C`index()\*(C'\fR, \f(CW\*(C`rindex()\*(C'\fR, \&\f(CW\*(C`sprintf()\*(C'\fR, \f(CW\*(C`write()\*(C'\fR, and \f(CW\*(C`length()\*(C'\fR. Operators that specifically do not switch include \f(CW\*(C`vec()\*(C'\fR, \f(CW\*(C`pack()\*(C'\fR, and \&\f(CW\*(C`unpack()\*(C'\fR. Operators that really don't care include operators that treats strings as a bucket of bits such as \f(CW\*(C`sort()\*(C'\fR, and operators dealing with filenames. .IP "\(bu" 4 The \f(CW\*(C`pack()\*(C'\fR/\f(CW\*(C`unpack()\*(C'\fR letters \f(CW\*(C`c\*(C'\fR and \f(CW\*(C`C\*(C'\fR do \fInot\fR change, since they are often used for byte-oriented formats. Again, think \&\f(CW\*(C`char\*(C'\fR in the C language. .Sp There is a new \f(CW\*(C`U\*(C'\fR specifier that converts between Unicode characters and code points. .IP "\(bu" 4 The \f(CW\*(C`chr()\*(C'\fR and \f(CW\*(C`ord()\*(C'\fR functions work on characters, similar to \&\f(CW\*(C`pack("U")\*(C'\fR and \f(CW\*(C`unpack("U")\*(C'\fR, \fInot\fR \f(CW\*(C`pack("C")\*(C'\fR and \&\f(CW\*(C`unpack("C")\*(C'\fR. \f(CW\*(C`pack("C")\*(C'\fR and \f(CW\*(C`unpack("C")\*(C'\fR are methods for emulating byte-oriented \f(CW\*(C`chr()\*(C'\fR and \f(CW\*(C`ord()\*(C'\fR on Unicode strings. While these methods reveal the internal encoding of Unicode strings, that is not something one normally needs to care about at all. .IP "\(bu" 4 The bit string operators, \f(CW\*(C`& | ^ ~\*(C'\fR, can operate on character data. However, for backward compatibility, such as when using bit string operations when characters are all less than 256 in ordinal value, one should not use \f(CW\*(C`~\*(C'\fR (the bit complement) with characters of both values less than 256 and values greater than 256. Most importantly, DeMorgan's laws (\f(CW\*(C`~($x|$y) eq ~$x&~$y\*(C'\fR and \f(CW\*(C`~($x&$y) eq ~$x|~$y\*(C'\fR) will not hold. The reason for this mathematical \fIfaux pas\fR is that the complement cannot return \fBboth\fR the 8\-bit (byte\-wide) bit complement \fBand\fR the full character-wide bit complement. .IP "\(bu" 4 \&\fIlc()\fR, \fIuc()\fR, \fIlcfirst()\fR, and \fIucfirst()\fR work for the following cases: .RS 4 .IP "\(bu" 8 the case mapping is from a single Unicode character to another single Unicode character, or .IP "\(bu" 8 the case mapping is from a single Unicode character to more than one Unicode character. .RE .RS 4 .Sp Things to do with locales (Lithuanian, Turkish, Azeri) do \fBnot\fR work since Perl does not understand the concept of Unicode locales. .Sp See the Unicode Technical Report #21, Case Mappings, for more details. .RE .IP "\(bu" 4 And finally, \f(CW\*(C`scalar reverse()\*(C'\fR reverses by character rather than by byte. .Sh "User-Defined Character Properties" .IX Subsection "User-Defined Character Properties" You can define your own character properties by defining subroutines whose names begin with \*(L"In\*(R" or \*(L"Is\*(R". The subroutines can be defined in any package. The user-defined properties can be used in the regular expression \f(CW\*(C`\ep\*(C'\fR and \f(CW\*(C`\eP\*(C'\fR constructs; if you are using a user-defined property from a package other than the one you are in, you must specify its package in the \f(CW\*(C`\ep\*(C'\fR or \f(CW\*(C`\eP\*(C'\fR construct. .PP .Vb 3 \& # assuming property IsForeign defined in Lang:: \& package main; # property package name required \& if ($txt =~ /\ep{Lang::IsForeign}+/) { ... } .Ve .PP .Vb 2 \& package Lang; # property package name not required \& if ($txt =~ /\ep{IsForeign}+/) { ... } .Ve .PP Note that the effect is compile-time and immutable once defined. .PP The subroutines must return a specially-formatted string, with one or more newline-separated lines. Each line must be one of the following: .IP "\(bu" 4 Two hexadecimal numbers separated by horizontal whitespace (space or tabular characters) denoting a range of Unicode code points to include. .IP "\(bu" 4 Something to include, prefixed by \*(L"+\*(R": a built-in character property (prefixed by \*(L"utf8::\*(R") or a user-defined character property, to represent all the characters in that property; two hexadecimal code points for a range; or a single hexadecimal code point. .IP "\(bu" 4 Something to exclude, prefixed by \*(L"\-\*(R": an existing character property (prefixed by \*(L"utf8::\*(R") or a user-defined character property, to represent all the characters in that property; two hexadecimal code points for a range; or a single hexadecimal code point. .IP "\(bu" 4 Something to negate, prefixed \*(L"!\*(R": an existing character property (prefixed by \*(L"utf8::\*(R") or a user-defined character property, to represent all the characters in that property; two hexadecimal code points for a range; or a single hexadecimal code point. .IP "\(bu" 4 Something to intersect with, prefixed by \*(L"&\*(R": an existing character property (prefixed by \*(L"utf8::\*(R") or a user-defined character property, for all the characters except the characters in the property; two hexadecimal code points for a range; or a single hexadecimal code point. .PP For example, to define a property that covers both the Japanese syllabaries (hiragana and katakana), you can define .PP .Vb 6 \& sub InKana { \& return </\fIunicore/SpecialCasing.txt\fR. The \f(CW\*(C`Digit\*(C'\fR and \f(CW\*(C`Fold\*(C'\fR mappings that one can see in the directory are not directly user\-accessible, one can use either the \&\f(CW\*(C`Unicode::UCD\*(C'\fR module, or just match case-insensitively (that's when the \f(CW\*(C`Fold\*(C'\fR mapping is used). .PP A final note on the user-defined property tests and mappings: they will be used only if the scalar has been marked as having Unicode characters. Old byte-style strings will not be affected. .Sh "Character Encodings for Input and Output" .IX Subsection "Character Encodings for Input and Output" See Encode. .Sh "Unicode Regular Expression Support Level" .IX Subsection "Unicode Regular Expression Support Level" The following list of Unicode support for regular expressions describes all the features currently supported. The references to \*(L"Level N\*(R" and the section numbers refer to the Unicode Technical Report 18, \&\*(L"Unicode Regular Expression Guidelines\*(R", version 6 (Unicode 3.2.0, Perl 5.8.0). .IP "\(bu" 4 Level 1 \- Basic Unicode Support .Sp .Vb 7 \& 2.1 Hex Notation - done [1] \& Named Notation - done [2] \& 2.2 Categories - done [3][4] \& 2.3 Subtraction - MISSING [5][6] \& 2.4 Simple Word Boundaries - done [7] \& 2.5 Simple Loose Matches - done [8] \& 2.6 End of Line - MISSING [9][10] .Ve .Sp .Vb 20 \& [ 1] \ex{...} \& [ 2] \eN{...} \& [ 3] . \ep{...} \eP{...} \& [ 4] support for scripts (see UTR#24 Script Names), blocks, \& binary properties, enumerated non-binary properties, and \& numeric properties (as listed in UTR#18 Other Properties) \& [ 5] have negation \& [ 6] can use regular expression look-ahead [a] \& or user-defined character properties [b] to emulate subtraction \& [ 7] include Letters in word characters \& [ 8] note that Perl does Full case-folding in matching, not Simple: \& for example U+1F88 is equivalent with U+1F00 U+03B9, \& not with 1F80. This difference matters for certain Greek \& capital letters with certain modifiers: the Full case-folding \& decomposes the letter, while the Simple case-folding would map \& it to a single character. \& [ 9] see UTR #13 Unicode Newline Guidelines \& [10] should do ^ and $ also on \ex{85}, \ex{2028} and \ex{2029} \& (should also affect <>, $., and script line numbers) \& (the \ex{85}, \ex{2028} and \ex{2029} do match \es) .Ve .Sp [a] You can mimic class subtraction using lookahead. For example, what \s-1UTR\s0 #18 might write as .Sp .Vb 1 \& [{Greek}-[{UNASSIGNED}]] .Ve .Sp in Perl can be written as: .Sp .Vb 2 \& (?!\ep{Unassigned})\ep{InGreekAndCoptic} \& (?=\ep{Assigned})\ep{InGreekAndCoptic} .Ve .Sp But in this particular example, you probably really want .Sp .Vb 1 \& \ep{GreekAndCoptic} .Ve .Sp which will match assigned characters known to be part of the Greek script. .Sp Also see the Unicode::Regex::Set module, it does implement the full \&\s-1UTR\s0 #18 grouping, intersection, union, and removal (subtraction) syntax. .Sp [b] See \*(L"User\-Defined Character Properties\*(R". .IP "\(bu" 4 Level 2 \- Extended Unicode Support .Sp .Vb 5 \& 3.1 Surrogates - MISSING [11] \& 3.2 Canonical Equivalents - MISSING [12][13] \& 3.3 Locale-Independent Graphemes - MISSING [14] \& 3.4 Locale-Independent Words - MISSING [15] \& 3.5 Locale-Independent Loose Matches - MISSING [16] .Ve .Sp .Vb 7 \& [11] Surrogates are solely a UTF-16 concept and Perl's internal \& representation is UTF-8. The Encode module does UTF-16, though. \& [12] see UTR#15 Unicode Normalization \& [13] have Unicode::Normalize but not integrated to regexes \& [14] have \eX but at this level . should equal that \& [15] need three classes, not just \ew and \eW \& [16] see UTR#21 Case Mappings .Ve .IP "\(bu" 4 Level 3 \- Locale-Sensitive Support .Sp .Vb 5 \& 4.1 Locale-Dependent Categories - MISSING \& 4.2 Locale-Dependent Graphemes - MISSING [16][17] \& 4.3 Locale-Dependent Words - MISSING \& 4.4 Locale-Dependent Loose Matches - MISSING \& 4.5 Locale-Dependent Ranges - MISSING .Ve .Sp .Vb 2 \& [16] see UTR#10 Unicode Collation Algorithms \& [17] have Unicode::Collate but not integrated to regexes .Ve .Sh "Unicode Encodings" .IX Subsection "Unicode Encodings" Unicode characters are assigned to \fIcode points\fR, which are abstract numbers. To use these numbers, various encodings are needed. .IP "\(bu" 4 \&\s-1UTF\-8\s0 .Sp \&\s-1UTF\-8\s0 is a variable-length (1 to 6 bytes, current character allocations require 4 bytes), byte-order independent encoding. For \s-1ASCII\s0 (and we really do mean 7\-bit \s-1ASCII\s0, not another 8\-bit encoding), \s-1UTF\-8\s0 is transparent. .Sp The following table is from Unicode 3.2. .Sp .Vb 1 \& Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte .Ve .Sp .Vb 10 \& U+0000..U+007F 00..7F \& U+0080..U+07FF C2..DF 80..BF \& U+0800..U+0FFF E0 A0..BF 80..BF \& U+1000..U+CFFF E1..EC 80..BF 80..BF \& U+D000..U+D7FF ED 80..9F 80..BF \& U+D800..U+DFFF ******* ill-formed ******* \& U+E000..U+FFFF EE..EF 80..BF 80..BF \& U+10000..U+3FFFF F0 90..BF 80..BF 80..BF \& U+40000..U+FFFFF F1..F3 80..BF 80..BF 80..BF \& U+100000..U+10FFFF F4 80..8F 80..BF 80..BF .Ve .Sp Note the \f(CW\*(C`A0..BF\*(C'\fR in \f(CW\*(C`U+0800..U+0FFF\*(C'\fR, the \f(CW\*(C`80..9F\*(C'\fR in \&\f(CW\*(C`U+D000...U+D7FF\*(C'\fR, the \f(CW\*(C`90..B\*(C'\fRF in \f(CW\*(C`U+10000..U+3FFFF\*(C'\fR, and the \&\f(CW\*(C`80...8F\*(C'\fR in \f(CW\*(C`U+100000..U+10FFFF\*(C'\fR. The \*(L"gaps\*(R" are caused by legal \&\s-1UTF\-8\s0 avoiding non-shortest encodings: it is technically possible to UTF\-8\-encode a single code point in different ways, but that is explicitly forbidden, and the shortest possible encoding should always be used. So that's what Perl does. .Sp Another way to look at it is via bits: .Sp .Vb 1 \& Code Points 1st Byte 2nd Byte 3rd Byte 4th Byte .Ve .Sp .Vb 4 \& 0aaaaaaa 0aaaaaaa \& 00000bbbbbaaaaaa 110bbbbb 10aaaaaa \& ccccbbbbbbaaaaaa 1110cccc 10bbbbbb 10aaaaaa \& 00000dddccccccbbbbbbaaaaaa 11110ddd 10cccccc 10bbbbbb 10aaaaaa .Ve .Sp As you can see, the continuation bytes all begin with \f(CW10\fR, and the leading bits of the start byte tell how many bytes the are in the encoded character. .IP "\(bu" 4 UTF-EBCDIC .Sp Like \s-1UTF\-8\s0 but EBCDIC\-safe, in the way that \s-1UTF\-8\s0 is ASCII\-safe. .IP "\(bu" 4 \&\s-1UTF\-16\s0, \s-1UTF\-16BE\s0, \s-1UTF\-16LE\s0, Surrogates, and BOMs (Byte Order Marks) .Sp The followings items are mostly for reference and general Unicode knowledge, Perl doesn't use these constructs internally. .Sp \&\s-1UTF\-16\s0 is a 2 or 4 byte encoding. The Unicode code points \&\f(CW\*(C`U+0000..U+FFFF\*(C'\fR are stored in a single 16\-bit unit, and the code points \f(CW\*(C`U+10000..U+10FFFF\*(C'\fR in two 16\-bit units. The latter case is using \fIsurrogates\fR, the first 16\-bit unit being the \fIhigh surrogate\fR, and the second being the \fIlow surrogate\fR. .Sp Surrogates are code points set aside to encode the \f(CW\*(C`U+10000..U+10FFFF\*(C'\fR range of Unicode code points in pairs of 16\-bit units. The \fIhigh surrogates\fR are the range \f(CW\*(C`U+D800..U+DBFF\*(C'\fR, and the \fIlow surrogates\fR are the range \f(CW\*(C`U+DC00..U+DFFF\*(C'\fR. The surrogate encoding is .Sp .Vb 2 \& $hi = ($uni - 0x10000) / 0x400 + 0xD800; \& $lo = ($uni - 0x10000) % 0x400 + 0xDC00; .Ve .Sp and the decoding is .Sp .Vb 1 \& $uni = 0x10000 + ($hi - 0xD800) * 0x400 + ($lo - 0xDC00); .Ve .Sp If you try to generate surrogates (for example by using \fIchr()\fR), you will get a warning if warnings are turned on, because those code points are not valid for a Unicode character. .Sp Because of the 16\-bitness, \s-1UTF\-16\s0 is byte-order dependent. \s-1UTF\-16\s0 itself can be used for in-memory computations, but if storage or transfer is required either \s-1UTF\-16BE\s0 (big\-endian) or \s-1UTF\-16LE\s0 (little\-endian) encodings must be chosen. .Sp This introduces another problem: what if you just know that your data is \s-1UTF\-16\s0, but you don't know which endianness? Byte Order Marks, or BOMs, are a solution to this. A special character has been reserved in Unicode to function as a byte order marker: the character with the code point \f(CW\*(C`U+FEFF\*(C'\fR is the \s-1BOM\s0. .Sp The trick is that if you read a \s-1BOM\s0, you will know the byte order, since if it was written on a big-endian platform, you will read the bytes \f(CW\*(C`0xFE 0xFF\*(C'\fR, but if it was written on a little-endian platform, you will read the bytes \f(CW\*(C`0xFF 0xFE\*(C'\fR. (And if the originating platform was writing in \s-1UTF\-8\s0, you will read the bytes \f(CW\*(C`0xEF 0xBB 0xBF\*(C'\fR.) .Sp The way this trick works is that the character with the code point \&\f(CW\*(C`U+FFFE\*(C'\fR is guaranteed not to be a valid Unicode character, so the sequence of bytes \f(CW\*(C`0xFF 0xFE\*(C'\fR is unambiguously \*(L"\s-1BOM\s0, represented in little-endian format\*(R" and cannot be \f(CW\*(C`U+FFFE\*(C'\fR, represented in big-endian format". .IP "\(bu" 4 \&\s-1UTF\-32\s0, \s-1UTF\-32BE\s0, \s-1UTF\-32LE\s0 .Sp The \s-1UTF\-32\s0 family is pretty much like the \s-1UTF\-16\s0 family, expect that the units are 32\-bit, and therefore the surrogate scheme is not needed. The \s-1BOM\s0 signatures will be \f(CW\*(C`0x00 0x00 0xFE 0xFF\*(C'\fR for \s-1BE\s0 and \&\f(CW\*(C`0xFF 0xFE 0x00 0x00\*(C'\fR for \s-1LE\s0. .IP "\(bu" 4 \&\s-1UCS\-2\s0, \s-1UCS\-4\s0 .Sp Encodings defined by the \s-1ISO\s0 10646 standard. \s-1UCS\-2\s0 is a 16\-bit encoding. Unlike \s-1UTF\-16\s0, \s-1UCS\-2\s0 is not extensible beyond \f(CW\*(C`U+FFFF\*(C'\fR, because it does not use surrogates. \s-1UCS\-4\s0 is a 32\-bit encoding, functionally identical to \s-1UTF\-32\s0. .IP "\(bu" 4 \&\s-1UTF\-7\s0 .Sp A seven-bit safe (non\-eight\-bit) encoding, which is useful if the transport or storage is not eight-bit safe. Defined by \s-1RFC\s0 2152. .Sh "Security Implications of Unicode" .IX Subsection "Security Implications of Unicode" .IP "\(bu" 4 Malformed \s-1UTF\-8\s0 .Sp Unfortunately, the specification of \s-1UTF\-8\s0 leaves some room for interpretation of how many bytes of encoded output one should generate from one input Unicode character. Strictly speaking, the shortest possible sequence of \s-1UTF\-8\s0 bytes should be generated, because otherwise there is potential for an input buffer overflow at the receiving end of a \s-1UTF\-8\s0 connection. Perl always generates the shortest length \s-1UTF\-8\s0, and with warnings on Perl will warn about non-shortest length \s-1UTF\-8\s0 along with other malformations, such as the surrogates, which are not real Unicode code points. .IP "\(bu" 4 Regular expressions behave slightly differently between byte data and character (Unicode) data. For example, the \*(L"word character\*(R" character class \f(CW\*(C`\ew\*(C'\fR will work differently depending on if data is eight-bit bytes or Unicode. .Sp In the first case, the set of \f(CW\*(C`\ew\*(C'\fR characters is either small\*(--the default set of alphabetic characters, digits, and the \*(L"_\*(R"\-\-or, if you are using a locale (see perllocale), the \f(CW\*(C`\ew\*(C'\fR might contain a few more letters according to your language and country. .Sp In the second case, the \f(CW\*(C`\ew\*(C'\fR set of characters is much, much larger. Most importantly, even in the set of the first 256 characters, it will probably match different characters: unlike most locales, which are specific to a language and country pair, Unicode classifies all the characters that are letters \fIsomewhere\fR as \f(CW\*(C`\ew\*(C'\fR. For example, your locale might not think that \s-1LATIN\s0 \s-1SMALL\s0 \s-1LETTER\s0 \s-1ETH\s0 is a letter (unless you happen to speak Icelandic), but Unicode does. .Sp As discussed elsewhere, Perl has one foot (two hooves?) planted in each of two worlds: the old world of bytes and the new world of characters, upgrading from bytes to characters when necessary. If your legacy code does not explicitly use Unicode, no automatic switch-over to characters should happen. Characters shouldn't get downgraded to bytes, either. It is possible to accidentally mix bytes and characters, however (see perluniintro), in which case \f(CW\*(C`\ew\*(C'\fR in regular expressions might start behaving differently. Review your code. Use warnings and the \f(CW\*(C`strict\*(C'\fR pragma. .Sh "Unicode in Perl on \s-1EBCDIC\s0" .IX Subsection "Unicode in Perl on EBCDIC" The way Unicode is handled on \s-1EBCDIC\s0 platforms is still experimental. On such platforms, references to \s-1UTF\-8\s0 encoding in this document and elsewhere should be read as meaning the UTF-EBCDIC specified in Unicode Technical Report 16, unless \s-1ASCII\s0 vs. \s-1EBCDIC\s0 issues are specifically discussed. There is no \f(CW\*(C`utfebcdic\*(C'\fR pragma or \&\*(L":utfebcdic\*(R" layer; rather, \*(L"utf8\*(R" and \*(L":utf8\*(R" are reused to mean the platform's \*(L"natural\*(R" 8\-bit encoding of Unicode. See perlebcdic for more discussion of the issues. .Sh "Locales" .IX Subsection "Locales" Usually locale settings and Unicode do not affect each other, but there are a couple of exceptions: .IP "\(bu" 4 You can enable automatic UTF\-8\-ification of your standard file handles, default \f(CW\*(C`open()\*(C'\fR layer, and \f(CW@ARGV\fR by using either the \f(CW\*(C`\-C\*(C'\fR command line switch or the \f(CW\*(C`PERL_UNICODE\*(C'\fR environment variable, see perlrun for the documentation of the \f(CW\*(C`\-C\*(C'\fR switch. .IP "\(bu" 4 Perl tries really hard to work both with Unicode and the old byte-oriented world. Most often this is nice, but sometimes Perl's straddling of the proverbial fence causes problems. .Sh "When Unicode Does Not Happen" .IX Subsection "When Unicode Does Not Happen" While Perl does have extensive ways to input and output in Unicode, and few other 'entry points' like the \f(CW@ARGV\fR which can be interpreted as Unicode (\s-1UTF\-8\s0), there still are many places where Unicode (in some encoding or another) could be given as arguments or received as results, or both, but it is not. .PP The following are such interfaces. For all of these interfaces Perl currently (as of 5.8.3) simply assumes byte strings both as arguments and results, or \s-1UTF\-8\s0 strings if the \f(CW\*(C`encoding\*(C'\fR pragma has been used. .PP One reason why Perl does not attempt to resolve the role of Unicode in this cases is that the answers are highly dependent on the operating system and the file system(s). For example, whether filenames can be in Unicode, and in exactly what kind of encoding, is not exactly a portable concept. Similarly for the qx and system: how well will the \&'command line interface' (and which of them?) handle Unicode? .IP "\(bu" 4 chdir, chmod, chown, chroot, exec, link, lstat, mkdir, rename, rmdir, stat, symlink, truncate, unlink, utime, \-X .IP "\(bu" 4 %ENV .IP "\(bu" 4 glob (aka the <*>) .IP "\(bu" 4 open, opendir, sysopen .IP "\(bu" 4 qx (aka the backtick operator), system .IP "\(bu" 4 readdir, readlink .Sh "Forcing Unicode in Perl (Or Unforcing Unicode in Perl)" .IX Subsection "Forcing Unicode in Perl (Or Unforcing Unicode in Perl)" Sometimes (see \*(L"When Unicode Does Not Happen\*(R") there are situations where you simply need to force Perl to believe that a byte string is \s-1UTF\-8\s0, or vice versa. The low-level calls utf8::upgrade($bytestring) and utf8::downgrade($utf8string) are the answers. .PP Do not use them without careful thought, though: Perl may easily get very confused, angry, or even crash, if you suddenly change the 'nature' of scalar like that. Especially careful you have to be if you use the \&\fIutf8::upgrade()\fR: any random byte string is not valid \s-1UTF\-8\s0. .Sh "Using Unicode in \s-1XS\s0" .IX Subsection "Using Unicode in XS" If you want to handle Perl Unicode in \s-1XS\s0 extensions, you may find the following C APIs useful. See also \*(L"Unicode Support\*(R" in perlguts for an explanation about Unicode at the \s-1XS\s0 level, and perlapi for the \s-1API\s0 details. .IP "\(bu" 4 \&\f(CW\*(C`DO_UTF8(sv)\*(C'\fR returns true if the \f(CW\*(C`UTF8\*(C'\fR flag is on and the bytes pragma is not in effect. \f(CW\*(C`SvUTF8(sv)\*(C'\fR returns true is the \f(CW\*(C`UTF8\*(C'\fR flag is on; the bytes pragma is ignored. The \f(CW\*(C`UTF8\*(C'\fR flag being on does \fBnot\fR mean that there are any characters of code points greater than 255 (or 127) in the scalar or that there are even any characters in the scalar. What the \f(CW\*(C`UTF8\*(C'\fR flag means is that the sequence of octets in the representation of the scalar is the sequence of \s-1UTF\-8\s0 encoded code points of the characters of a string. The \f(CW\*(C`UTF8\*(C'\fR flag being off means that each octet in this representation encodes a single character with code point 0..255 within the string. Perl's Unicode model is not to use \s-1UTF\-8\s0 until it is absolutely necessary. .IP "\(bu" 4 \&\f(CW\*(C`uvuni_to_utf8(buf, chr)\*(C'\fR writes a Unicode character code point into a buffer encoding the code point as \s-1UTF\-8\s0, and returns a pointer pointing after the \s-1UTF\-8\s0 bytes. .IP "\(bu" 4 \&\f(CW\*(C`utf8_to_uvuni(buf, lenp)\*(C'\fR reads \s-1UTF\-8\s0 encoded bytes from a buffer and returns the Unicode character code point and, optionally, the length of the \s-1UTF\-8\s0 byte sequence. .IP "\(bu" 4 \&\f(CW\*(C`utf8_length(start, end)\*(C'\fR returns the length of the \s-1UTF\-8\s0 encoded buffer in characters. \f(CW\*(C`sv_len_utf8(sv)\*(C'\fR returns the length of the \s-1UTF\-8\s0 encoded scalar. .IP "\(bu" 4 \&\f(CW\*(C`sv_utf8_upgrade(sv)\*(C'\fR converts the string of the scalar to its \s-1UTF\-8\s0 encoded form. \f(CW\*(C`sv_utf8_downgrade(sv)\*(C'\fR does the opposite, if possible. \f(CW\*(C`sv_utf8_encode(sv)\*(C'\fR is like sv_utf8_upgrade except that it does not set the \f(CW\*(C`UTF8\*(C'\fR flag. \f(CW\*(C`sv_utf8_decode()\*(C'\fR does the opposite of \f(CW\*(C`sv_utf8_encode()\*(C'\fR. Note that none of these are to be used as general-purpose encoding or decoding interfaces: \f(CW\*(C`use Encode\*(C'\fR for that. \f(CW\*(C`sv_utf8_upgrade()\*(C'\fR is affected by the encoding pragma but \f(CW\*(C`sv_utf8_downgrade()\*(C'\fR is not (since the encoding pragma is designed to be a one-way street). .IP "\(bu" 4 \&\f(CWis_utf8_char(s)\fR returns true if the pointer points to a valid \s-1UTF\-8\s0 character. .IP "\(bu" 4 \&\f(CW\*(C`is_utf8_string(buf, len)\*(C'\fR returns true if \f(CW\*(C`len\*(C'\fR bytes of the buffer are valid \s-1UTF\-8\s0. .IP "\(bu" 4 \&\f(CW\*(C`UTF8SKIP(buf)\*(C'\fR will return the number of bytes in the \s-1UTF\-8\s0 encoded character in the buffer. \f(CW\*(C`UNISKIP(chr)\*(C'\fR will return the number of bytes required to UTF\-8\-encode the Unicode character code point. \f(CW\*(C`UTF8SKIP()\*(C'\fR is useful for example for iterating over the characters of a \s-1UTF\-8\s0 encoded buffer; \f(CW\*(C`UNISKIP()\*(C'\fR is useful, for example, in computing the size required for a \s-1UTF\-8\s0 encoded buffer. .IP "\(bu" 4 \&\f(CW\*(C`utf8_distance(a, b)\*(C'\fR will tell the distance in characters between the two pointers pointing to the same \s-1UTF\-8\s0 encoded buffer. .IP "\(bu" 4 \&\f(CW\*(C`utf8_hop(s, off)\*(C'\fR will return a pointer to an \s-1UTF\-8\s0 encoded buffer that is \f(CW\*(C`off\*(C'\fR (positive or negative) Unicode characters displaced from the \s-1UTF\-8\s0 buffer \f(CW\*(C`s\*(C'\fR. Be careful not to overstep the buffer: \&\f(CW\*(C`utf8_hop()\*(C'\fR will merrily run off the end or the beginning of the buffer if told to do so. .IP "\(bu" 4 \&\f(CW\*(C`pv_uni_display(dsv, spv, len, pvlim, flags)\*(C'\fR and \&\f(CW\*(C`sv_uni_display(dsv, ssv, pvlim, flags)\*(C'\fR are useful for debugging the output of Unicode strings and scalars. By default they are useful only for debugging\*(--they display \fBall\fR characters as hexadecimal code points\*(--but with the flags \f(CW\*(C`UNI_DISPLAY_ISPRINT\*(C'\fR, \&\f(CW\*(C`UNI_DISPLAY_BACKSLASH\*(C'\fR, and \f(CW\*(C`UNI_DISPLAY_QQ\*(C'\fR you can make the output more readable. .IP "\(bu" 4 \&\f(CW\*(C`ibcmp_utf8(s1, pe1, u1, l1, u1, s2, pe2, l2, u2)\*(C'\fR can be used to compare two strings case-insensitively in Unicode. For case-sensitive comparisons you can just use \f(CW\*(C`memEQ()\*(C'\fR and \f(CW\*(C`memNE()\*(C'\fR as usual. .PP For more information, see perlapi, and \fIutf8.c\fR and \fIutf8.h\fR in the Perl source code distribution. .SH "BUGS" .IX Header "BUGS" .Sh "Interaction with Locales" .IX Subsection "Interaction with Locales" Use of locales with Unicode data may lead to odd results. Currently, Perl attempts to attach 8\-bit locale info to characters in the range 0..255, but this technique is demonstrably incorrect for locales that use characters above that range when mapped into Unicode. Perl's Unicode support will also tend to run slower. Use of locales with Unicode is discouraged. .Sh "Interaction with Extensions" .IX Subsection "Interaction with Extensions" When Perl exchanges data with an extension, the extension should be able to understand the \s-1UTF\-8\s0 flag and act accordingly. If the extension doesn't know about the flag, it's likely that the extension will return incorrectly-flagged data. .PP So if you're working with Unicode data, consult the documentation of every module you're using if there are any issues with Unicode data exchange. If the documentation does not talk about Unicode at all, suspect the worst and probably look at the source to learn how the module is implemented. Modules written completely in Perl shouldn't cause problems. Modules that directly or indirectly access code written in other programming languages are at risk. .PP For affected functions, the simple strategy to avoid data corruption is to always make the encoding of the exchanged data explicit. Choose an encoding that you know the extension can handle. Convert arguments passed to the extensions to that encoding and convert results back from that encoding. Write wrapper functions that do the conversions for you, so you can later change the functions when the extension catches up. .PP To provide an example, let's say the popular Foo::Bar::escape_html function doesn't deal with Unicode data yet. The wrapper function would convert the argument to raw \s-1UTF\-8\s0 and convert the result back to Perl's internal representation like so: .PP .Vb 5 \& sub my_escape_html ($) { \& my($what) = shift; \& return unless defined $what; \& Encode::decode_utf8(Foo::Bar::escape_html(Encode::encode_utf8($what))); \& } .Ve .PP Sometimes, when the extension does not convert data but just stores and retrieves them, you will be in a position to use the otherwise dangerous \fIEncode::_utf8_on()\fR function. Let's say the popular \&\f(CW\*(C`Foo::Bar\*(C'\fR extension, written in C, provides a \f(CW\*(C`param\*(C'\fR method that lets you store and retrieve data according to these prototypes: .PP .Vb 2 \& $self->param($name, $value); # set a scalar \& $value = $self->param($name); # retrieve a scalar .Ve .PP If it does not yet provide support for any encoding, one could write a derived class with such a \f(CW\*(C`param\*(C'\fR method: .PP .Vb 12 \& sub param { \& my($self,$name,$value) = @_; \& utf8::upgrade($name); # make sure it is UTF-8 encoded \& if (defined $value) \& utf8::upgrade($value); # make sure it is UTF-8 encoded \& return $self->SUPER::param($name,$value); \& } else { \& my $ret = $self->SUPER::param($name); \& Encode::_utf8_on($ret); # we know, it is UTF-8 encoded \& return $ret; \& } \& } .Ve .PP Some extensions provide filters on data entry/exit points, such as DB_File::filter_store_key and family. Look out for such filters in the documentation of your extensions, they can make the transition to Unicode data much easier. .Sh "Speed" .IX Subsection "Speed" Some functions are slower when working on \s-1UTF\-8\s0 encoded strings than on byte encoded strings. All functions that need to hop over characters such as \fIlength()\fR, \fIsubstr()\fR or \fIindex()\fR, or matching regular expressions can work \fBmuch\fR faster when the underlying data are byte\-encoded. .PP In Perl 5.8.0 the slowness was often quite spectacular; in Perl 5.8.1 a caching scheme was introduced which will hopefully make the slowness somewhat less spectacular, at least for some operations. In general, operations with \s-1UTF\-8\s0 encoded strings are still slower. As an example, the Unicode properties (character classes) like \f(CW\*(C`\ep{Nd}\*(C'\fR are known to be quite a bit slower (5\-20 times) than their simpler counterparts like \f(CW\*(C`\ed\*(C'\fR (then again, there 268 Unicode characters matching \f(CW\*(C`Nd\*(C'\fR compared with the 10 \s-1ASCII\s0 characters matching \f(CW\*(C`d\*(C'\fR). .Sh "Porting code from perl\-5.6.X" .IX Subsection "Porting code from perl-5.6.X" Perl 5.8 has a different Unicode model from 5.6. In 5.6 the programmer was required to use the \f(CW\*(C`utf8\*(C'\fR pragma to declare that a given scope expected to deal with Unicode data and had to make sure that only Unicode data were reaching that scope. If you have code that is working with 5.6, you will need some of the following adjustments to your code. The examples are written such that the code will continue to work under 5.6, so you should be safe to try them out. .IP "\(bu" 4 A filehandle that should read or write \s-1UTF\-8\s0 .Sp .Vb 3 \& if ($] > 5.007) { \& binmode $fh, ":utf8"; \& } .Ve .IP "\(bu" 4 A scalar that is going to be passed to some extension .Sp Be it Compress::Zlib, Apache::Request or any extension that has no mention of Unicode in the manpage, you need to make sure that the \&\s-1UTF\-8\s0 flag is stripped off. Note that at the time of this writing (October 2002) the mentioned modules are not UTF\-8\-aware. Please check the documentation to verify if this is still true. .Sp .Vb 4 \& if ($] > 5.007) { \& require Encode; \& $val = Encode::encode_utf8($val); # make octets \& } .Ve .IP "\(bu" 4 A scalar we got back from an extension .Sp If you believe the scalar comes back as \s-1UTF\-8\s0, you will most likely want the \s-1UTF\-8\s0 flag restored: .Sp .Vb 4 \& if ($] > 5.007) { \& require Encode; \& $val = Encode::decode_utf8($val); \& } .Ve .IP "\(bu" 4 Same thing, if you are really sure it is \s-1UTF\-8\s0 .Sp .Vb 4 \& if ($] > 5.007) { \& require Encode; \& Encode::_utf8_on($val); \& } .Ve .IP "\(bu" 4 A wrapper for fetchrow_array and fetchrow_hashref .Sp When the database contains only \s-1UTF\-8\s0, a wrapper function or method is a convenient way to replace all your fetchrow_array and fetchrow_hashref calls. A wrapper function will also make it easier to adapt to future enhancements in your database driver. Note that at the time of this writing (October 2002), the \s-1DBI\s0 has no standardized way to deal with \s-1UTF\-8\s0 data. Please check the documentation to verify if that is still true. .Sp .Vb 26 \& sub fetchrow { \& my($self, $sth, $what) = @_; # $what is one of fetchrow_{array,hashref} \& if ($] < 5.007) { \& return $sth->$what; \& } else { \& require Encode; \& if (wantarray) { \& my @arr = $sth->$what; \& for (@arr) { \& defined && /[^\e000-\e177]/ && Encode::_utf8_on($_); \& } \& return @arr; \& } else { \& my $ret = $sth->$what; \& if (ref $ret) { \& for my $k (keys %$ret) { \& defined && /[^\e000-\e177]/ && Encode::_utf8_on($_) for $ret->{$k}; \& } \& return $ret; \& } else { \& defined && /[^\e000-\e177]/ && Encode::_utf8_on($_) for $ret; \& return $ret; \& } \& } \& } \& } .Ve .IP "\(bu" 4 A large scalar that you know can only contain \s-1ASCII\s0 .Sp Scalars that contain only \s-1ASCII\s0 and are marked as \s-1UTF\-8\s0 are sometimes a drag to your program. If you recognize such a situation, just remove the \s-1UTF\-8\s0 flag: .Sp .Vb 1 \& utf8::downgrade($val) if $] > 5.007; .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" perluniintro, encoding, Encode, open, utf8, bytes, perlretut, \*(L"${^UNICODE}\*(R" in perlvar