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