Upgrade to Encode 1.56, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / encoding.pm
1 package encoding;
2 our $VERSION = do { my @r = (q$Revision: 1.33 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
3
4 use Encode;
5 use strict;
6
7 BEGIN {
8     if (ord("A") == 193) {
9         require Carp;
10         Carp::croak "encoding pragma does not support EBCDIC platforms";
11     }
12 }
13
14 our $HAS_PERLIO = 0;
15 eval { require PerlIO::encoding };
16 unless ($@){
17     $HAS_PERLIO = (PerlIO::encoding->VERSION >= 0.02);
18 }
19
20 sub import {
21     my $class = shift;
22     my $name  = shift;
23     my %arg = @_;
24     $name ||= $ENV{PERL_ENCODING};
25
26     my $enc = find_encoding($name);
27     unless (defined $enc) {
28         require Carp;
29         Carp::croak "Unknown encoding '$name'";
30     }
31     unless ($arg{Filter}){
32         ${^ENCODING} = $enc; # this is all you need, actually.
33         $HAS_PERLIO or return 1;
34         for my $h (qw(STDIN STDOUT)){
35             if ($arg{$h}){
36                 unless (defined find_encoding($arg{$h})) {
37                     require Carp;
38                     Carp::croak "Unknown encoding for $h, '$arg{$h}'";
39                 }
40                 eval { binmode($h, ":encoding($arg{$h})") };
41             }else{
42                 unless (exists $arg{$h}){
43                     eval { 
44                         no warnings 'uninitialized';
45                         binmode($h, ":encoding($name)");
46                     };
47                 }
48             }
49             if ($@){
50                 require Carp;
51                 Carp::croak($@);
52             }
53         }
54     }else{
55         defined(${^ENCODING}) and undef ${^ENCODING};
56         eval {
57             require Filter::Util::Call ;
58             Filter::Util::Call->import ;
59             binmode(STDIN);
60             binmode(STDOUT);
61             filter_add(sub{
62                            my $status;
63                            if (($status = filter_read()) > 0){
64                                $_ = $enc->decode($_, 1);
65                                # warn $_;
66                            }
67                            $status ;
68                        });
69         };
70         # warn "Filter installed";
71     }
72     return 1; # I doubt if we need it, though
73 }
74
75 sub unimport{
76     no warnings;
77     undef ${^ENCODING};
78     binmode(STDIN);
79     binmode(STDOUT);
80     if ($INC{"Filter/Util/Call.pm"}){
81         eval { filter_del() };
82     }
83 }
84
85 1;
86 __END__
87
88 =pod
89
90 =head1 NAME
91
92 encoding - allows you to write your script in non-ascii or non-utf8
93
94 =head1 SYNOPSIS
95
96   use encoding "greek";  # Perl like Greek to you?
97   use encoding "euc-jp"; # Jperl!
98
99   # or you can even do this if your shell supports your native encoding
100
101   perl -Mencoding=latin2 -e '...' # Feeling centrally European?
102   perl -Mencoding=euc-kr -e '...' # Or Korean?
103
104   # or from the shebang line
105
106   #!/your/path/to/perl -Mencoding="8859-6" # Arabian Nights
107   #!/your/path/to/perl -Mencoding=big5     # Taiwanese
108
109   # more control
110
111   # A simple euc-cn => utf-8 converter
112   use encoding "euc-cn", STDOUT => "utf8";  while(<>){print};
113
114   # "no encoding;" supported (but not scoped!)
115   no encoding;
116
117   # an alternate way, Filter
118   use encoding "euc-jp", Filter=>1;
119   use utf8;
120   # now you can use kanji identifiers -- in euc-jp!
121
122 =head1 ABSTRACT
123
124 Let's start with a bit of history: Perl 5.6.0 introduced Unicode
125 support.  You could apply C<substr()> and regexes even to complex CJK
126 characters -- so long as the script was written in UTF-8.  But back
127 then, text editors that supported UTF-8 were still rare and many users
128 instead chose to write scripts in legacy encodings, giving up a whole
129 new feature of Perl 5.6.
130
131 Rewind to the future: starting from perl 5.8.0 with the B<encoding>
132 pragma, you can write your script in any encoding you like (so long
133 as the C<Encode> module supports it) and still enjoy Unicode support.
134 You can write code in EUC-JP as follows:
135
136   my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji
137                #<-char-><-char->   # 4 octets
138   s/\bCamel\b/$Rakuda/;
139
140 And with C<use encoding "euc-jp"> in effect, it is the same thing as
141 the code in UTF-8:
142
143   my $Rakuda = "\x{99F1}\x{99DD}"; # two Unicode Characters
144   s/\bCamel\b/$Rakuda/;
145
146 The B<encoding> pragma also modifies the filehandle disciplines of
147 STDIN, STDOUT, and STDERR to the specified encoding.  Therefore,
148
149   use encoding "euc-jp";
150   my $message = "Camel is the symbol of perl.\n";
151   my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji
152   $message =~ s/\bCamel\b/$Rakuda/;
153   print $message;
154
155 Will print "\xF1\xD1\xF1\xCC is the symbol of perl.\n",
156 not "\x{99F1}\x{99DD} is the symbol of perl.\n".
157
158 You can override this by giving extra arguments; see below.
159
160 =head1 USAGE
161
162 =over 4
163
164 =item use encoding [I<ENCNAME>] ;
165
166 Sets the script encoding to I<ENCNAME>. Filehandle disciplines of
167 STDIN and STDOUT are set to ":encoding(I<ENCNAME>)".  Note that STDERR
168 will not be changed.
169
170 If no encoding is specified, the environment variable L<PERL_ENCODING>
171 is consulted.  If no encoding can be found, the error C<Unknown encoding
172 'I<ENCNAME>'> will be thrown.
173
174 Note that non-STD file handles remain unaffected.  Use C<use open> or
175 C<binmode> to change disciplines of those.
176
177 =item use encoding I<ENCNAME> [ STDIN =E<gt> I<ENCNAME_IN> ...] ;
178
179 You can also individually set encodings of STDIN and STDOUT via the
180 C<< STDIN => I<ENCNAME> >> form.  In this case, you cannot omit the
181 first I<ENCNAME>.  C<< STDIN => undef >> turns the IO transcoding
182 completely off.
183
184 =item no encoding;
185
186 Unsets the script encoding. The disciplines of STDIN, STDOUT are
187 reset to ":raw" (the default unprocessed raw stream of bytes).
188
189 =back
190
191 =head1 CAVEATS
192
193 =head2 NOT SCOPED
194
195 The pragma is a per script, not a per block lexical.  Only the last
196 C<use encoding> or C<no encoding> matters, and it affects B<the whole script>.
197 However, the <no encoding> pragma is supported and C<use encoding> can
198 appear as many times as you want in a given script.  The multiple use
199 of this pragma is discouraged.
200
201 =head2 DO NOT MIX MULTIPLE ENCODINGS
202
203 Notice that only literals (string or regular expression) having only
204 legacy code points are affected: if you mix data like this
205
206         \xDF\x{100}
207
208 the data is assumed to be in (Latin 1 and) Unicode, not in your native
209 encoding.  In other words, this will match in "greek":
210
211         "\xDF" =~ /\x{3af}/
212
213 but this will not
214
215         "\xDF\x{100}" =~ /\x{3af}\x{100}/
216
217 since the C<\xDF> (ISO 8859-7 GREEK SMALL LETTER IOTA WITH TONOS) on
218 the left will B<not> be upgraded to C<\x{3af}> (Unicode GREEK SMALL
219 LETTER IOTA WITH TONOS) because of the C<\x{100}> on the left.  You
220 should not be mixing your legacy data and Unicode in the same string.
221
222 This pragma also affects encoding of the 0x80..0xFF code point range:
223 normally characters in that range are left as eight-bit bytes (unless
224 they are combined with characters with code points 0x100 or larger,
225 in which case all characters need to become UTF-8 encoded), but if
226 the C<encoding> pragma is present, even the 0x80..0xFF range always
227 gets UTF-8 encoded.
228
229 After all, the best thing about this pragma is that you don't have to
230 resort to \x{....} just to spell your name in a native encoding.
231 So feel free to put your strings in your encoding in quotes and
232 regexes.
233
234 =head1 Non-ASCII Identifiers and Filter option
235
236 The magic of C<use encoding> is not applied to the names of
237 identifiers.  In order to make C<${"\x{4eba}"}++> ($human++, where human
238 is a single Han ideograph) work, you still need to write your script
239 in UTF-8 or use a source filter.
240
241 In other words, the same restriction as with Jperl applies.
242
243 If you dare to experiment, however, you can try the Filter option.
244
245 =over 4
246
247 =item use encoding I<ENCNAME> Filter=E<gt>1;
248
249 This turns the encoding pragma into a source filter.  While the default
250 approach just decodes interpolated literals (in qq() and qr()), this
251 will apply a source filter to the entire source code.  In this case,
252 STDIN and STDOUT remain untouched.
253
254 =back
255
256 What does this mean?  Your source code behaves as if it is written in
257 UTF-8.  So even if your editor only supports Shift_JIS, for example,
258 you can still try examples in Chapter 15 of C<Programming Perl, 3rd
259 Ed.>.  For instance, you can use UTF-8 identifiers.
260
261 This option is significantly slower and (as of this writing) non-ASCII
262 identifiers are not very stable WITHOUT this option and with the
263 source code written in UTF-8.
264
265 To make your script in legacy encoding work with minimum effort,
266 do not use Filter=E<gt>1.
267
268 =head1 EXAMPLE - Greekperl
269
270     use encoding "iso 8859-7";
271
272     # \xDF in ISO 8859-7 (Greek) is \x{3af} in Unicode.
273
274     $a = "\xDF";
275     $b = "\x{100}";
276
277     printf "%#x\n", ord($a); # will print 0x3af, not 0xdf
278
279     $c = $a . $b;
280
281     # $c will be "\x{3af}\x{100}", not "\x{df}\x{100}".
282
283     # chr() is affected, and ...
284
285     print "mega\n"  if ord(chr(0xdf)) == 0x3af;
286
287     # ... ord() is affected by the encoding pragma ...
288
289     print "tera\n" if ord(pack("C", 0xdf)) == 0x3af;
290
291     # ... as are eq and cmp ...
292
293     print "peta\n" if "\x{3af}" eq  pack("C", 0xdf);
294     print "exa\n"  if "\x{3af}" cmp pack("C", 0xdf) == 0;
295
296     # ... but pack/unpack C are not affected, in case you still
297     # want to go back to your native encoding
298
299     print "zetta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf;
300
301 =head1 KNOWN PROBLEMS
302
303 For native multibyte encodings (either fixed or variable length),
304 the current implementation of the regular expressions may introduce
305 recoding errors for regular expression literals longer than 127 bytes.
306
307 The encoding pragma is not supported on EBCDIC platforms.
308 (Porters who are willing and able to remove this limitation are
309 welcome.)
310
311 =head1 SEE ALSO
312
313 L<perlunicode>, L<Encode>, L<open>, L<Filter::Util::Call>,
314
315 Ch. 15 of C<Programming Perl (3rd Edition)>
316 by Larry Wall, Tom Christiansen, Jon Orwant;
317 O'Reilly & Associates; ISBN 0-596-00027-8
318
319 =cut