Perl, json, 한글 출력


#!/usr/bin/env perl

use utf8;
use strict;
use Encode;
use JSON;

binmode(STDOUT, ":utf8");

my ($line, $content, $perl_hashref, $key, $json);

$content = qq/{"별명":"컴맹"}/;
$perl_hashref = from_json($content);
for $key (sort keys %{$perl_hashref}){
  print $key . " : " . $perl_hashref->{$key} . "\n";
}

my $json = to_json($perl_hashref, {utf8 => 0, pretty => 1});
print $json;

아래 문서의 설명을 보면,
http://search.cpan.org/~ishigaki/JSON-2.97001/lib/JSON.pm

to_json : Converts the given Perl data structure to a Unicode string by default.
from_json : The opposite of to_json: expects a Unicode string and tries to parse it, returning the resulting reference.

encode_json과 decode_json은 UTF-8 encoded, binary string을 입력 받거나 출력한다고 되어있다.
encode_json과 decode_json을 써서는 json에서 한글 출력이 번거로웠다.
그래서, binmode와 to_json, from_json을 사용해야겠다.