2

Here is a sample code snippet:

my $str = '21156_MLA Ã Copy4.ens';

I'm using this $str in my code, and it should display as 21156_MLA ß Copy4.ens.

I'm uploading a file with the filename 21156_MLA ß Copy4.ens, but on the browser display it's displaying as 21156_MLA Ã Copy4.ens. In the database it's stored properly as ß, but when we are retrieving that from the DB (using fetchall_hashref) it's getting converted to Ã. Subsequently the display on the browser is 21156_MLA Ã Copy4.ens. How to avoid this conversion here?

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
user1686082
  • 79
  • 1
  • 2
  • 8
  • 1
    1. Replace the code in your question with the output of `use Data::Dumper qw( Dumper ); { local $Data::Dumper::Useqq = 1; local $Data::Dumper::Indent = 0; local $Data::Dumper::Terse = 1; print('my $str = '.Dumper($str)."\n"); }`. – ikegami Jan 22 '13 at 08:07
  • 1
    2. Supply the values you pass to `DBI->connect`. (Feel free to replace the db,host,port,user,passwd with different words.) – ikegami Jan 22 '13 at 08:07
  • 1
    3. Add the charset do you specify (in the Content-Type) for the HTML you output (if any), and tell us how you encode the data you send to the browser 9if at all). – ikegami Jan 22 '13 at 08:08
  • Also, note that there is certain "voodoo" required to get perl to handle utf8 well, see http://stackoverflow.com/a/6163129/23118. http://stackoverflow.com/q/10824366/23118 is also useful. – hlovdal Jan 22 '13 at 08:37

3 Answers3

1

Check below modules. they have hte inforation that you need:

https://metacpan.org/pod/Encode

https://metacpan.org/pod/MIME::QuotedPrint

As You haven't posted what you have tried so far, i am not going a write code from the scratch. However, Check above perl module documentation, they have information.

szabgab
  • 6,202
  • 11
  • 50
  • 64
slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123
1

If your source code file is encoded as UTF-8 then you need to specify:

use utf8;

in your source code file to tell the interpreter that you may have strings embedded in your source that are UTF-8.

See http://perldoc.perl.org/utf8.html

PP.
  • 10,764
  • 7
  • 45
  • 59
0

First, as PP notes, if your source file is encoded in UTF-8, you should use utf8; so that Perl will know that and interpret any string literals in it correctly.

Second, make sure the text in the database is also correctly encoded. The details of this will depend on you database, but e.g. for MySQL, the best way is probably to make sure your text columns have the utf8 character set and the utf8_unicode_ci collation (or the appropriate national collation scheme, if needed), and to include the mysql_enable_utf8 option when connecting to the database using DBI.

Third, you need to tell Perl that you want your I/O streams to be UTF-8 encoded, too. You can do this using binmode(), as in:

binmode STDOUT, ':utf8';

Finally, you also need to tell the browser that you're sending it UTF-8 text. (I suspect this part is your actual problem, but if you do all the other steps too, you'll have achieved a fully Unicode-aware workflow.) You can do this by sending the HTTP header:

Content-Type: text/html; charset=UTF-8

and/or the equivalent HTML meta tag:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

or, in HTML5, simply:

<meta charset="utf-8">
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153