Extend the effect of the encoding pragma to chr() and ord().
[p5sagit/p5-mst-13.2.git] / lib / encoding.pm
CommitLineData
0a378802 1package encoding;
2
3use Encode;
4
5sub import {
6 my ($class, $name) = @_;
7 $name = $ENV{PERL_ENCODING} if @_ < 2;
121910a4 8 $name = "latin1" unless defined $name;
0a378802 9 my $enc = find_encoding($name);
10 unless (defined $enc) {
11 require Carp;
12 Carp::croak "Unknown encoding '$name'";
13 }
14 ${^ENCODING} = $enc;
15}
16
17=pod
18
19=head1 NAME
20
21encoding - pragma to control the conversion of legacy data into Unicode
22
23=head1 SYNOPSIS
24
25 use encoding "iso 8859-7";
26
121910a4 27 # The \xDF of ISO 8859-7 (Greek) is \x{3af} in Unicode.
4bdee82d 28
0a378802 29 $a = "\xDF";
30 $b = "\x{100}";
31
4bdee82d 32 printf "%#x\n", ord($a); # will print 0x3af, not 0xdf
33
0a378802 34 $c = $a . $b;
35
36 # $c will be "\x{3af}\x{100}", not "\x{df}\x{100}".
0a378802 37
121910a4 38 # chr() is affected, and ...
39
40 print "mega\n" if ord(chr(0xdf)) == 0x3af;
41
42 # ... ord() is affected by the encoding pragma ...
43
44 print "tera\n" if ord(pack("C", 0xdf)) == 0x3af;
45
46 # but pack/unpack C are not, in case you still
47 # want back to your native encoding
48
49 print "peta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf;
50
0a378802 51=head1 DESCRIPTION
52
53Normally when legacy 8-bit data is converted to Unicode the data is
54expected to be Latin-1 (or EBCDIC in EBCDIC platforms). With the
55encoding pragma you can change this default.
56
57The pragma is a per script, not a per block lexical. Only the last
9f4817db 58C<use encoding> matters, and it affects B<the whole script>.
0a378802 59
4bdee82d 60If no encoding is specified, the environment variable L<PERL_ENCODING>
121910a4 61is consulted. If that fails, "latin1" (ISO 8859-1) is assumed.
62If no encoding can be found, C<Unknown encoding '...'> error will be thrown.
4bdee82d 63
0a378802 64=head1 FUTURE POSSIBILITIES
65
121910a4 66The C<\x..> and C<\0...> in regular expressions are not affected by
67this pragma. They probably should.
9f4817db 68
121910a4 69The charnames "\N{...}" does not work with this pragma.
0a378802 70
d521382b 71=head1 KNOWN PROBLEMS
72
73Cannot be combined with C<use utf8>. Note that this is a problem
74B<only> if you would like to have Unicode identifiers in your scripts.
75You should not need C<use utf8> for anything else these days
121910a4 76(since Perl 5.8.0).
d521382b 77
0a378802 78=head1 SEE ALSO
79
121910a4 80L<perlunicode>, L<Encode>
0a378802 81
82=cut
83
841;