T. Carson T. Carson - 7 months ago 38
Perl Question

Decode unicode escape characters with perl

I hate to ask a question that's undoubtedly been answered a dozen times before, but I find encoding issues confusing and am having a hard time matching up other people's q/a with my own problem.

I'm pulling information from a json file online, and my perl script isn't handling unicode escape characters properly.

Script looks like this:

use LWP::Simple;
use JSON;
my $url = ______;
my $json = get($url);
my $data = decode_json($json);
foreach my $i (0 .. $#{data->{People}}) {
print "$data->{People}[$i]{first_name} $data->{People}[$i]{last_name}\n";

It encounters jsons that look like this:
and prints them like this:
FΘlix Cat

I'm sure there's a trivial fix here, but I'm stumped. I'd really appreciate any help you can provide.


You didn't tell Perl how to encode the output. You need to add

use open ':std', ':encoding(XXX)';

where XXX is the encoding the terminal expects.

On unix boxes, you normally need

use open ':std', ':encoding(UTF-8)';

On Windows boxes, you normally need

use Win32 qw( );
use open ':std', ':encoding(cp'.Win32::GetConsoleOutputCP().')';