I have run into what I believe is a bug when passing unicode text to decode_json. I am using JSON::XS 2.33 which is the latest version. I also checked the below programs and the source files I was using before with File and isutf8 (from moreutils) both of which report everything is proper UTF-8 unicode text. Here is the broken code:
#!/usr/bin/perl
use v5.14;
use warnings;
use utf8::all;
use JSON::XS qw( decode_json );
use Data::Dumper;
my $wl = '{"creche": "crèche",
"¥": "£",
"₡": "волн"
}';
my $pattern_list = decode_json( $wl );
print Dumper $pattern_list;
Generates the following message: 'Wide character in subroutine entry at ./micro_test.pl line 22.' Now I tried switching the decode_json call to my $pattern_list = JSON::XS->new->utf8->decode( $wl ); but I got the same error message. Now if I save that data to a file and then load the file I get no error message.
#!/usr/bin/perl
use v5.14;
use warnings;
use utf8::all;
use JSON::XS qw( decode_json );
use File::Slurp qw( read_file );
use Data::Dumper;
my $wl = '{"creche": "crèche",
"¥": "£",
"₡": "волн"
}';
open my $fh, '>', 'test_file2';
say {$fh} $wl;
close $fh;
my $pattern_list = decode_json( read_file('test_file2') );
print Dumper $pattern_list;
Is there a step missing from the first program that I am unaware of? I expected the first program to just work since the POD for JSON::XS states that decode_json expects UTF-8. I also tried JSON::PP and it gave me different errors.
#!/usr/bin/perl
use v5.14;
use warnings;
use utf8::all;
use JSON::PP qw( decode_json );
use File::Slurp qw( read_file );
use Data::Dumper;
my $wl = '{"creche": "crèche",
"¥": "£",
"₡": "волн"
}';
open my $fh, '>', 't2';
say {$fh} $wl;
close $fh;
my $pattern_list = decode_json( read_file('t2') );
print Dumper $pattern_list;
, or } expected while parsing object/hash, at character offset 21 (before "\n") at ./micro_test.pl line 28