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 a Perl scalar to/from UTF-8.
14           $num_octets = utf8::upgrade($string);
15           $success    = utf8::downgrade($string[, FAIL_OK]);
16
17           # Change the native bytes of a Perl scalar to/from UTF-8 bytes.
18           utf8::encode($string);
19           utf8::decode($string);
20
21           $flag = utf8::is_utf8(STRING); # since Perl 5.8.1
22           $flag = utf8::valid(STRING);
23

DESCRIPTION

25       The "use utf8" pragma tells the Perl parser to allow UTF-8 in the pro‐
26       gram text in the current lexical scope (allow UTF-EBCDIC on EBCDIC
27       based platforms).  The "no utf8" pragma tells Perl to switch back to
28       treating the source text as literal bytes in the current lexical scope.
29
30       This pragma is primarily a compatibility device.  Perl versions earlier
31       than 5.6 allowed arbitrary bytes in source code, whereas in future we
32       would like to standardize on the UTF-8 encoding for source text.
33
34       Do not use this pragma for anything else than telling Perl that your
35       script is written in UTF-8. The utility functions described below are
36       useful for their own purposes, but they are not really part of the
37       "pragmatic" effect.
38
39       Until UTF-8 becomes the default format for source text, either this
40       pragma or the encoding pragma should be used to recognize UTF-8 in the
41       source.  When UTF-8 becomes the standard source format, this pragma
42       will effectively become a no-op.  For convenience in what follows the
43       term UTF-X is used to refer to UTF-8 on ASCII and ISO Latin based plat‐
44       forms and UTF-EBCDIC on EBCDIC based platforms.
45
46       See also the effects of the "-C" switch and its cousin, the
47       $ENV{PERL_UNICODE}, in perlrun.
48
49       Enabling the "utf8" pragma has the following effect:
50
51       ·   Bytes in the source text that have their high-bit set will be
52           treated as being part of a literal UTF-8 character.  This includes
53           most literals such as identifier names, string constants, and con‐
54           stant regular expression patterns.
55
56           On EBCDIC platforms characters in the Latin 1 character set are
57           treated as being part of a literal UTF-EBCDIC character.
58
59       Note that if you have bytes with the eighth bit on in your script (for
60       example embedded Latin-1 in your string literals), "use utf8" will be
61       unhappy since the bytes are most probably not well-formed UTF-8.  If
62       you want to have such bytes and use utf8, you can disable utf8 until
63       the end the block (or file, if at top level) by "no utf8;".
64
65       If you want to automatically upgrade your 8-bit legacy bytes to UTF-8,
66       use the encoding pragma instead of this pragma.  For example, if you
67       want to implicitly upgrade your ISO 8859-1 (Latin-1) bytes to UTF-8 as
68       used in e.g. "chr()" and "\x{...}", try this:
69
70           use encoding "latin-1";
71           my $c = chr(0xc4);
72           my $x = "\x{c5}";
73
74       In case you are wondering: yes, "use encoding 'utf8';" works much the
75       same as "use utf8;".
76
77       Utility functions
78
79       The following functions are defined in the "utf8::" package by the Perl
80       core.  You do not need to say "use utf8" to use these and in fact you
81       should not say that  unless you really want to have UTF-8 source code.
82
83       * $num_octets = utf8::upgrade($string)
84           Converts in-place the octet sequence in the native encoding
85           (Latin-1 or EBCDIC) to the equivalent character sequence in UTF-X.
86           $string already encoded as characters does no harm.  Returns the
87           number of octets necessary to represent the string as UTF-X.  Can
88           be used to make sure that the UTF-8 flag is on, so that "\w" or
89           "lc()" work as Unicode on strings containing characters in the
90           range 0x80-0xFF (on ASCII and derivatives).
91
92           Note that this function does not handle arbitrary encodings.
93           Therefore Encode.pm is recommended for the general purposes.
94
95           Affected by the encoding pragma.
96
97       * $success = utf8::downgrade($string[, FAIL_OK])
98           Converts in-place the character sequence in UTF-X to the equivalent
99           octet sequence in the native encoding (Latin-1 or EBCDIC).  $string
100           already encoded as octets does no harm.  Returns true on success.
101           On failure dies or, if the value of "FAIL_OK" is true, returns
102           false.  Can be used to make sure that the UTF-8 flag is off, e.g.
103           when you want to make sure that the substr() or length() function
104           works with the usually faster byte algorithm.
105
106           Note that this function does not handle arbitrary encodings.
107           Therefore Encode.pm is recommended for the general purposes.
108
109           Not affected by the encoding pragma.
110
111           NOTE: this function is experimental and may change or be removed
112           without notice.
113
114       * utf8::encode($string)
115           Converts in-place the character sequence to the corresponding octet
116           sequence in UTF-X.  The UTF-8 flag is turned off.  Returns nothing.
117
118           Note that this function does not handle arbitrary encodings.
119           Therefore Encode.pm is recommended for the general purposes.
120
121       * utf8::decode($string)
122           Attempts to convert in-place the octet sequence in UTF-X to the
123           corresponding character sequence.  The UTF-8 flag is turned on only
124           if the source string contains multiple-byte UTF-X characters.  If
125           $string is invalid as UTF-X, returns false; otherwise returns true.
126
127           Note that this function does not handle arbitrary encodings.
128           Therefore Encode.pm is recommended for the general purposes.
129
130           NOTE: this function is experimental and may change or be removed
131           without notice.
132
133       * $flag = utf8::is_utf8(STRING)
134           (Since Perl 5.8.1)  Test whether STRING is in UTF-8.  Functionally
135           the same as Encode::is_utf8().
136
137       * $flag = utf8::valid(STRING)
138           [INTERNAL] Test whether STRING is in a consistent state regarding
139           UTF-8.  Will return true is well-formed UTF-8 and has the UTF-8
140           flag on or if string is held as bytes (both these states are 'con‐
141           sistent').  Main reason for this routine is to allow Perl's test‐
142           suite to check that operations have left strings in a consistent
143           state.  You most probably want to use utf8::is_utf8() instead.
144
145       "utf8::encode" is like "utf8::upgrade", but the UTF8 flag is cleared.
146       See perlunicode for more on the UTF8 flag and the C API functions
147       "sv_utf8_upgrade", "sv_utf8_downgrade", "sv_utf8_encode", and
148       "sv_utf8_decode", which are wrapped by the Perl functions
149       "utf8::upgrade", "utf8::downgrade", "utf8::encode" and "utf8::decode".
150       Note that in the Perl 5.8.0 and 5.8.1 implementation the functions
151       utf8::is_utf8, utf8::valid, utf8::encode, utf8::decode, utf8::upgrade,
152       and utf8::downgrade are always available, without a "require utf8"
153       statement-- this may change in future releases.
154

BUGS

156       One can have Unicode in identifier names, but not in package/class or
157       subroutine names.  While some limited functionality towards this does
158       exist as of Perl 5.8.0, that is more accidental than designed; use of
159       Unicode for the said purposes is unsupported.
160
161       One reason of this unfinishedness is its (currently) inherent unporta‐
162       bility: since both package names and subroutine names may need to be
163       mapped to file and directory names, the Unicode capability of the
164       filesystem becomes important-- and there unfortunately aren't portable
165       answers.
166

SEE ALSO

168       perluniintro, encoding, perlrun, bytes, perlunicode
169
170
171
172perl v5.8.8                       2001-09-21                         utf8(3pm)
Impressum