Claude
Claude

Reputation: 171

PERL: how to detect string encoding so I can use the right charset

I have these 2 example strings:

$a = "點看"; 
$b = "pøp";

First one is displayed correctly using charset UTF-8, but second string not. Second is displayed correctly if charset is changed to iso-8859-1.

I don't know how to display latin1 characters with charset utf-8. Or at least, I need a solution to detect string type (e.g this is "utf-8" or this is "iso-8859-1"), so I can use appropriate charset to display it.

Upvotes: 3

Views: 1651

Answers (1)

ikegami
ikegami

Reputation: 385546

Decode inputs. Encode outputs.

use strict;
use warnings qw( all );
use feature qw( say );

use utf8;                             # Source code is encoded using UTF-8
use open ':std', ':encoding(UTF-8)';  # Terminal expects UTF-8

my $s1 = "點看"; 
my $s2 = "pøp";

say for $s1, $s2;

Upvotes: 4

Related Questions