in reply to Convert database into UTF-8

Yes sorry, I'm just scanning documents hopelessly looking for help. I'm not sure about Encode - when I pull my data from the database, has Perl already converted it into UTF-8 for internal use? Would this mean I need to do no work ? I would I just &decode('windows-1250', $database{column}) - meaning the values returned from the decode function would be in UTF-8 ?

Replies are listed 'Best First'.
Re^2: Convert database into UTF-8
by graff (Chancellor) on Apr 20, 2005 at 13:29 UTC
    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.
      Wow, brilliant - many thanks for that explanation graff!
Re^2: Convert database into UTF-8
by Anonymous Monk on Apr 20, 2005 at 10:59 UTC
    perluniintro - Perl Unicode introduction