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:

Does anyone know how to fix my simple test script?
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, $word;
}
push @{$root->{words}}, $words;
}
print to_json($root, {utf8 => 1, pretty => 1});
__DATA__
: , , , , , , ,
: , , , , ,
source
share