1 # $Id: encoding.pm,v 1.47 2003/08/20 11:15:31 dankogai Exp dankogai $
3 our $VERSION = do { my @r = (q$Revision: 1.47 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
10 if (ord("A") == 193) {
12 Carp::croak("encoding pragma does not support EBCDIC platforms");
17 eval { require PerlIO::encoding };
19 $HAS_PERLIO = (PerlIO::encoding->VERSION >= 0.02);
24 $] > 5.008 and return 0; # 5.8.1 or higher then no
25 my %utfs = map {$_=>1}
26 qw(utf8 UCS-2BE UCS-2LE UTF-16 UTF-16BE UTF-16LE
27 UTF-32 UTF-32BE UTF-32LE);
28 $utfs{$name} or return 0; # UTFs or no
29 require Config; Config->import(); our %Config;
30 return $Config{perl_patchlevel} ? 0 : 1 # maintperl then no
37 $name ||= $ENV{PERL_ENCODING};
38 my $enc = find_encoding($name);
39 unless (defined $enc) {
41 Carp::croak("Unknown encoding '$name'");
43 $name = $enc->name; # canonize
44 unless ($arg{Filter}) {
45 DEBUG and warn "_exception($name) = ", _exception($name);
46 _exception($name) or ${^ENCODING} = $enc;
47 $HAS_PERLIO or return 1;
49 defined(${^ENCODING}) and undef ${^ENCODING};
50 # implicitly 'use utf8'
51 require utf8; # to fetch $utf8::hint_bits;
52 $^H |= $utf8::hint_bits;
54 require Filter::Util::Call ;
55 Filter::Util::Call->import ;
57 my $status = filter_read();
59 $_ = $enc->decode($_, 1);
65 } DEBUG and warn "Filter installed";
66 defined ${^UNICODE} and ${^UNICODE} != 0 and return 1;
67 for my $h (qw(STDIN STDOUT)){
69 unless (defined find_encoding($arg{$h})) {
71 Carp::croak("Unknown encoding for $h, '$arg{$h}'");
73 eval { binmode($h, ":raw :encoding($arg{$h})") };
75 unless (exists $arg{$h}){
77 no warnings 'uninitialized';
78 binmode($h, ":raw :encoding($name)");
87 return 1; # I doubt if we need it, though
94 binmode(STDIN, ":raw");
95 binmode(STDOUT, ":raw");
100 if ($INC{"Filter/Util/Call.pm"}){
101 eval { filter_del() };
112 encoding - allows you to write your script in non-ascii or non-utf8
116 use encoding "greek"; # Perl like Greek to you?
117 use encoding "euc-jp"; # Jperl!
119 # or you can even do this if your shell supports your native encoding
121 perl -Mencoding=latin2 -e '...' # Feeling centrally European?
122 perl -Mencoding=euc-kr -e '...' # Or Korean?
126 # A simple euc-cn => utf-8 converter
127 use encoding "euc-cn", STDOUT => "utf8"; while(<>){print};
129 # "no encoding;" supported (but not scoped!)
132 # an alternate way, Filter
133 use encoding "euc-jp", Filter=>1;
134 # now you can use kanji identifiers -- in euc-jp!
138 Let's start with a bit of history: Perl 5.6.0 introduced Unicode
139 support. You could apply C<substr()> and regexes even to complex CJK
140 characters -- so long as the script was written in UTF-8. But back
141 then, text editors that supported UTF-8 were still rare and many users
142 instead chose to write scripts in legacy encodings, giving up a whole
143 new feature of Perl 5.6.
145 Rewind to the future: starting from perl 5.8.0 with the B<encoding>
146 pragma, you can write your script in any encoding you like (so long
147 as the C<Encode> module supports it) and still enjoy Unicode support.
148 This pragma achieves that by doing the following:
154 Internally converts all literals (C<q//,qq//,qr//,qw///, qx//>) from
155 the encoding specified to utf8. In Perl 5.8.1 and later, literals in
156 C<tr///> and C<DATA> pseudo-filehandle are also converted.
160 Changing PerlIO layers of C<STDIN> and C<STDOUT> to the encoding
165 =head2 Literal Conversions
167 You can write code in EUC-JP as follows:
169 my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji
170 #<-char-><-char-> # 4 octets
171 s/\bCamel\b/$Rakuda/;
173 And with C<use encoding "euc-jp"> in effect, it is the same thing as
176 my $Rakuda = "\x{99F1}\x{99DD}"; # two Unicode Characters
177 s/\bCamel\b/$Rakuda/;
179 =head2 PerlIO layers for C<STD(IN|OUT)>
181 The B<encoding> pragma also modifies the filehandle layers of
182 STDIN and STDOUT to the specified encoding. Therefore,
184 use encoding "euc-jp";
185 my $message = "Camel is the symbol of perl.\n";
186 my $Rakuda = "\xF1\xD1\xF1\xCC"; # Camel in Kanji
187 $message =~ s/\bCamel\b/$Rakuda/;
190 Will print "\xF1\xD1\xF1\xCC is the symbol of perl.\n",
191 not "\x{99F1}\x{99DD} is the symbol of perl.\n".
193 You can override this by giving extra arguments; see below.
195 =head1 FEATURES THAT REQUIRE 5.8.1
197 Some of the features offered by this pragma requires perl 5.8.1. Most
198 of these are done by Inaba Hiroto. Any other features and changes
203 =item "NON-EUC" doublebyte encodings
205 Because perl needs to parse script before applying this pragma, such
206 encodings as Shift_JIS and Big-5 that may contain '\' (BACKSLASH;
207 \x5c) in the second byte fails because the second byte may
208 accidentally escape the quoting character that follows. Perl 5.8.1
209 or later fixes this problem.
213 C<tr//> was overlooked by Perl 5 porters when they released perl 5.8.0
214 See the section below for details.
216 =item DATA pseudo-filehandle
218 Another feature that was overlooked was C<DATA>.
226 =item use encoding [I<ENCNAME>] ;
228 Sets the script encoding to I<ENCNAME>. And unless ${^UNICODE}
229 exists and non-zero, PerlIO layers of STDIN and STDOUT are set to
230 ":encoding(I<ENCNAME>)".
232 Note that STDERR WILL NOT be changed.
234 Also note that non-STD file handles remain unaffected. Use C<use
235 open> or C<binmode> to change layers of those.
237 If no encoding is specified, the environment variable L<PERL_ENCODING>
238 is consulted. If no encoding can be found, the error C<Unknown encoding
239 'I<ENCNAME>'> will be thrown.
241 =item use encoding I<ENCNAME> [ STDIN =E<gt> I<ENCNAME_IN> ...] ;
243 You can also individually set encodings of STDIN and STDOUT via the
244 C<< STDIN => I<ENCNAME> >> form. In this case, you cannot omit the
245 first I<ENCNAME>. C<< STDIN => undef >> turns the IO transcoding
248 When ${^UNICODE} exists and non-zero, these options will completely
249 ignored. ${^UNICODE} is a variable introduced in perl 5.8.1. See
250 L<perlrun> see L<perlvar/"${^UNICODE}"> and L<perlrun/"-C"> for
251 details (perl 5.8.1 and later).
253 =item use encoding I<ENCNAME> Filter=E<gt>1;
255 This turns the encoding pragma into a source filter. While the
256 default approach just decodes interpolated literals (in qq() and
257 qr()), this will apply a source filter to the entire source code. See
258 L</"The Filter Option"> below for details.
262 Unsets the script encoding. The layers of STDIN, STDOUT are
263 reset to ":raw" (the default unprocessed raw stream of bytes).
267 =head1 The Filter Option
269 The magic of C<use encoding> is not applied to the names of
270 identifiers. In order to make C<${"\x{4eba}"}++> ($human++, where human
271 is a single Han ideograph) work, you still need to write your script
272 in UTF-8 -- or use a source filter. That's what 'Filter=>1' does.
274 What does this mean? Your source code behaves as if it is written in
275 UTF-8 with 'use utf8' in effect. So even if your editor only supports
276 Shift_JIS, for example, you can still try examples in Chapter 15 of
277 C<Programming Perl, 3rd Ed.>. For instance, you can use UTF-8
280 This option is significantly slower and (as of this writing) non-ASCII
281 identifiers are not very stable WITHOUT this option and with the
282 source code written in UTF-8.
284 =head2 Filter-related changes at Encode version 1.87
290 The Filter option now sets STDIN and STDOUT like non-filter options.
291 And C<< STDIN=>I<ENCODING> >> and C<< STDOUT=>I<ENCODING> >> work like
296 C<use utf8> is implicitly declared so you no longer have to C<use
297 utf8> to C<${"\x{4eba}"}++>.
305 The pragma is a per script, not a per block lexical. Only the last
306 C<use encoding> or C<no encoding> matters, and it affects
307 B<the whole script>. However, the <no encoding> pragma is supported and
308 B<use encoding> can appear as many times as you want in a given script.
309 The multiple use of this pragma is discouraged.
311 By the same reason, the use this pragma inside modules is also
312 discouraged (though not as strongly discouranged as the case above.
315 If you still have to write a module with this pragma, be very careful
316 of the load order. See the codes below;
319 package Module_IN_BAR;
321 # stuff in "bar" encoding here
327 # surprise! use encoding "bar" is in effect.
329 The best way to avoid this oddity is to use this pragma RIGHT AFTER
330 other modules are loaded. i.e.
335 =head2 DO NOT MIX MULTIPLE ENCODINGS
337 Notice that only literals (string or regular expression) having only
338 legacy code points are affected: if you mix data like this
342 the data is assumed to be in (Latin 1 and) Unicode, not in your native
343 encoding. In other words, this will match in "greek":
349 "\xDF\x{100}" =~ /\x{3af}\x{100}/
351 since the C<\xDF> (ISO 8859-7 GREEK SMALL LETTER IOTA WITH TONOS) on
352 the left will B<not> be upgraded to C<\x{3af}> (Unicode GREEK SMALL
353 LETTER IOTA WITH TONOS) because of the C<\x{100}> on the left. You
354 should not be mixing your legacy data and Unicode in the same string.
356 This pragma also affects encoding of the 0x80..0xFF code point range:
357 normally characters in that range are left as eight-bit bytes (unless
358 they are combined with characters with code points 0x100 or larger,
359 in which case all characters need to become UTF-8 encoded), but if
360 the C<encoding> pragma is present, even the 0x80..0xFF range always
363 After all, the best thing about this pragma is that you don't have to
364 resort to \x{....} just to spell your name in a native encoding.
365 So feel free to put your strings in your encoding in quotes and
368 =head2 tr/// with ranges
370 The B<encoding> pragma works by decoding string literals in
371 C<q//,qq//,qr//,qw///, qx//> and so forth. In perl 5.8.0, this
372 does not apply to C<tr///>. Therefore,
374 use encoding 'euc-jp';
376 $kana =~ tr/\xA4\xA1-\xA4\xF3/\xA5\xA1-\xA5\xF3/;
377 # -------- -------- -------- --------
381 $kana =~ tr/\x{3041}-\x{3093}/\x{30a1}-\x{30f3}/;
385 =item Legend of characters above
387 utf8 euc-jp charnames::viacode()
388 -----------------------------------------
389 \x{3041} \xA4\xA1 HIRAGANA LETTER SMALL A
390 \x{3093} \xA4\xF3 HIRAGANA LETTER N
391 \x{30a1} \xA5\xA1 KATAKANA LETTER SMALL A
392 \x{30f3} \xA5\xF3 KATAKANA LETTER N
396 This counterintuitive behavior has been fixed in perl 5.8.1.
398 =head3 workaround to tr///;
400 In perl 5.8.0, you can work around as follows;
402 use encoding 'euc-jp';
404 eval qq{ \$kana =~ tr/\xA4\xA1-\xA4\xF3/\xA5\xA1-\xA5\xF3/ };
406 Note the C<tr//> expression is surrounded by C<qq{}>. The idea behind
407 is the same as classic idiom that makes C<tr///> 'interpolate'.
409 tr/$from/$to/; # wrong!
410 eval qq{ tr/$from/$to/ }; # workaround.
412 Nevertheless, in case of B<encoding> pragma even C<q//> is affected so
413 C<tr///> not being decoded was obviously against the will of Perl5
414 Porters so it has been fixed in Perl 5.8.1 or later.
416 =head1 EXAMPLE - Greekperl
418 use encoding "iso 8859-7";
420 # \xDF in ISO 8859-7 (Greek) is \x{3af} in Unicode.
425 printf "%#x\n", ord($a); # will print 0x3af, not 0xdf
429 # $c will be "\x{3af}\x{100}", not "\x{df}\x{100}".
431 # chr() is affected, and ...
433 print "mega\n" if ord(chr(0xdf)) == 0x3af;
435 # ... ord() is affected by the encoding pragma ...
437 print "tera\n" if ord(pack("C", 0xdf)) == 0x3af;
439 # ... as are eq and cmp ...
441 print "peta\n" if "\x{3af}" eq pack("C", 0xdf);
442 print "exa\n" if "\x{3af}" cmp pack("C", 0xdf) == 0;
444 # ... but pack/unpack C are not affected, in case you still
445 # want to go back to your native encoding
447 print "zetta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf;
449 =head1 KNOWN PROBLEMS
453 =item literals in regex that are longer than 127 bytes
455 For native multibyte encodings (either fixed or variable length),
456 the current implementation of the regular expressions may introduce
457 recoding errors for regular expression literals longer than 127 bytes.
461 The encoding pragma is not supported on EBCDIC platforms.
462 (Porters who are willing and able to remove this limitation are
467 This pragma doesn't work well with format because PerlIO does not
468 get along very well with it. When format contains non-ascii
469 characters it prints funny or gets "wide character warnings".
470 To understand it, try the code below.
472 # Save this one in utf8
473 # replace *non-ascii* with a non-ascii string
479 $camel = "*non-ascii*";
480 binmode(STDOUT=>':encoding(utf8)'); # bang!
482 print $camel, "\n"; # fine
484 Without binmode this happens to work but without binmode, print()
485 fails instead of write().
487 At any rate, the very use of format is questionable when it comes to
488 unicode characters since you have to consider such things as character
489 width (i.e. double-width for ideographs) and directions (i.e. BIDI for
496 This pragma first appeared in Perl 5.8.0. For features that require
497 5.8.1 and better, see above.
501 L<perlunicode>, L<Encode>, L<open>, L<Filter::Util::Call>,
503 Ch. 15 of C<Programming Perl (3rd Edition)>
504 by Larry Wall, Tom Christiansen, Jon Orwant;
505 O'Reilly & Associates; ISBN 0-596-00027-8