$dbh->do( "set names utf8" ); #### #!/usr/bin/perl use strict; use warnings; use Encode; use DBI; use utf8; # added by update, lest there be any doubt about this my $DSN = "DBI:mysql:database=test;host=localhost"; my $db = DBI->connect( $DSN, "", "", { AutoCommit => 1, RaiseError => 0 } ) or die $DBI::errstr; my @setup = ( 'SET NAMES utf8', 'DROP TABLE IF EXISTS widechar_test', 'CREATE TABLE widechar_test (string varchar(50)) DEFAULT CHARACTER SET utf8', ); for my $s ( @setup ) { $db->do( $s ) or die $db->errstr; } my $sth = $db->prepare( "INSERT INTO widechar_test (string) VALUES (?)" ) or die $db->errstr; my $latin = "\x{e7}\x{e1}"; my $greek = "\x{3a0}\x{397}\x{3a1}\x{39b}"; printf( " char lengths: %d latin, %d greek\n", length( $latin ), length( $greek )); $sth->execute( prep( $latin )) or warn "latin char insert failed: ".$sth->errstr; $sth->execute( prep( $greek )) or warn "greek char insert failed: ".$sth->errstr; { use bytes; printf( " byte lengths: %d latin, %d greek\n", length( $latin ), length( $greek )); $sth->execute( prep( $latin )) or warn "latin byte insert failed: ".$sth->errstr; $sth->execute( prep( $greek )) or warn "greek byte insert failed: ".$sth->errstr; } $sth->finish; binmode STDOUT, ":utf8"; my $rows = $db->selectall_arrayref( "select * from widechar_test" ); for my $row ( @$rows ) { print deprep( $$row[0] ); } sub prep { return encode( "utf8", $_[0] ); # return $_[0]; } sub deprep { return decode( "utf8", $_[0] ) . "\n"; # return "$_\n"; }