encoding - pragma to control the conversion of legacy data into Unicode
use encoding "iso 8859-7"; # The \xDF of ISO 8859-7 (Greek) is \x{3af} in Unicode. $a = "\xDF"; $b = "\x{100}"; printf "%#x\n", ord($a); # will print 0x3af, not 0xdf $c = $a . $b; # $c will be "\x{3af}\x{100}", not "\x{df}\x{100}". # chr() is affected, and ... print "mega\n" if ord(chr(0xdf)) == 0x3af; # ... ord() is affected by the encoding pragma ... print "tera\n" if ord(pack("C", 0xdf)) == 0x3af; # but pack/unpack are not affected, in case you still # want back to your native encoding print "peta\n" if unpack("C", (pack("C", 0xdf))) == 0xdf;
Normally when legacy 8-bit data is converted to Unicode the data is expected to be Latin-1 (or EBCDIC in EBCDIC platforms). With the encoding pragma you can change this default.
The pragma is a per script, not a per block lexical. Only the last use encoding matters, and it affects the whole script.
use encoding
Notice that only literals (string or regular expression) having only legacy code points are affected: if you mix data like this
\xDF\x{100}
the data is assumed to be in (Latin 1 and) Unicode, not in your native encoding. In other words, this will match in "greek":
"\xDF" =~ /\x{3af}/
but this will not
"\xDF\x{100}" =~ /\x{3af}\x{100}/
since the \xDF on the left will not be upgraded to \x{3af} because of the \x{100} on the left. You should not be mixing your legacy data and Unicode in the same string.
\xDF
\x{3af}
\x{100}
This pragma also affects encoding of the 0x80..0xFF code point range: normally characters in that range are left as eight-bit bytes (unless they are combined with characters with code points 0x100 or larger, in which case all characters need to become UTF-8 encoded), but if the encoding pragma is present, even the 0x80..0xFF range always gets UTF-8 encoded.
encoding
If no encoding is specified, the environment variable PERL_ENCODING is consulted. If that fails, "latin1" (ISO 8859-1) is assumed. If no encoding can be found, Unknown encoding '...' error will be thrown.
Unknown encoding '...'
For native multibyte encodings (either fixed or variable length) the current implementation of the regular expressions may introduce recoding errors for longer regular expression literals than 127 bytes.
The encoding pragma is not supported on EBCDIC platforms.
perlunicode, Encode
To install if, copy and paste the appropriate command in to your terminal.
cpanm
cpanm if
CPAN shell
perl -MCPAN -e shell install if
For more information on module installation, please visit the detailed CPAN module installation guide.