Debian Codegraph @

upstream/mageia-cauldron/man3pm/utf8.3pm

raw
1 .\" -*- mode: troff; coding: utf-8 -*-
2 .\" Automatically generated by Pod::Man v6.0.2 (Pod::Simple 3.45)
3 .\"
4 .\" Standard preamble:
5 .\" ========================================================================
6 .de Sp \" Vertical space (when we can't use .PP)
7 .if t .sp .5v
8 .if n .sp
9 ..
10 .de Vb \" Begin verbatim text
11 .ft CW
12 .nf
13 .ne \\$1
14 ..
15 .de Ve \" End verbatim text
16 .ft R
17 .fi
18 ..
19 .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
20 .ie n \{\
21 . ds C` ""
22 . ds C' ""
23 'br\}
24 .el\{\
25 . ds C`
26 . ds C'
27 'br\}
28 .\"
29 .\" Escape single quotes in literal strings from groff's Unicode transform.
30 .ie \n(.g .ds Aq \(aq
31 .el .ds Aq '
32 .\"
33 .\" If the F register is >0, we'll generate index entries on stderr for
34 .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
35 .\" entries marked with X<> in POD. Of course, you'll have to process the
36 .\" output yourself in some meaningful fashion.
37 .\"
38 .\" Avoid warning from groff about undefined register 'F'.
39 .de IX
40 ..
41 .nr rF 0
42 .if \n(.g .if rF .nr rF 1
43 .if (\n(rF:(\n(.g==0)) \{\
44 . if \nF \{\
45 . de IX
46 . tm Index:\\$1\t\\n%\t"\\$2"
47 ..
48 . if !\nF==2 \{\
49 . nr % 0
50 . nr F 2
51 . \}
52 . \}
53 .\}
54 .rr rF
55 .\"
56 .\" Required to disable full justification in groff 1.23.0.
57 .if n .ds AD l
58 .\" ========================================================================
59 .\"
60 .IX Title "utf8 3pm"
61 .TH utf8 3pm 2025-06-24 "perl v5.42.0" "Perl Programmers Reference Guide"
62 .\" For nroff, turn off justification. Always turn off hyphenation; it makes
63 .\" way too many mistakes in technical documents.
64 .if n .ad l
65 .nh
66 .SH NAME
67 utf8 \- Perl pragma to enable/disable UTF\-8 (or UTF\-EBCDIC) in source code
68 .SH SYNOPSIS
69 .IX Header "SYNOPSIS"
70 .Vb 2
71 \& use utf8;
72 \& no utf8;
73 \&
74 \& # Convert the internal representation of a Perl scalar to/from UTF\-8.
75 \&
76 \& $num_octets = utf8::upgrade($string);
77 \& $success = utf8::downgrade($string[, $fail_ok]);
78 \&
79 \& # Change each character of a Perl scalar to/from a series of
80 \& # characters that represent the UTF\-8 bytes of each original character.
81 \&
82 \& utf8::encode($string); # "\ex{100}" becomes "\exc4\ex80"
83 \& utf8::decode($string); # "\exc4\ex80" becomes "\ex{100}"
84 \&
85 \& # Convert a code point from the platform native character set to
86 \& # Unicode, and vice\-versa.
87 \& $unicode = utf8::native_to_unicode(ord(\*(AqA\*(Aq)); # returns 65 on both
88 \& # ASCII and EBCDIC
89 \& # platforms
90 \& $native = utf8::unicode_to_native(65); # returns 65 on ASCII
91 \& # platforms; 193 on
92 \& # EBCDIC
93 \&
94 \& $flag = utf8::is_utf8($string); # since Perl 5.8.1
95 \& $flag = utf8::valid($string);
96 .Ve
97 .SH DESCRIPTION
98 .IX Header "DESCRIPTION"
99 The \f(CW\*(C`use utf8\*(C'\fR pragma tells the Perl parser to allow UTF\-8 in the
100 program text in the current lexical scope. The \f(CW\*(C`no utf8\*(C'\fR pragma tells Perl
101 to switch back to treating the source text as literal bytes in the current
102 lexical scope. (On EBCDIC platforms, technically it is allowing UTF\-EBCDIC,
103 and not UTF\-8, but this distinction is academic, so in this document the term
104 UTF\-8 is used to mean both).
105 .PP
106 \&\fBDo not use this pragma for anything else than telling Perl that your
107 script is written in UTF\-8.\fR The utility functions described below are
108 directly usable without \f(CW\*(C`use utf8;\*(C'\fR.
109 .PP
110 Because it is not possible to reliably tell UTF\-8 from native 8 bit
111 encodings, you need either a Byte Order Mark at the beginning of your
112 source code, or \f(CW\*(C`use utf8;\*(C'\fR, to instruct perl.
113 .PP
114 When UTF\-8 becomes the standard source format, this pragma will
115 effectively become a no\-op.
116 .PP
117 See also the effects of the \f(CW\*(C`\-C\*(C'\fR switch and its cousin, the
118 \&\f(CW\*(C`PERL_UNICODE\*(C'\fR environment variable, in perlrun.
119 .PP
120 Enabling the \f(CW\*(C`utf8\*(C'\fR pragma has the following effect:
121 .IP \(bu 4
122 Bytes in the source text that are not in the ASCII character set will be
123 treated as being part of a literal UTF\-8 sequence. This includes most
124 literals such as identifier names, string constants, and constant
125 regular expression patterns.
126 .PP
127 Note that if you have non\-ASCII, non\-UTF\-8 bytes in your script (for example
128 embedded Latin\-1 in your string literals), \f(CW\*(C`use utf8\*(C'\fR will be unhappy. If
129 you want to have such bytes under \f(CW\*(C`use utf8\*(C'\fR, you can disable this pragma
130 until the end the block (or file, if at top level) by \f(CW\*(C`no utf8;\*(C'\fR.
131 .SS "Utility functions"
132 .IX Subsection "Utility functions"
133 The following functions are defined in the \f(CW\*(C`utf8::\*(C'\fR package by the
134 Perl core. You do not need to say \f(CW\*(C`use utf8\*(C'\fR to use these and in fact
135 you should not say that unless you really want to have UTF\-8 source code.
136 .IP \(bu 4
137 \&\f(CW\*(C`$num_octets = utf8::upgrade($string)\*(C'\fR
138 .Sp
139 (Since Perl v5.8.0)
140 Converts in\-place the internal representation of the string from an octet
141 sequence in the native encoding (Latin\-1 or EBCDIC) to UTF\-8. The
142 logical character sequence itself is unchanged. If \fR\f(CI$string\fR\fI\fR is already
143 upgraded, then this is a no\-op. Returns the
144 number of octets necessary to represent the string as UTF\-8.
145 Since Perl v5.38, if \f(CW$string\fR is \f(CW\*(C`undef\*(C'\fR no action is taken; prior to that,
146 it would be converted to be defined and zero\-length.
147 .Sp
148 If your code needs to be compatible with versions of perl without
149 \&\f(CW\*(C`use feature \*(Aqunicode_strings\*(Aq;\*(C'\fR, you can force Unicode semantics on
150 a given string:
151 .Sp
152 .Vb 3
153 \& # force unicode semantics for $string without the
154 \& # "unicode_strings" feature
155 \& utf8::upgrade($string);
156 .Ve
157 .Sp
158 For example:
159 .Sp
160 .Vb 7
161 \& # without explicit or implicit use feature \*(Aqunicode_strings\*(Aq
162 \& my $x = "\exDF"; # LATIN SMALL LETTER SHARP S
163 \& $x =~ /ss/i; # won\*(Aqt match
164 \& my $y = uc($x); # won\*(Aqt convert
165 \& utf8::upgrade($x);
166 \& $x =~ /ss/i; # matches
167 \& my $z = uc($x); # converts to "SS"
168 .Ve
169 .Sp
170 \&\fBNote that this function does not handle arbitrary encodings\fR;
171 use Encode instead.
172 .IP \(bu 4
173 \&\f(CW\*(C`$success = utf8::downgrade($string[, $fail_ok])\*(C'\fR
174 .Sp
175 (Since Perl v5.8.0)
176 Converts in\-place the internal representation of the string from UTF\-8 to the
177 equivalent octet sequence in the native encoding (Latin\-1 or EBCDIC). The
178 logical character sequence itself is unchanged. If \fR\f(CI$string\fR\fI\fR is already
179 stored as native 8 bit, then this is a no\-op. Can be used to make sure that
180 the UTF\-8 flag is off, e.g. when you want to make sure that the \fBsubstr()\fR or
181 \&\fBlength()\fR function works with the usually faster byte algorithm.
182 .Sp
183 Fails if the original UTF\-8 sequence cannot be represented in the
184 native 8 bit encoding. On failure dies or, if the value of \fR\f(CI$fail_ok\fR\fI\fR is
185 true, returns false.
186 .Sp
187 Returns true on success.
188 .Sp
189 If your code expects an octet sequence this can be used to validate
190 that you\*(Aqve received one:
191 .Sp
192 .Vb 2
193 \& # throw an exception if not representable as octets
194 \& utf8::downgrade($string)
195 \&
196 \& # or do your own error handling
197 \& utf8::downgrade($string, 1) or die "string must be octets";
198 .Ve
199 .Sp
200 \&\fBNote that this function does not handle arbitrary encodings\fR;
201 use Encode instead.
202 .IP \(bu 4
203 \&\f(CWutf8::encode($string)\fR
204 .Sp
205 (Since Perl v5.8.0)
206 Converts in\-place the character sequence to the corresponding octet
207 sequence in Perl\*(Aqs extended UTF\-8. That is, every (possibly wide) character
208 gets replaced with a sequence of one or more characters that represent the
209 individual UTF\-8 bytes of the character. The UTF8 flag is turned off.
210 Returns nothing.
211 .Sp
212 .Vb 4
213 \& my $x = "\ex{100}"; # $x contains one character, with ord 0x100
214 \& utf8::encode($x); # $x contains two characters, with ords (on
215 \& # ASCII platforms) 0xc4 and 0x80. On EBCDIC
216 \& # 1047, this would instead be 0x8C and 0x41.
217 .Ve
218 .Sp
219 Similar to:
220 .Sp
221 .Vb 2
222 \& use Encode;
223 \& $x = Encode::encode("utf8", $x);
224 .Ve
225 .Sp
226 \&\fBNote that this function does not handle arbitrary encodings\fR;
227 use Encode instead.
228 .IP \(bu 4
229 \&\f(CW\*(C`$success = utf8::decode($string)\*(C'\fR
230 .Sp
231 (Since Perl v5.8.0)
232 Attempts to convert in\-place the octet sequence encoded in Perl\*(Aqs extended
233 UTF\-8 to the corresponding character sequence. That is, it replaces each
234 sequence of characters in the string whose ords represent a valid (extended)
235 UTF\-8 byte sequence, with the corresponding single character. The UTF\-8 flag
236 is turned on only if the source string contains multiple\-byte UTF\-8
237 characters. If \fR\f(CI$string\fR\fI\fR is invalid as extended UTF\-8, returns false;
238 otherwise returns true.
239 .Sp
240 .Vb 11
241 \& my $x = "\exc4\ex80"; # $x contains two characters, with ords
242 \& # 0xc4 and 0x80
243 \& utf8::decode($x); # On ASCII platforms, $x contains one char,
244 \& # with ord 0x100. Since these bytes aren\*(Aqt
245 \& # legal UTF\-EBCDIC, on EBCDIC platforms, $x is
246 \& # unchanged and the function returns FALSE.
247 \& my $y = "\exc3\ex83\exc2\exab"; This has been encoded twice; this
248 \& # example is only for ASCII platforms
249 \& utf8::decode($y); # Converts $y to \exc3\exab, returns TRUE;
250 \& utf8::decode($y); # Further converts to \exeb, returns TRUE;
251 \& utf8::decode($y); # Returns FALSE, leaves $y unchanged
252 .Ve
253 .Sp
254 \&\fBNote that this function does not handle arbitrary encodings\fR;
255 use Encode instead.
256 .IP \(bu 4
257 \&\f(CW\*(C`$unicode = utf8::native_to_unicode($code_point)\*(C'\fR
258 .Sp
259 (Since Perl v5.8.0)
260 This takes an unsigned integer (which represents the ordinal number of a
261 character (or a code point) on the platform the program is being run on) and
262 returns its Unicode equivalent value. Since ASCII platforms natively use the
263 Unicode code points, this function returns its input on them. On EBCDIC
264 platforms it converts from EBCDIC to Unicode.
265 .Sp
266 A meaningless value will currently be returned if the input is not an unsigned
267 integer.
268 .Sp
269 Since Perl v5.22.0, calls to this function are optimized out on ASCII
270 platforms, so there is no performance hit in using it there.
271 .IP \(bu 4
272 \&\f(CW\*(C`$native = utf8::unicode_to_native($code_point)\*(C'\fR
273 .Sp
274 (Since Perl v5.8.0)
275 This is the inverse of \f(CWutf8::native_to_unicode()\fR, converting the other
276 direction. Again, on ASCII platforms, this returns its input, but on EBCDIC
277 platforms it will find the native platform code point, given any Unicode one.
278 .Sp
279 A meaningless value will currently be returned if the input is not an unsigned
280 integer.
281 .Sp
282 Since Perl v5.22.0, calls to this function are optimized out on ASCII
283 platforms, so there is no performance hit in using it there.
284 .IP \(bu 4
285 \&\f(CW\*(C`$flag = utf8::is_utf8($string)\*(C'\fR
286 .Sp
287 (Since Perl 5.8.1) Test whether \fR\f(CI$string\fR\fI\fR is marked internally as encoded in
288 UTF\-8. Functionally the same as \f(CWEncode::is_utf8($string)\fR.
289 .Sp
290 Typically only necessary for debugging and testing, if you need to
291 dump the internals of an SV, Devel::Peek\*(Aqs \fBDump()\fR
292 provides more detail in a compact form.
293 .Sp
294 If you still think you need this outside of debugging, testing or
295 dealing with filenames, you should probably read perlunitut and
296 "What is "the UTF8 flag"?" in perlunifaq.
297 .Sp
298 Don\*(Aqt use this flag as a marker to distinguish character and binary
299 data: that should be decided for each variable when you write your
300 code.
301 .Sp
302 To force unicode semantics in code portable to perl 5.8 and 5.10, call
303 \&\f(CWutf8::upgrade($string)\fR unconditionally.
304 .IP \(bu 4
305 \&\f(CW\*(C`$flag = utf8::valid($string)\*(C'\fR
306 .Sp
307 [INTERNAL] Test whether \fR\f(CI$string\fR\fI\fR is in a consistent state regarding
308 UTF\-8. Will return true if it is well\-formed Perl extended UTF\-8 and has the
309 UTF\-8 flag
310 on \fBor\fR if \fI\fR\f(CI$string\fR\fI\fR is held as bytes (both these states are \*(Aqconsistent\*(Aq).
311 The main reason for this routine is to allow Perl\*(Aqs test suite to check
312 that operations have left strings in a consistent state.
313 .PP
314 \&\f(CW\*(C`utf8::encode\*(C'\fR is like \f(CW\*(C`utf8::upgrade\*(C'\fR, but the UTF8 flag is
315 cleared. See perlunicode, and the C API
316 functions \f(CW\*(C`sv_utf8_upgrade\*(C'\fR,
317 \&\f(CW\*(C`"sv_utf8_downgrade" in perlapi\*(C'\fR, \f(CW\*(C`"sv_utf8_encode" in perlapi\*(C'\fR,
318 and \f(CW\*(C`"sv_utf8_decode" in perlapi\*(C'\fR, which are wrapped by the Perl functions
319 \&\f(CW\*(C`utf8::upgrade\*(C'\fR, \f(CW\*(C`utf8::downgrade\*(C'\fR, \f(CW\*(C`utf8::encode\*(C'\fR and
320 \&\f(CW\*(C`utf8::decode\*(C'\fR. Also, the functions \f(CW\*(C`utf8::is_utf8\*(C'\fR, \f(CW\*(C`utf8::valid\*(C'\fR,
321 \&\f(CW\*(C`utf8::encode\*(C'\fR, \f(CW\*(C`utf8::decode\*(C'\fR, \f(CW\*(C`utf8::upgrade\*(C'\fR, and \f(CW\*(C`utf8::downgrade\*(C'\fR are
322 actually internal, and thus always available, without a \f(CW\*(C`require utf8\*(C'\fR
323 statement.
324 .SH BUGS
325 .IX Header "BUGS"
326 Some filesystems may not support UTF\-8 file names, or they may be supported
327 incompatibly with Perl. Therefore UTF\-8 names that are visible to the
328 filesystem, such as module names may not work.
329 .SH "SEE ALSO"
330 .IX Header "SEE ALSO"
331 perlunitut, perluniintro, perlrun, bytes, perlunicode