Commit | Line | Data |
a0ed51b3 |
1 | package utf8; |
2 | |
393fec97 |
3 | $^U = 1 if caller and caller eq 'main'; # they are unicode aware |
4 | # XXX split this out? |
d41ff1b8 |
5 | |
a0ed51b3 |
6 | sub import { |
393fec97 |
7 | $^H |= 0x00800000; |
a0ed51b3 |
8 | $enc{caller()} = $_[1] if $_[1]; |
9 | } |
10 | |
11 | sub unimport { |
393fec97 |
12 | $^H &= ~0x00800000; |
a0ed51b3 |
13 | } |
14 | |
15 | sub AUTOLOAD { |
16 | require "utf8_heavy.pl"; |
17 | goto &$AUTOLOAD; |
18 | } |
19 | |
20 | 1; |
21 | __END__ |
22 | |
23 | =head1 NAME |
24 | |
393fec97 |
25 | utf8 - Perl pragma to enable/disable UTF-8 in source code |
a0ed51b3 |
26 | |
27 | =head1 SYNOPSIS |
28 | |
29 | use utf8; |
30 | no utf8; |
31 | |
32 | =head1 DESCRIPTION |
33 | |
393fec97 |
34 | WARNING: The implementation of Unicode support in Perl is incomplete. |
35 | Expect sudden and unannounced changes! |
a0ed51b3 |
36 | |
393fec97 |
37 | The C<use utf8> pragma tells the Perl parser to allow UTF-8 in the |
38 | program text in the current lexical scope. The C<no utf8> pragma |
39 | tells Perl to switch back to treating the source text as literal |
40 | bytes in the current lexical scope. |
a0ed51b3 |
41 | |
393fec97 |
42 | This pragma is primarily a compatibility device. Perl versions |
43 | earlier than 5.6 allowed arbitrary bytes in source code, whereas |
44 | in future we would like to standardize on the UTF-8 encoding for |
45 | source text. Until UTF-8 becomes the default format for source |
46 | text, this pragma should be used to recognize UTF-8 in the source. |
47 | When UTF-8 becomes the standard source format, this pragma will |
48 | effectively become a no-op. |
a0ed51b3 |
49 | |
393fec97 |
50 | Enabling the C<utf8> pragma has the following effects: |
a0ed51b3 |
51 | |
393fec97 |
52 | =over |
a0ed51b3 |
53 | |
54 | =item * |
55 | |
393fec97 |
56 | Bytes in the source text that have their high-bit set will be treated |
57 | as being part of a literal UTF-8 character. This includes most literals |
58 | such as identifiers, string constants, constant regular expression patterns |
59 | and package names. |
a0ed51b3 |
60 | |
61 | =item * |
62 | |
393fec97 |
63 | As a side effect, when this pragma is used within the main package, |
64 | it also enables Unicode character semantics for the entire program. |
65 | See L<perlunicode> for more on that. |
a0ed51b3 |
66 | |
393fec97 |
67 | [XXX: split this out into separate "pragma" and/or -C command-line |
68 | switch?] |
a0ed51b3 |
69 | |
70 | =item * |
71 | |
393fec97 |
72 | In the absence of inputs marked as UTF-8, regular expressions within the |
73 | scope of this pragma will default to using character semantics instead |
74 | of byte semantics. |
a0ed51b3 |
75 | |
393fec97 |
76 | @bytes_or_chars = split //, $data; # may split to bytes if data |
77 | # $data isn't UTF-8 |
78 | { |
79 | use utf8; # force char semantics |
80 | @chars = split //, $data; # splits characters |
a0ed51b3 |
81 | } |
82 | |
393fec97 |
83 | [XXX: Should this should be enabled like chr()/sprintf("%c") by looking |
84 | at $^U instead?] |
a0ed51b3 |
85 | |
393fec97 |
86 | =head1 SEE ALSO |
a0ed51b3 |
87 | |
393fec97 |
88 | L<perlunicode>, L<byte> |
a0ed51b3 |
89 | |
90 | =cut |