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