6 our $utf8_hint_bits = 0x00800000;
7 our $ascii_hint_bits = 0x00000010; # Turned off when utf8 turned on
13 $^H |= $utf8_hint_bits;
14 $^H &= ~$ascii_hint_bits;
18 $^H &= ~$utf8_hint_bits;
22 goto &$AUTOLOAD if defined &$AUTOLOAD;
24 Carp::croak("Undefined subroutine $AUTOLOAD called");
32 utf8 - Perl pragma to enable/disable UTF-8 (or UTF-EBCDIC) in source code
39 # Convert the internal representation of a Perl scalar to/from UTF-8.
41 $num_octets = utf8::upgrade($string);
42 $success = utf8::downgrade($string[, $fail_ok]);
44 # Change each character of a Perl scalar to/from a series of
45 # characters that represent the UTF-8 bytes of each original character.
47 utf8::encode($string); # "\x{100}" becomes "\xc4\x80"
48 utf8::decode($string); # "\xc4\x80" becomes "\x{100}"
50 # Convert a code point from the platform native character set to
51 # Unicode, and vice-versa.
52 $unicode = utf8::native_to_unicode(ord('A')); # returns 65 on both
55 $native = utf8::unicode_to_native(65); # returns 65 on ASCII
59 $flag = utf8::is_utf8($string); # since Perl 5.8.1
60 $flag = utf8::valid($string);
64 The C<use utf8> pragma tells the Perl parser to allow UTF-8 in the
65 program text in the current lexical scope. The C<no utf8> pragma tells Perl
66 to switch back to treating the source text as literal bytes in the current
67 lexical scope. (On EBCDIC platforms, technically it is allowing UTF-EBCDIC,
68 and not UTF-8, but this distinction is academic, so in this document the term
69 UTF-8 is used to mean both).
71 B<Do not use this pragma for anything else than telling Perl that your
72 script is written in UTF-8.> The utility functions described below are
73 directly usable without C<use utf8;>.
75 Because it is not possible to reliably tell UTF-8 from native 8 bit
76 encodings, you need either a Byte Order Mark at the beginning of your
77 source code, or C<use utf8;>, to instruct perl.
79 When UTF-8 becomes the standard source format, this pragma will
80 effectively become a no-op.
82 See also the effects of the C<-C> switch and its cousin, the
83 C<PERL_UNICODE> environment variable, in L<perlrun>.
85 Enabling the C<utf8> pragma has the following effect:
91 Bytes in the source text that are not in the ASCII character set will be
92 treated as being part of a literal UTF-8 sequence. This includes most
93 literals such as identifier names, string constants, and constant
94 regular expression patterns.
98 Note that if you have non-ASCII, non-UTF-8 bytes in your script (for example
99 embedded Latin-1 in your string literals), C<use utf8> will be unhappy. If
100 you want to have such bytes under C<use utf8>, you can disable this pragma
101 until the end the block (or file, if at top level) by C<no utf8;>.
103 =head2 Utility functions
105 The following functions are defined in the C<utf8::> package by the
106 Perl core. You do not need to say C<use utf8> to use these and in fact
107 you should not say that unless you really want to have UTF-8 source code.
111 =item * C<$num_octets = utf8::upgrade($string)>
114 Converts in-place the internal representation of the string from an octet
115 sequence in the native encoding (Latin-1 or EBCDIC) to UTF-8. The
116 logical character sequence itself is unchanged. If I<$string> is already
117 upgraded, then this is a no-op. Returns the
118 number of octets necessary to represent the string as UTF-8.
119 Since Perl v5.38, if C<$string> is C<undef> no action is taken; prior to that,
120 it would be converted to be defined and zero-length.
122 If your code needs to be compatible with versions of perl without
123 C<use feature 'unicode_strings';>, you can force Unicode semantics on
126 # force unicode semantics for $string without the
127 # "unicode_strings" feature
128 utf8::upgrade($string);
132 # without explicit or implicit use feature 'unicode_strings'
133 my $x = "\xDF"; # LATIN SMALL LETTER SHARP S
134 $x =~ /ss/i; # won't match
135 my $y = uc($x); # won't convert
137 $x =~ /ss/i; # matches
138 my $z = uc($x); # converts to "SS"
140 B<Note that this function does not handle arbitrary encodings>;
141 use L<Encode> instead.
143 =item * C<$success = utf8::downgrade($string[, $fail_ok])>
146 Converts in-place the internal representation of the string from UTF-8 to the
147 equivalent octet sequence in the native encoding (Latin-1 or EBCDIC). The
148 logical character sequence itself is unchanged. If I<$string> is already
149 stored as native 8 bit, then this is a no-op. Can be used to make sure that
150 the UTF-8 flag is off, e.g. when you want to make sure that the substr() or
151 length() function works with the usually faster byte algorithm.
153 Fails if the original UTF-8 sequence cannot be represented in the
154 native 8 bit encoding. On failure dies or, if the value of I<$fail_ok> is
157 Returns true on success.
159 If your code expects an octet sequence this can be used to validate
160 that you've received one:
162 # throw an exception if not representable as octets
163 utf8::downgrade($string)
165 # or do your own error handling
166 utf8::downgrade($string, 1) or die "string must be octets";
168 B<Note that this function does not handle arbitrary encodings>;
169 use L<Encode> instead.
171 =item * C<utf8::encode($string)>
174 Converts in-place the character sequence to the corresponding octet
175 sequence in Perl's extended UTF-8. That is, every (possibly wide) character
176 gets replaced with a sequence of one or more characters that represent the
177 individual UTF-8 bytes of the character. The UTF8 flag is turned off.
180 my $x = "\x{100}"; # $x contains one character, with ord 0x100
181 utf8::encode($x); # $x contains two characters, with ords (on
182 # ASCII platforms) 0xc4 and 0x80. On EBCDIC
183 # 1047, this would instead be 0x8C and 0x41.
188 $x = Encode::encode("utf8", $x);
190 B<Note that this function does not handle arbitrary encodings>;
191 use L<Encode> instead.
193 =item * C<$success = utf8::decode($string)>
196 Attempts to convert in-place the octet sequence encoded in Perl's extended
197 UTF-8 to the corresponding character sequence. That is, it replaces each
198 sequence of characters in the string whose ords represent a valid (extended)
199 UTF-8 byte sequence, with the corresponding single character. The UTF-8 flag
200 is turned on only if the source string contains multiple-byte UTF-8
201 characters. If I<$string> is invalid as extended UTF-8, returns false;
202 otherwise returns true.
204 my $x = "\xc4\x80"; # $x contains two characters, with ords
206 utf8::decode($x); # On ASCII platforms, $x contains one char,
207 # with ord 0x100. Since these bytes aren't
208 # legal UTF-EBCDIC, on EBCDIC platforms, $x is
209 # unchanged and the function returns FALSE.
210 my $y = "\xc3\x83\xc2\xab"; This has been encoded twice; this
211 # example is only for ASCII platforms
212 utf8::decode($y); # Converts $y to \xc3\xab, returns TRUE;
213 utf8::decode($y); # Further converts to \xeb, returns TRUE;
214 utf8::decode($y); # Returns FALSE, leaves $y unchanged
216 B<Note that this function does not handle arbitrary encodings>;
217 use L<Encode> instead.
219 =item * C<$unicode = utf8::native_to_unicode($code_point)>
222 This takes an unsigned integer (which represents the ordinal number of a
223 character (or a code point) on the platform the program is being run on) and
224 returns its Unicode equivalent value. Since ASCII platforms natively use the
225 Unicode code points, this function returns its input on them. On EBCDIC
226 platforms it converts from EBCDIC to Unicode.
228 A meaningless value will currently be returned if the input is not an unsigned
231 Since Perl v5.22.0, calls to this function are optimized out on ASCII
232 platforms, so there is no performance hit in using it there.
234 =item * C<$native = utf8::unicode_to_native($code_point)>
237 This is the inverse of C<utf8::native_to_unicode()>, converting the other
238 direction. Again, on ASCII platforms, this returns its input, but on EBCDIC
239 platforms it will find the native platform code point, given any Unicode one.
241 A meaningless value will currently be returned if the input is not an unsigned
244 Since Perl v5.22.0, calls to this function are optimized out on ASCII
245 platforms, so there is no performance hit in using it there.
247 =item * C<$flag = utf8::is_utf8($string)>
249 (Since Perl 5.8.1) Test whether I<$string> is marked internally as encoded in
250 UTF-8. Functionally the same as C<Encode::is_utf8($string)>.
252 Typically only necessary for debugging and testing, if you need to
253 dump the internals of an SV, L<Devel::Peek's|Devel::Peek> Dump()
254 provides more detail in a compact form.
256 If you still think you need this outside of debugging, testing or
257 dealing with filenames, you should probably read L<perlunitut> and
258 L<perlunifaq/What is "the UTF8 flag"?>.
260 Don't use this flag as a marker to distinguish character and binary
261 data: that should be decided for each variable when you write your
264 To force unicode semantics in code portable to perl 5.8 and 5.10, call
265 C<utf8::upgrade($string)> unconditionally.
267 =item * C<$flag = utf8::valid($string)>
269 [INTERNAL] Test whether I<$string> is in a consistent state regarding
270 UTF-8. Will return true if it is well-formed Perl extended UTF-8 and has the
272 on B<or> if I<$string> is held as bytes (both these states are 'consistent').
273 The main reason for this routine is to allow Perl's test suite to check
274 that operations have left strings in a consistent state.
278 C<utf8::encode> is like C<utf8::upgrade>, but the UTF8 flag is
279 cleared. See L<perlunicode>, and the C API
280 functions C<L<sv_utf8_upgrade|perlapi/sv_utf8_upgrade>>,
281 C<L<perlapi/sv_utf8_downgrade>>, C<L<perlapi/sv_utf8_encode>>,
282 and C<L<perlapi/sv_utf8_decode>>, which are wrapped by the Perl functions
283 C<utf8::upgrade>, C<utf8::downgrade>, C<utf8::encode> and
284 C<utf8::decode>. Also, the functions C<utf8::is_utf8>, C<utf8::valid>,
285 C<utf8::encode>, C<utf8::decode>, C<utf8::upgrade>, and C<utf8::downgrade> are
286 actually internal, and thus always available, without a C<require utf8>
291 Some filesystems may not support UTF-8 file names, or they may be supported
292 incompatibly with Perl. Therefore UTF-8 names that are visible to the
293 filesystem, such as module names may not work.
297 L<perlunitut>, L<perluniintro>, L<perlrun>, L<bytes>, L<perlunicode>