Script extracts and prints UTF-8 words well, but prints JSON as trash

I tried using the script on Mac OS Mavericks (perl 5.16.2) and Yosemite, as well as on Windows 7 (strawberry-pearl transfer-5.20.1.1-64bit).

It is supposed to read UTF-8 data (Russian text) and put it in the data structure - and finally print the data structure as a JSON string (the output will be used to supply Core Data to the iOS game).

The first part works (extracting words and printing them for verification) works well, but the final part does not: the resulting JSON string contains garbage:

screenshot

Does anyone know how to fix my simple test script?

#!/usr/bin/perl -w

use strict;
use warnings;
use utf8;
use JSON;

binmode(STDOUT, ':utf8');

my $root = { words => [] };

while (<DATA>) {
        chomp;
        utf8::decode($_);
        my @a = split /\s*[:,]\s*/;

        my $words = [];
        for my $word (@a[1 .. $#a]) {
                print "WORD: $word\n";
                #push @$words, utf8::encode($word);
                push @$words, $word;
        }

        push @{$root->{words}}, $words;
}

print to_json($root, {utf8 => 1, pretty => 1});

__DATA__
: , , , , , , , 
: , , , , , 
+4
source share
2 answers

. from_json (utf8 => 1), STDOUT (binmode(STDOUT, ':utf8');).

, , . -JSON JSON STDOUT, from_json .

+3

"", : . ,

binmode STDOUT, ':raw';

JSON.

script encode_json:

#!/usr/bin/perl

use strict;
use warnings;
use utf8;
use JSON;

binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");

my $root;

while (<DATA>) {
        chomp;
        my @words = split /\s*[:,]\s*/;
        push @{ $root->{words} }, [];

        for my $word (@words[1 .. $#words]) {
                print "WORD: $word\n";
                push @{ $root->{words}[-1] }, $word;
        }
}

my $json = encode_json($root);
binmode STDOUT, ':raw';
print $json;
+3

All Articles