Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
9.7 KiB

  1. =head1 NAME
  2. perlunicode - Unicode support in Perl (EXPERIMENTAL, subject to change)
  3. =head1 DESCRIPTION
  4. =head2 Important Caveat
  5. WARNING: As of the 5.6.1 release, the implementation of Unicode
  6. support in Perl is incomplete, and continues to be highly experimental.
  7. The following areas need further work. They are being rapidly addressed
  8. in the 5.7.x development branch.
  9. =over 4
  10. =item Input and Output Disciplines
  11. There is currently no easy way to mark data read from a file or other
  12. external source as being utf8. This will be one of the major areas of
  13. focus in the near future.
  14. =item Regular Expressions
  15. The existing regular expression compiler does not produce polymorphic
  16. opcodes. This means that the determination on whether to match Unicode
  17. characters is made when the pattern is compiled, based on whether the
  18. pattern contains Unicode characters, and not when the matching happens
  19. at run time. This needs to be changed to adaptively match Unicode if
  20. the string to be matched is Unicode.
  21. =item C<use utf8> still needed to enable a few features
  22. The C<utf8> pragma implements the tables used for Unicode support. These
  23. tables are automatically loaded on demand, so the C<utf8> pragma need not
  24. normally be used.
  25. However, as a compatibility measure, this pragma must be explicitly used
  26. to enable recognition of UTF-8 encoded literals and identifiers in the
  27. source text.
  28. =back
  29. =head2 Byte and Character semantics
  30. Beginning with version 5.6, Perl uses logically wide characters to
  31. represent strings internally. This internal representation of strings
  32. uses the UTF-8 encoding.
  33. In future, Perl-level operations can be expected to work with characters
  34. rather than bytes, in general.
  35. However, as strictly an interim compatibility measure, Perl v5.6 aims to
  36. provide a safe migration path from byte semantics to character semantics
  37. for programs. For operations where Perl can unambiguously decide that the
  38. input data is characters, Perl now switches to character semantics.
  39. For operations where this determination cannot be made without additional
  40. information from the user, Perl decides in favor of compatibility, and
  41. chooses to use byte semantics.
  42. This behavior preserves compatibility with earlier versions of Perl,
  43. which allowed byte semantics in Perl operations, but only as long as
  44. none of the program's inputs are marked as being as source of Unicode
  45. character data. Such data may come from filehandles, from calls to
  46. external programs, from information provided by the system (such as %ENV),
  47. or from literals and constants in the source text.
  48. If the C<-C> command line switch is used, (or the ${^WIDE_SYSTEM_CALLS}
  49. global flag is set to C<1>), all system calls will use the
  50. corresponding wide character APIs. This is currently only implemented
  51. on Windows.
  52. Regardless of the above, the C<bytes> pragma can always be used to force
  53. byte semantics in a particular lexical scope. See L<bytes>.
  54. The C<utf8> pragma is primarily a compatibility device that enables
  55. recognition of UTF-8 in literals encountered by the parser. It may also
  56. be used for enabling some of the more experimental Unicode support features.
  57. Note that this pragma is only required until a future version of Perl
  58. in which character semantics will become the default. This pragma may
  59. then become a no-op. See L<utf8>.
  60. Unless mentioned otherwise, Perl operators will use character semantics
  61. when they are dealing with Unicode data, and byte semantics otherwise.
  62. Thus, character semantics for these operations apply transparently; if
  63. the input data came from a Unicode source (for example, by adding a
  64. character encoding discipline to the filehandle whence it came, or a
  65. literal UTF-8 string constant in the program), character semantics
  66. apply; otherwise, byte semantics are in effect. To force byte semantics
  67. on Unicode data, the C<bytes> pragma should be used.
  68. Under character semantics, many operations that formerly operated on
  69. bytes change to operating on characters. For ASCII data this makes
  70. no difference, because UTF-8 stores ASCII in single bytes, but for
  71. any character greater than C<chr(127)>, the character may be stored in
  72. a sequence of two or more bytes, all of which have the high bit set.
  73. But by and large, the user need not worry about this, because Perl
  74. hides it from the user. A character in Perl is logically just a number
  75. ranging from 0 to 2**32 or so. Larger characters encode to longer
  76. sequences of bytes internally, but again, this is just an internal
  77. detail which is hidden at the Perl level.
  78. =head2 Effects of character semantics
  79. Character semantics have the following effects:
  80. =over 4
  81. =item *
  82. Strings and patterns may contain characters that have an ordinal value
  83. larger than 255.
  84. Presuming you use a Unicode editor to edit your program, such characters
  85. will typically occur directly within the literal strings as UTF-8
  86. characters, but you can also specify a particular character with an
  87. extension of the C<\x> notation. UTF-8 characters are specified by
  88. putting the hexadecimal code within curlies after the C<\x>. For instance,
  89. a Unicode smiley face is C<\x{263A}>.
  90. =item *
  91. Identifiers within the Perl script may contain Unicode alphanumeric
  92. characters, including ideographs. (You are currently on your own when
  93. it comes to using the canonical forms of characters--Perl doesn't (yet)
  94. attempt to canonicalize variable names for you.)
  95. =item *
  96. Regular expressions match characters instead of bytes. For instance,
  97. "." matches a character instead of a byte. (However, the C<\C> pattern
  98. is provided to force a match a single byte ("C<char>" in C, hence
  99. C<\C>).)
  100. =item *
  101. Character classes in regular expressions match characters instead of
  102. bytes, and match against the character properties specified in the
  103. Unicode properties database. So C<\w> can be used to match an ideograph,
  104. for instance.
  105. =item *
  106. Named Unicode properties and block ranges make be used as character
  107. classes via the new C<\p{}> (matches property) and C<\P{}> (doesn't
  108. match property) constructs. For instance, C<\p{Lu}> matches any
  109. character with the Unicode uppercase property, while C<\p{M}> matches
  110. any mark character. Single letter properties may omit the brackets, so
  111. that can be written C<\pM> also. Many predefined character classes are
  112. available, such as C<\p{IsMirrored}> and C<\p{InTibetan}>.
  113. =item *
  114. The special pattern C<\X> match matches any extended Unicode sequence
  115. (a "combining character sequence" in Standardese), where the first
  116. character is a base character and subsequent characters are mark
  117. characters that apply to the base character. It is equivalent to
  118. C<(?:\PM\pM*)>.
  119. =item *
  120. The C<tr///> operator translates characters instead of bytes. Note
  121. that the C<tr///CU> functionality has been removed, as the interface
  122. was a mistake. For similar functionality see pack('U0', ...) and
  123. pack('C0', ...).
  124. =item *
  125. Case translation operators use the Unicode case translation tables
  126. when provided character input. Note that C<uc()> translates to
  127. uppercase, while C<ucfirst> translates to titlecase (for languages
  128. that make the distinction). Naturally the corresponding backslash
  129. sequences have the same semantics.
  130. =item *
  131. Most operators that deal with positions or lengths in the string will
  132. automatically switch to using character positions, including C<chop()>,
  133. C<substr()>, C<pos()>, C<index()>, C<rindex()>, C<sprintf()>,
  134. C<write()>, and C<length()>. Operators that specifically don't switch
  135. include C<vec()>, C<pack()>, and C<unpack()>. Operators that really
  136. don't care include C<chomp()>, as well as any other operator that
  137. treats a string as a bucket of bits, such as C<sort()>, and the
  138. operators dealing with filenames.
  139. =item *
  140. The C<pack()>/C<unpack()> letters "C<c>" and "C<C>" do I<not> change,
  141. since they're often used for byte-oriented formats. (Again, think
  142. "C<char>" in the C language.) However, there is a new "C<U>" specifier
  143. that will convert between UTF-8 characters and integers. (It works
  144. outside of the utf8 pragma too.)
  145. =item *
  146. The C<chr()> and C<ord()> functions work on characters. This is like
  147. C<pack("U")> and C<unpack("U")>, not like C<pack("C")> and
  148. C<unpack("C")>. In fact, the latter are how you now emulate
  149. byte-oriented C<chr()> and C<ord()> under utf8.
  150. =item *
  151. The bit string operators C<& | ^ ~> can operate on character data.
  152. However, for backward compatibility reasons (bit string operations
  153. when the characters all are less than 256 in ordinal value) one cannot
  154. mix C<~> (the bit complement) and characters both less than 256 and
  155. equal or greater than 256. Most importantly, the DeMorgan's laws
  156. (C<~($x|$y) eq ~$x&~$y>, C<~($x&$y) eq ~$x|~$y>) won't hold.
  157. Another way to look at this is that the complement cannot return
  158. B<both> the 8-bit (byte) wide bit complement, and the full character
  159. wide bit complement.
  160. =item *
  161. And finally, C<scalar reverse()> reverses by character rather than by byte.
  162. =back
  163. =head2 Character encodings for input and output
  164. [XXX: This feature is not yet implemented.]
  165. =head1 CAVEATS
  166. As of yet, there is no method for automatically coercing input and
  167. output to some encoding other than UTF-8. This is planned in the near
  168. future, however.
  169. Whether an arbitrary piece of data will be treated as "characters" or
  170. "bytes" by internal operations cannot be divined at the current time.
  171. Use of locales with utf8 may lead to odd results. Currently there is
  172. some attempt to apply 8-bit locale info to characters in the range
  173. 0..255, but this is demonstrably incorrect for locales that use
  174. characters above that range (when mapped into Unicode). It will also
  175. tend to run slower. Avoidance of locales is strongly encouraged.
  176. =head1 SEE ALSO
  177. L<bytes>, L<utf8>, L<perlvar/"${^WIDE_SYSTEM_CALLS}">
  178. =cut