From 2f41bca4e8ef591f5fa5c9ee620c7aacc9409f71 Mon Sep 17 00:00:00 2001 From: Guilhem Moulin Date: Fri, 2 Mar 2012 11:53:49 +0100 Subject: cleaner way to set restore the UTF-8 flag for the output of SELECT queries --- videoadd.pl | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'videoadd.pl') diff --git a/videoadd.pl b/videoadd.pl index 1c8b0db..0d39eae 100755 --- a/videoadd.pl +++ b/videoadd.pl @@ -267,6 +267,7 @@ unless ( defined $ignoredb_flag ) { } $dbh = DBI->connect($dsn, $config{user}, $config{password}) or die "Error: Can't connect do database.\n"; + $dbh->do( "SET NAMES UTF8" ) or die "Error: Can't set names to UTF-8.\n"; } my $id; @@ -658,6 +659,15 @@ sub getfile { my $res = $dbh->selectall_hashref( $SELECT, 'id' ) or die "Can't select: $!\n"; + + # The database is using UTF-8, but the UTF-8 flag may be missing; + # restore it. + foreach my $id (keys $res) { + foreach my $k (keys $res->{$id}) { + Encode::_utf8_on($res->{$id}->{$k}); + } + } + # We have a new file to add to the database if (scalar (keys $res) > 0) { my $answer; @@ -672,7 +682,7 @@ sub getfile { my @options; foreach my $id (keys $res) { printf "%04d - ", $res->{$id}->{id} or die "Can't printf: $!"; - print &iconv($res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n", 'utf8') + print $res->{$id}->{title}. " (" .$res->{$id}->{year}. ")\n" or die "Can't print: $!"; push @options, sprintf ("%04d", $res->{$id}->{id}); } @@ -798,22 +808,13 @@ sub YNquestion { } # Convert to UTF-8 -# (Strings from the database are in UTF-8, but are lacking the UTF-8 flag) +# The input string should be in latin1 unless the UTF-8 flag is on. sub iconv { - my ($string, $enc) = @_; + my $string = $_[0]; return unless defined $string; unless (Encode::is_utf8($string)) { - my @encodings = qw/latin1 utf8/; - @encodings = $enc if defined $enc; - - my $decoded; - foreach my $enc (@encodings) { - $decoded = Encode::decode( $enc, $string); - next if $@ or not $decoded; - last if utf8::valid($decoded); - } - $string = $decoded; + $string = Encode::decode( 'latin1', $string, 1 ); } # The UTF-8 flag should be on now -- cgit v1.2.3