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

BUGS

147       One can have Unicode in identifier names, but not in package/class or
148       subroutine names.  While some limited functionality towards this does
149       exist as of Perl 5.8.0, that is more accidental than designed; use of
150       Unicode for the said purposes is unsupported.
151
152       One reason of this unfinishedness is its (currently) inherent
153       unportability: since both package names and subroutine names may need
154       to be mapped to file and directory names, the Unicode capability of the
155       filesystem becomes important-- and there unfortunately aren't portable
156       answers.
157

SEE ALSO

159       perlunitut, perluniintro, perlrun, bytes, perlunicode
160
161
162
163perl v5.10.1                      2009-02-12                         utf8(3pm)
Impressum