Surviving the Perl/UTF-8 Madness
Internal Scalars
ASCII
use utf8; #this script is written in UTF-8 my $string = "Äußerst süße Töchter aus Ölüberschußländern in Übergröße";
Standard File Handles
# treat all input and output as UTF-8 and set the flags correctly binmode STDOUT, ":utf8"; binmode STDERR, ":utf8"; binmode STDIN, ":utf8";
Reading from Files
# read from a latin1 encoded file open FH, "<:encoding(iso-8859-1)", 'test.latin1.txt'; $latin1 = <FH>; close FH; # read from a UTF-8 encoded file open FH, "<:encoding(utf8)", 'test.utf8.txt'; $utf8 = <FH>; close FH;
MySQL Databases
$DBH = DBI->connect("DBI:mysql:database=foo;host=localhost", 'user', 'password', { mysql_enable_utf8 => 1 });
Manually setting the UTF-8 flag
“”
use Encode; # $line containes UTF-8 encoded text but the flag isn't set, yet $line = Encode::decode_utf8($line); # set the flag
