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