in reply to Re: Convert database into UTF-8
in thread Convert database into UTF-8

When DBI pulls text data out of your database, the data will be treated as "bytes", not as characters -- because Perl has no way of knowing what sort of character encoding has been stored in the database.

So the data coming out of the database is a set of "octets", and needs to be "decoded" into a utf8 string within your perl script. If DBI has given you a hash keyed by column name, then:

# I'm sure you have a very different (more sensible) way of # mapping table values to their proper legacy encodings, but # this is just to show how to handle the data: my %column_enc_map = ( columnA => 'cp1250', columnB => 'cp1251', # or whatever... ); for my $field ( keys %column_enc_map ) { # replace the hash values from the database with utf8 strings: $database{$field} = decode( $column_enc_map{$field}, $database{$fi +eld} ); } # %database values are now in utf8; you can load them back to the data +base via updates
Looking at your later reply in this thread, I'm pretty sure you don't need the extra "encode()" step on top of the "decode". All that does is turn off the utf8 flag on the string, which is kind of pointless, I think.

Replies are listed 'Best First'.
Re^3: Convert database into UTF-8
by salonmonk (Beadle) on Apr 20, 2005 at 15:07 UTC
    Wow, brilliant - many thanks for that explanation graff!