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