1utf8(3pm)              Perl Programmers Reference Guide              utf8(3pm)
2
3
4

NAME

6       utf8 - Perl pragma to enable/disable UTF-8 (or UTF-EBCDIC) in source
7       code
8

SYNOPSIS

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

DESCRIPTION

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.  Since Perl v5.38, if $string is "undef" no action is
84           taken; prior to that, it would be converted to be defined and zero-
85           length.
86
87           If your code needs to be compatible with versions of perl without
88           "use feature 'unicode_strings';", you can force Unicode semantics
89           on a given string:
90
91             # force unicode semantics for $string without the
92             # "unicode_strings" feature
93             utf8::upgrade($string);
94
95           For example:
96
97             # without explicit or implicit use feature 'unicode_strings'
98             my $x = "\xDF";    # LATIN SMALL LETTER SHARP S
99             $x =~ /ss/i;       # won't match
100             my $y = uc($x);    # won't convert
101             utf8::upgrade($x);
102             $x =~ /ss/i;       # matches
103             my $z = uc($x);    # converts to "SS"
104
105           Note that this function does not handle arbitrary encodings; use
106           Encode instead.
107
108       •   "$success = utf8::downgrade($string[, $fail_ok])"
109
110           (Since Perl v5.8.0) Converts in-place the internal representation
111           of the string from UTF-8 to the equivalent octet sequence in the
112           native encoding (Latin-1 or EBCDIC). The logical character sequence
113           itself is unchanged. If $string is already stored as native 8 bit,
114           then this is a no-op.  Can be used to make sure that the UTF-8 flag
115           is off, e.g. when you want to make sure that the substr() or
116           length() function works with the usually faster byte algorithm.
117
118           Fails if the original UTF-8 sequence cannot be represented in the
119           native 8 bit encoding. On failure dies or, if the value of $fail_ok
120           is true, returns false.
121
122           Returns true on success.
123
124           If your code expects an octet sequence this can be used to validate
125           that you've received one:
126
127             # throw an exception if not representable as octets
128             utf8::downgrade($string)
129
130             # or do your own error handling
131             utf8::downgrade($string, 1) or die "string must be octets";
132
133           Note that this function does not handle arbitrary encodings; use
134           Encode instead.
135
136       •   utf8::encode($string)
137
138           (Since Perl v5.8.0) Converts in-place the character sequence to the
139           corresponding octet sequence in Perl's extended UTF-8. That is,
140           every (possibly wide) character gets replaced with a sequence of
141           one or more characters that represent the individual UTF-8 bytes of
142           the character.  The UTF8 flag is turned off.  Returns nothing.
143
144            my $x = "\x{100}"; # $x contains one character, with ord 0x100
145            utf8::encode($x);  # $x contains two characters, with ords (on
146                               # ASCII platforms) 0xc4 and 0x80.  On EBCDIC
147                               # 1047, this would instead be 0x8C and 0x41.
148
149           Similar to:
150
151             use Encode;
152             $x = Encode::encode("utf8", $x);
153
154           Note that this function does not handle arbitrary encodings; use
155           Encode instead.
156
157       •   "$success = utf8::decode($string)"
158
159           (Since Perl v5.8.0) Attempts to convert in-place the octet sequence
160           encoded in Perl's extended UTF-8 to the corresponding character
161           sequence. That is, it replaces each sequence of characters in the
162           string whose ords represent a valid (extended) UTF-8 byte sequence,
163           with the corresponding single character.  The UTF-8 flag is turned
164           on only if the source string contains multiple-byte UTF-8
165           characters.  If $string is invalid as extended UTF-8, returns
166           false; otherwise returns true.
167
168            my $x = "\xc4\x80"; # $x contains two characters, with ords
169                                # 0xc4 and 0x80
170            utf8::decode($x);   # On ASCII platforms, $x contains one char,
171                                # with ord 0x100.   Since these bytes aren't
172                                # legal UTF-EBCDIC, on EBCDIC platforms, $x is
173                                # unchanged and the function returns FALSE.
174            my $y = "\xc3\x83\xc2\xab"; This has been encoded twice; this
175                                # example is only for ASCII platforms
176            utf8::decode($y);   # Converts $y to \xc3\xab, returns TRUE;
177            utf8::decode($y);   # Further converts to \xeb, returns TRUE;
178            utf8::decode($y);   # Returns FALSE, leaves $y unchanged
179
180           Note that this function does not handle arbitrary encodings; use
181           Encode instead.
182
183       •   "$unicode = utf8::native_to_unicode($code_point)"
184
185           (Since Perl v5.8.0) This takes an unsigned integer (which
186           represents the ordinal number of a character (or a code point) on
187           the platform the program is being run on) and returns its Unicode
188           equivalent value.  Since ASCII platforms natively use the Unicode
189           code points, this function returns its input on them.  On EBCDIC
190           platforms it converts from EBCDIC to Unicode.
191
192           A meaningless value will currently be returned if the input is not
193           an unsigned integer.
194
195           Since Perl v5.22.0, calls to this function are optimized out on
196           ASCII platforms, so there is no performance hit in using it there.
197
198       •   "$native = utf8::unicode_to_native($code_point)"
199
200           (Since Perl v5.8.0) This is the inverse of
201           utf8::native_to_unicode(), converting the other direction.  Again,
202           on ASCII platforms, this returns its input, but on EBCDIC platforms
203           it will find the native platform code point, given any Unicode one.
204
205           A meaningless value will currently be returned if the input is not
206           an unsigned integer.
207
208           Since Perl v5.22.0, calls to this function are optimized out on
209           ASCII platforms, so there is no performance hit in using it there.
210
211       •   "$flag = utf8::is_utf8($string)"
212
213           (Since Perl 5.8.1)  Test whether $string is marked internally as
214           encoded in UTF-8.  Functionally the same as
215           Encode::is_utf8($string).
216
217           Typically only necessary for debugging and testing, if you need to
218           dump the internals of an SV, Devel::Peek's Dump() provides more
219           detail in a compact form.
220
221           If you still think you need this outside of debugging, testing or
222           dealing with filenames, you should probably read perlunitut and
223           "What is "the UTF8 flag"?" in perlunifaq.
224
225           Don't use this flag as a marker to distinguish character and binary
226           data: that should be decided for each variable when you write your
227           code.
228
229           To force unicode semantics in code portable to perl 5.8 and 5.10,
230           call utf8::upgrade($string) unconditionally.
231
232       •   "$flag = utf8::valid($string)"
233
234           [INTERNAL] Test whether $string is in a consistent state regarding
235           UTF-8.  Will return true if it is well-formed Perl extended UTF-8
236           and has the UTF-8 flag on or if $string is held as bytes (both
237           these states are 'consistent').  The main reason for this routine
238           is to allow Perl's test suite to check that operations have left
239           strings in a consistent state.
240
241       "utf8::encode" is like "utf8::upgrade", but the UTF8 flag is cleared.
242       See perlunicode, and the C API functions "sv_utf8_upgrade",
243       ""sv_utf8_downgrade" in perlapi", ""sv_utf8_encode" in perlapi", and
244       ""sv_utf8_decode" in perlapi", which are wrapped by the Perl functions
245       "utf8::upgrade", "utf8::downgrade", "utf8::encode" and "utf8::decode".
246       Also, the functions "utf8::is_utf8", "utf8::valid", "utf8::encode",
247       "utf8::decode", "utf8::upgrade", and "utf8::downgrade" are actually
248       internal, and thus always available, without a "require utf8"
249       statement.
250

BUGS

252       Some filesystems may not support UTF-8 file names, or they may be
253       supported incompatibly with Perl.  Therefore UTF-8 names that are
254       visible to the filesystem, such as module names may not work.
255

SEE ALSO

257       perlunitut, perluniintro, perlrun, bytes, perlunicode
258
259
260
261perl v5.38.2                      2023-11-30                         utf8(3pm)
Impressum