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           $flag = utf8::is_utf8(STRING); # since Perl 5.8.1
25           $flag = utf8::valid(STRING);
26

DESCRIPTION

28       The "use utf8" pragma tells the Perl parser to allow UTF-8 in the
29       program text in the current lexical scope (allow UTF-EBCDIC on EBCDIC
30       based platforms).  The "no utf8" pragma tells Perl to switch back to
31       treating the source text as literal bytes in the current lexical scope.
32
33       Do not use this pragma for anything else than telling Perl that your
34       script is written in UTF-8. The utility functions described below are
35       directly usable without "use utf8;".
36
37       Because it is not possible to reliably tell UTF-8 from native 8 bit
38       encodings, you need either a Byte Order Mark at the beginning of your
39       source code, or "use utf8;", to instruct perl.
40
41       When UTF-8 becomes the standard source format, this pragma will
42       effectively become a no-op.  For convenience in what follows the term
43       UTF-X is used to refer to UTF-8 on ASCII and ISO Latin based platforms
44       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-X sequence.  This includes
53           most literals such as identifier names, string constants, and
54           constant 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-X.  If
62       you want to have such bytes under "use utf8", you can disable this
63       pragma until the end the block (or file, if at top level) by "no
64       utf8;".
65
66   Utility functions
67       The following functions are defined in the "utf8::" package by the Perl
68       core.  You do not need to say "use utf8" to use these and in fact you
69       should not say that  unless you really want to have UTF-8 source code.
70
71       ·   $num_octets = utf8::upgrade($string)
72
73           Converts in-place the internal representation of the string from an
74           octet sequence in the native encoding (Latin-1 or EBCDIC) to UTF-X.
75           The logical character sequence itself is unchanged.  If $string is
76           already stored as UTF-X, then this is a no-op. Returns the number
77           of octets necessary to represent the string as UTF-X.  Can be used
78           to make sure that the UTF-8 flag is on, so that "\w" or "lc()" work
79           as Unicode on strings containing characters in the range 0x80-0xFF
80           (on ASCII and derivatives).
81
82           Note that this function does not handle arbitrary encodings.
83           Therefore Encode is recommended for the general purposes; see also
84           Encode.
85
86       ·   $success = utf8::downgrade($string[, FAIL_OK])
87
88           Converts in-place the the internal representation of the string
89           from UTF-X to the equivalent octet sequence in the native encoding
90           (Latin-1 or EBCDIC). The logical character sequence itself is
91           unchanged. If $string is already stored as native 8 bit, then this
92           is a no-op.  Can be used to make sure that the UTF-8 flag is off,
93           e.g. when you want to make sure that the substr() or length()
94           function works with the usually faster byte algorithm.
95
96           Fails if the original UTF-X sequence cannot be represented in the
97           native 8 bit encoding. On failure dies or, if the value of
98           "FAIL_OK" is true, returns false.
99
100           Returns true on success.
101
102           Note that this function does not handle arbitrary encodings.
103           Therefore Encode is recommended for the general purposes; see also
104           Encode.
105
106       ·   utf8::encode($string)
107
108           Converts in-place the character sequence to the corresponding octet
109           sequence in UTF-X. That is, every (possibly wide) character gets
110           replaced with a sequence of one or more characters that represent
111           the individual UTF-X bytes of the character.  The UTF8 flag is
112           turned off.  Returns nothing.
113
114               my $a = "\x{100}"; # $a contains one character, with ord 0x100
115               utf8::encode($a);  # $a contains two characters, with ords 0xc4 and 0x80
116
117           Note that this function does not handle arbitrary encodings.
118           Therefore Encode is recommended for the general purposes; see also
119           Encode.
120
121       ·   $success = utf8::decode($string)
122
123           Attempts to convert in-place the octet sequence in UTF-X to the
124           corresponding character sequence. That is, it replaces each
125           sequence of characters in the string whose ords represent a valid
126           UTF-X byte sequence, with the corresponding single character.  The
127           UTF-8 flag is turned on only if the source string contains
128           multiple-byte UTF-X characters.  If $string is invalid as UTF-X,
129           returns false; otherwise returns true.
130
131               my $a = "\xc4\x80"; # $a contains two characters, with ords 0xc4 and 0x80
132               utf8::decode($a);   # $a contains one character, with ord 0x100
133
134           Note that this function does not handle arbitrary encodings.
135           Therefore Encode is recommended for the general purposes; see also
136           Encode.
137
138       ·   $flag = utf8::is_utf8(STRING)
139
140           (Since Perl 5.8.1)  Test whether STRING is in UTF-8 internally.
141           Functionally the same as Encode::is_utf8().
142
143       ·   $flag = utf8::valid(STRING)
144
145           [INTERNAL] Test whether STRING is in a consistent state regarding
146           UTF-8.  Will return true is well-formed UTF-8 and has the UTF-8
147           flag on or if string is held as bytes (both these states are
148           'consistent').  Main reason for this routine is to allow Perl's
149           testsuite to check that operations have left strings in a
150           consistent state.  You most probably want to use utf8::is_utf8()
151           instead.
152
153       "utf8::encode" is like "utf8::upgrade", but the UTF8 flag is cleared.
154       See perlunicode for more on the UTF8 flag and the C API functions
155       "sv_utf8_upgrade", "sv_utf8_downgrade", "sv_utf8_encode", and
156       "sv_utf8_decode", which are wrapped by the Perl functions
157       "utf8::upgrade", "utf8::downgrade", "utf8::encode" and "utf8::decode".
158       Also, the functions utf8::is_utf8, utf8::valid, utf8::encode,
159       utf8::decode, utf8::upgrade, and utf8::downgrade are actually internal,
160       and thus always available, without a "require utf8" statement.
161

BUGS

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

SEE ALSO

175       perlunitut, perluniintro, perlrun, bytes, perlunicode
176
177
178
179perl v5.12.4                      2011-06-07                         utf8(3pm)
Impressum