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 stored as UTF-8, then this is
82           a no-op. Returns the number of octets necessary to represent the
83           string as UTF-8.  Can be used to make sure that the UTF-8 flag is
84           on, so that "\w" or "lc()" work as Unicode on strings containing
85           non-ASCII characters whose code points are below 256.
86
87           Note that this function does not handle arbitrary encodings; use
88           Encode instead.
89
90       ·   "$success = utf8::downgrade($string[, $fail_ok])"
91
92           (Since Perl v5.8.0) Converts in-place the internal representation
93           of the string from UTF-8 to the equivalent octet sequence in the
94           native encoding (Latin-1 or EBCDIC). The logical character sequence
95           itself is unchanged. If $string is already stored as native 8 bit,
96           then this is a no-op.  Can be used to make sure that the UTF-8 flag
97           is off, e.g. when you want to make sure that the substr() or
98           length() function works with the usually faster byte algorithm.
99
100           Fails if the original UTF-8 sequence cannot be represented in the
101           native 8 bit encoding. On failure dies or, if the value of $fail_ok
102           is true, returns false.
103
104           Returns true on success.
105
106           Note that this function does not handle arbitrary encodings; use
107           Encode instead.
108
109       ·   "utf8::encode($string)"
110
111           (Since Perl v5.8.0) Converts in-place the character sequence to the
112           corresponding octet sequence in UTF-8. That is, every (possibly
113           wide) character gets replaced with a sequence of one or more
114           characters that represent the individual UTF-8 bytes of the
115           character.  The UTF8 flag is turned off.  Returns nothing.
116
117            my $a = "\x{100}"; # $a contains one character, with ord 0x100
118            utf8::encode($a);  # $a contains two characters, with ords (on
119                               # ASCII platforms) 0xc4 and 0x80.  On EBCDIC
120                               # 1047, this would instead be 0x8C and 0x41.
121
122           Note that this function does not handle arbitrary encodings; use
123           Encode instead.
124
125       ·   "$success = utf8::decode($string)"
126
127           (Since Perl v5.8.0) Attempts to convert in-place the octet sequence
128           encoded as UTF-8 to the corresponding character sequence. That is,
129           it replaces each sequence of characters in the string whose ords
130           represent a valid UTF-8 byte sequence, with the corresponding
131           single character.  The UTF-8 flag is turned on only if the source
132           string contains multiple-byte UTF-8 characters.  If $string is
133           invalid as UTF-8, returns false; otherwise returns true.
134
135            my $a = "\xc4\x80"; # $a contains two characters, with ords
136                                # 0xc4 and 0x80
137            utf8::decode($a);   # On ASCII platforms, $a contains one char,
138                                # with ord 0x100.   Since these bytes aren't
139                                # legal UTF-EBCDIC, on EBCDIC platforms, $a is
140                                # unchanged and the function returns FALSE.
141
142           Note that this function does not handle arbitrary encodings; use
143           Encode instead.
144
145       ·   "$unicode = utf8::native_to_unicode($code_point)"
146
147           (Since Perl v5.8.0) This takes an unsigned integer (which
148           represents the ordinal number of a character (or a code point) on
149           the platform the program is being run on) and returns its Unicode
150           equivalent value.  Since ASCII platforms natively use the Unicode
151           code points, this function returns its input on them.  On EBCDIC
152           platforms it converts from EBCDIC to Unicode.
153
154           A meaningless value will currently be returned if the input is not
155           an unsigned integer.
156
157           Since Perl v5.22.0, calls to this function are optimized out on
158           ASCII platforms, so there is no performance hit in using it there.
159
160       ·   "$native = utf8::unicode_to_native($code_point)"
161
162           (Since Perl v5.8.0) This is the inverse of
163           "utf8::native_to_unicode()", converting the other direction.
164           Again, on ASCII platforms, this returns its input, but on EBCDIC
165           platforms it will find the native platform code point, given any
166           Unicode one.
167
168           A meaningless value will currently be returned if the input is not
169           an unsigned integer.
170
171           Since Perl v5.22.0, calls to this function are optimized out on
172           ASCII platforms, so there is no performance hit in using it there.
173
174       ·   "$flag = utf8::is_utf8($string)"
175
176           (Since Perl 5.8.1)  Test whether $string is marked internally as
177           encoded in UTF-8.  Functionally the same as "Encode::is_utf8()".
178
179       ·   "$flag = utf8::valid($string)"
180
181           [INTERNAL] Test whether $string is in a consistent state regarding
182           UTF-8.  Will return true if it is well-formed UTF-8 and has the
183           UTF-8 flag on or if $string is held as bytes (both these states are
184           'consistent').  Main reason for this routine is to allow Perl's
185           test suite to check that operations have left strings in a
186           consistent state.  You most probably want to use "utf8::is_utf8()"
187           instead.
188
189       "utf8::encode" is like "utf8::upgrade", but the UTF8 flag is cleared.
190       See perlunicode, and the C API functions "sv_utf8_upgrade",
191       ""sv_utf8_downgrade" in perlapi", ""sv_utf8_encode" in perlapi", and
192       ""sv_utf8_decode" in perlapi", which are wrapped by the Perl functions
193       "utf8::upgrade", "utf8::downgrade", "utf8::encode" and "utf8::decode".
194       Also, the functions "utf8::is_utf8", "utf8::valid", "utf8::encode",
195       "utf8::decode", "utf8::upgrade", and "utf8::downgrade" are actually
196       internal, and thus always available, without a "require utf8"
197       statement.
198

BUGS

200       Some filesystems may not support UTF-8 file names, or they may be
201       supported incompatibly with Perl.  Therefore UTF-8 names that are
202       visible to the filesystem, such as module names may not work.
203

SEE ALSO

205       perlunitut, perluniintro, perlrun, bytes, perlunicode
206
207
208
209perl v5.26.3                      2018-03-01                         utf8(3pm)
Impressum