This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Fix for
[perl5.git] / lib / utf8.pm
CommitLineData
a0ed51b3
LW
1package utf8;
2
663b9db3 3
d5448623
GS
4$utf8::hint_bits = 0x00800000;
5
b75c8c73
MS
6our $VERSION = '1.00';
7
a0ed51b3 8sub import {
d5448623 9 $^H |= $utf8::hint_bits;
a0ed51b3
LW
10 $enc{caller()} = $_[1] if $_[1];
11}
12
13sub unimport {
d5448623 14 $^H &= ~$utf8::hint_bits;
a0ed51b3
LW
15}
16
17sub AUTOLOAD {
18 require "utf8_heavy.pl";
daf4d4ea
SC
19 goto &$AUTOLOAD if defined &$AUTOLOAD;
20 Carp::croak("Undefined subroutine $AUTOLOAD called");
a0ed51b3
LW
21}
22
231;
24__END__
25
26=head1 NAME
27
b3419ed8 28utf8 - Perl pragma to enable/disable UTF-8 (or UTF-EBCDIC) in source code
a0ed51b3
LW
29
30=head1 SYNOPSIS
31
32 use utf8;
33 no utf8;
34
35=head1 DESCRIPTION
36
393fec97 37WARNING: The implementation of Unicode support in Perl is incomplete.
21bad921 38See L<perlunicode> for the exact details.
a0ed51b3 39
393fec97 40The C<use utf8> pragma tells the Perl parser to allow UTF-8 in the
b3419ed8
PK
41program text in the current lexical scope (allow UTF-EBCDIC on EBCDIC based
42platforms). The C<no utf8> pragma tells Perl to switch back to treating
43the source text as literal bytes in the current lexical scope.
a0ed51b3 44
393fec97
GS
45This pragma is primarily a compatibility device. Perl versions
46earlier than 5.6 allowed arbitrary bytes in source code, whereas
47in future we would like to standardize on the UTF-8 encoding for
48source text. Until UTF-8 becomes the default format for source
49text, this pragma should be used to recognize UTF-8 in the source.
50When UTF-8 becomes the standard source format, this pragma will
b3419ed8
PK
51effectively become a no-op. For convenience in what follows the
52term UTF-X is used to refer to UTF-8 on ASCII and ISO Latin based
53platforms and UTF-EBCDIC on EBCDIC based platforms.
a0ed51b3 54
393fec97 55Enabling the C<utf8> pragma has the following effects:
a0ed51b3 56
4ac9195f 57=over 4
a0ed51b3
LW
58
59=item *
60
393fec97
GS
61Bytes in the source text that have their high-bit set will be treated
62as being part of a literal UTF-8 character. This includes most literals
63such as identifiers, string constants, constant regular expression patterns
395f5a0c
PK
64and package names. On EBCDIC platforms characters in the Latin 1
65character set are treated as being part of a literal UTF-EBCDIC character.
a0ed51b3
LW
66
67=item *
68
b3419ed8 69In the absence of inputs marked as UTF-X, regular expressions within the
393fec97
GS
70scope of this pragma will default to using character semantics instead
71of byte semantics.
a0ed51b3 72
393fec97 73 @bytes_or_chars = split //, $data; # may split to bytes if data
b3419ed8 74 # $data isn't UTF-X
393fec97
GS
75 {
76 use utf8; # force char semantics
77 @chars = split //, $data; # splits characters
a0ed51b3
LW
78 }
79
4ac9195f
MS
80=back
81
1b026014
NIS
82=head2 Utility functions
83
84The following functions are defined in the C<utf8::> package by the perl core.
85
86=over 4
87
88=item * $num_octets = utf8::upgrade($string);
89
90Converts internal representation of string to the perls internal UTF-X form.
91Returns the number of octets necessary to represent the string as UTF-X.
92
93=item * utf8::downgrade($string[, CHECK])
94
95Converts internal representation of string to be un-encoded bytes.
96
97=item * utf8::encode($string)
98
99Converts (in-place) I<$string> from logical characters to octet sequence
100representing it in perl's UTF-X encoding.
101
102=item * $flag = utf8::decode($string)
103
b3419ed8 104Attempts to convert I<$string> in-place from perl's UTF-X encoding into logical characters.
1b026014
NIS
105
106=back
107
393fec97 108=head1 SEE ALSO
a0ed51b3 109
8058d7ab 110L<perlunicode>, L<bytes>
a0ed51b3
LW
111
112=cut