1utf8(3pm) Perl Programmers Reference Guide utf8(3pm)
2
3
4
6 utf8 - Perl pragma to enable/disable UTF-8 (or UTF-EBCDIC) in source
7 code
8
10 use utf8;
11 no utf8;
12
13 # Convert the internal representation of a Perl scalar to/from UTF-8.
14
15 $num_octets = utf8::upgrade($string);
16 $success = utf8::downgrade($string[, $fail_ok]);
17
18 # Change each character of a Perl scalar to/from a series of
19 # characters that represent the UTF-8 bytes of each original character.
20
21 utf8::encode($string); # "\x{100}" becomes "\xc4\x80"
22 utf8::decode($string); # "\xc4\x80" becomes "\x{100}"
23
24 # Convert a code point from the platform native character set to
25 # Unicode, and vice-versa.
26 $unicode = utf8::native_to_unicode(ord('A')); # returns 65 on both
27 # ASCII and EBCDIC
28 # platforms
29 $native = utf8::unicode_to_native(65); # returns 65 on ASCII
30 # platforms; 193 on
31 # EBCDIC
32
33 $flag = utf8::is_utf8($string); # since Perl 5.8.1
34 $flag = utf8::valid($string);
35
37 The "use utf8" pragma tells the Perl parser to allow UTF-8 in the
38 program text in the current lexical scope. The "no utf8" pragma tells
39 Perl to switch back to treating the source text as literal bytes in the
40 current lexical scope. (On EBCDIC platforms, technically it is
41 allowing UTF-EBCDIC, and not UTF-8, but this distinction is academic,
42 so in this document the term UTF-8 is used to mean both).
43
44 Do not use this pragma for anything else than telling Perl that your
45 script is written in UTF-8. The utility functions described below are
46 directly usable without "use utf8;".
47
48 Because it is not possible to reliably tell UTF-8 from native 8 bit
49 encodings, you need either a Byte Order Mark at the beginning of your
50 source code, or "use utf8;", to instruct perl.
51
52 When UTF-8 becomes the standard source format, this pragma will
53 effectively become a no-op.
54
55 See also the effects of the "-C" switch and its cousin, the
56 "PERL_UNICODE" environment variable, in perlrun.
57
58 Enabling the "utf8" pragma has the following effect:
59
60 · Bytes in the source text that are not in the ASCII character set
61 will be treated as being part of a literal UTF-8 sequence. This
62 includes most literals such as identifier names, string constants,
63 and constant regular expression patterns.
64
65 Note that if you have non-ASCII, non-UTF-8 bytes in your script (for
66 example embedded Latin-1 in your string literals), "use utf8" will be
67 unhappy. If you want to have such bytes under "use utf8", you can
68 disable this pragma until the end the block (or file, if at top level)
69 by "no utf8;".
70
71 Utility functions
72 The following functions are defined in the "utf8::" package by the Perl
73 core. You do not need to say "use utf8" to use these and in fact you
74 should not say that unless you really want to have UTF-8 source code.
75
76 · "$num_octets = utf8::upgrade($string)"
77
78 (Since Perl v5.8.0) Converts in-place the internal representation
79 of the string from an octet sequence in the native encoding
80 (Latin-1 or EBCDIC) to UTF-8. The logical character sequence itself
81 is unchanged. If $string is already upgraded, then this is a no-
82 op. Returns the number of octets necessary to represent the string
83 as UTF-8.
84
85 If your code needs to be compatible with versions of perl without
86 "use feature 'unicode_strings';", you can force Unicode semantics
87 on a given string:
88
89 # force unicode semantics for $string without the
90 # "unicode_strings" feature
91 utf8::upgrade($string);
92
93 For example:
94
95 # without explicit or implicit use feature 'unicode_strings'
96 my $x = "\xDF"; # LATIN SMALL LETTER SHARP S
97 $x =~ /ss/i; # won't match
98 my $y = uc($x); # won't convert
99 utf8::upgrade($x);
100 $x =~ /ss/i; # matches
101 my $z = uc($x); # converts to "SS"
102
103 Note that this function does not handle arbitrary encodings; use
104 Encode instead.
105
106 · "$success = utf8::downgrade($string[, $fail_ok])"
107
108 (Since Perl v5.8.0) Converts in-place the internal representation
109 of the string from UTF-8 to the equivalent octet sequence in the
110 native encoding (Latin-1 or EBCDIC). The logical character sequence
111 itself is unchanged. If $string is already stored as native 8 bit,
112 then this is a no-op. Can be used to make sure that the UTF-8 flag
113 is off, e.g. when you want to make sure that the substr() or
114 length() function works with the usually faster byte algorithm.
115
116 Fails if the original UTF-8 sequence cannot be represented in the
117 native 8 bit encoding. On failure dies or, if the value of $fail_ok
118 is true, returns false.
119
120 Returns true on success.
121
122 If your code expects an octet sequence this can be used to validate
123 that you've received one:
124
125 # throw an exception if not representable as octets
126 utf8::downgrade($string)
127
128 # or do your own error handling
129 utf8::downgrade($string, 1) or die "string must be octets";
130
131 Note that this function does not handle arbitrary encodings; use
132 Encode instead.
133
134 · "utf8::encode($string)"
135
136 (Since Perl v5.8.0) Converts in-place the character sequence to the
137 corresponding octet sequence in Perl's extended UTF-8. That is,
138 every (possibly wide) character gets replaced with a sequence of
139 one or more characters that represent the individual UTF-8 bytes of
140 the character. The UTF8 flag is turned off. Returns nothing.
141
142 my $x = "\x{100}"; # $x contains one character, with ord 0x100
143 utf8::encode($x); # $x contains two characters, with ords (on
144 # ASCII platforms) 0xc4 and 0x80. On EBCDIC
145 # 1047, this would instead be 0x8C and 0x41.
146
147 Similar to:
148
149 use Encode;
150 $x = Encode::encode("utf8", $x);
151
152 Note that this function does not handle arbitrary encodings; use
153 Encode instead.
154
155 · "$success = utf8::decode($string)"
156
157 (Since Perl v5.8.0) Attempts to convert in-place the octet sequence
158 encoded in Perl's extended UTF-8 to the corresponding character
159 sequence. That is, it replaces each sequence of characters in the
160 string whose ords represent a valid (extended) UTF-8 byte sequence,
161 with the corresponding single character. The UTF-8 flag is turned
162 on only if the source string contains multiple-byte UTF-8
163 characters. If $string is invalid as extended UTF-8, returns
164 false; otherwise returns true.
165
166 my $x = "\xc4\x80"; # $x contains two characters, with ords
167 # 0xc4 and 0x80
168 utf8::decode($x); # On ASCII platforms, $x contains one char,
169 # with ord 0x100. Since these bytes aren't
170 # legal UTF-EBCDIC, on EBCDIC platforms, $x is
171 # unchanged and the function returns FALSE.
172
173 Note that this function does not handle arbitrary encodings; use
174 Encode instead.
175
176 · "$unicode = utf8::native_to_unicode($code_point)"
177
178 (Since Perl v5.8.0) This takes an unsigned integer (which
179 represents the ordinal number of a character (or a code point) on
180 the platform the program is being run on) and returns its Unicode
181 equivalent value. Since ASCII platforms natively use the Unicode
182 code points, this function returns its input on them. On EBCDIC
183 platforms it converts from EBCDIC to Unicode.
184
185 A meaningless value will currently be returned if the input is not
186 an unsigned integer.
187
188 Since Perl v5.22.0, calls to this function are optimized out on
189 ASCII platforms, so there is no performance hit in using it there.
190
191 · "$native = utf8::unicode_to_native($code_point)"
192
193 (Since Perl v5.8.0) This is the inverse of
194 "utf8::native_to_unicode()", converting the other direction.
195 Again, on ASCII platforms, this returns its input, but on EBCDIC
196 platforms it will find the native platform code point, given any
197 Unicode one.
198
199 A meaningless value will currently be returned if the input is not
200 an unsigned integer.
201
202 Since Perl v5.22.0, calls to this function are optimized out on
203 ASCII platforms, so there is no performance hit in using it there.
204
205 · "$flag = utf8::is_utf8($string)"
206
207 (Since Perl 5.8.1) Test whether $string is marked internally as
208 encoded in UTF-8. Functionally the same as
209 "Encode::is_utf8($string)".
210
211 Typically only necessary for debugging and testing, if you need to
212 dump the internals of an SV, Devel::Peek's Dump() provides more
213 detail in a compact form.
214
215 If you still think you need this outside of debugging, testing or
216 dealing with filenames, you should probably read perlunitut and
217 "What is "the UTF8 flag"?" in perlunifaq.
218
219 Don't use this flag as a marker to distinguish character and binary
220 data: that should be decided for each variable when you write your
221 code.
222
223 To force unicode semantics in code portable to perl 5.8 and 5.10,
224 call "utf8::upgrade($string)" unconditionally.
225
226 · "$flag = utf8::valid($string)"
227
228 [INTERNAL] Test whether $string is in a consistent state regarding
229 UTF-8. Will return true if it is well-formed Perl extended UTF-8
230 and has the UTF-8 flag on or if $string is held as bytes (both
231 these states are 'consistent'). The main reason for this routine
232 is to allow Perl's test suite to check that operations have left
233 strings in a consistent state.
234
235 "utf8::encode" is like "utf8::upgrade", but the UTF8 flag is cleared.
236 See perlunicode, and the C API functions "sv_utf8_upgrade",
237 ""sv_utf8_downgrade" in perlapi", ""sv_utf8_encode" in perlapi", and
238 ""sv_utf8_decode" in perlapi", which are wrapped by the Perl functions
239 "utf8::upgrade", "utf8::downgrade", "utf8::encode" and "utf8::decode".
240 Also, the functions "utf8::is_utf8", "utf8::valid", "utf8::encode",
241 "utf8::decode", "utf8::upgrade", and "utf8::downgrade" are actually
242 internal, and thus always available, without a "require utf8"
243 statement.
244
246 Some filesystems may not support UTF-8 file names, or they may be
247 supported incompatibly with Perl. Therefore UTF-8 names that are
248 visible to the filesystem, such as module names may not work.
249
251 perlunitut, perluniintro, perlrun, bytes, perlunicode
252
253
254
255perl v5.30.1 2019-11-29 utf8(3pm)