6 my ($class, $name) = @_;
7 $name = $ENV{PERL_ENCODING} if @_ < 2;
8 $name = "latin1" unless defined $name;
9 my $enc = find_encoding($name);
10 unless (defined $enc) {
12 Carp::croak "Unknown encoding '$name'";
21 encoding - pragma to control the conversion of legacy data into Unicode
25 use encoding "iso 8859-7";
27 # The \xDF of ISO 8859-7 (Greek) is \x{3af} in Unicode.
32 printf "%#x\n", ord($a); # will print 0x3af, not 0xdf
36 # $c will be "\x{3af}\x{100}", not "\x{df}\x{100}".
38 # chr() is affected, and ...
40 print "mega\n" if ord(chr(0xdf)) == 0x3af;
42 # ... ord() is affected by the encoding pragma ...
44 print "tera\n" if ord(pack("C", 0xdf)) == 0x3af;
46 # but pack/unpack C are not, in case you still
47 # want back to your native encoding
49 print "peta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf;
53 Normally when legacy 8-bit data is converted to Unicode the data is
54 expected to be Latin-1 (or EBCDIC in EBCDIC platforms). With the
55 encoding pragma you can change this default.
57 The pragma is a per script, not a per block lexical. Only the last
58 C<use encoding> matters, and it affects B<the whole script>.
60 If no encoding is specified, the environment variable L<PERL_ENCODING>
61 is consulted. If that fails, "latin1" (ISO 8859-1) is assumed.
62 If no encoding can be found, C<Unknown encoding '...'> error will be thrown.
66 The C<\x..> and C<\0...> in regular expressions are not affected by
67 this pragma. They very probably should.
69 The charnames pragma ("\N{LATIN SMALL SHARP LETTER S}") does not work
74 L<perlunicode>, L<Encode>