5
votes

When I extract data from a MySQL database, some of the output have special characters, when opened in e.g. emacs it decodes to \240 and \346.

When shown in an UTF-8 terminal, the special characters is shown as

So the used encoding seams to only use 1 byte per character.

I can e.g. see that \346 should be æ.

Question

Does Perl have a module that can encode these special characters to UTF-8?

1
The question is not about emacs. I would like Perl to encode the content to UTF-8.Sandra Schlichting
have you set up the terminal to show utf-8?Karoly Horvath
You likely just need to use encode/decode. See : perldoc.perl.org/perlunitut.htmlJoe
@Joe that solved the problem =) If you post that, when I will accept it as the answer.Sandra Schlichting

1 Answers

3
votes

Use Encode::decode to decode your data from whatever encoding it's in to Perl's internal format.

Then, when writing the data out to a file, set the 'utf8' layer to make the data be written in UTF-8.

use Encode;

my $data_from_database = ...;

my $perl_data = decode('ISO-8859-1', $data_from_database);

binmode STDOUT, ':utf8';

print $perl_data;