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