Term::ReadLine::Gnu Unicode Hell
I must be doing something wrong. Surely. I have a REST service. I get data using HTTP::Tiny and use JSON::Tiny to decode it.
If I try to print to the Term::ReadLine::Gnu OUT filehandle, I get double encoded strings, like this:
coração
If I try to binmode it to utf-8, things get worst, with triple encoded strings:
coração
Resolved it decoding (Encode::decode) from UTF-8 and using the internal Perl character representation. It worked.
The problem was when I tried to feed a pre-defined input line to Term::ReadLine::Gnu. Result looked something like this:
my $newGloss = $self->{term}->readline(">> ",
decode("utf-8", decode("utf-8", $gloss->{gloss})));
my $x = encode("utf-8", $newGloss);
if (length($x) && $x ne $gloss->{gloss}) {
chomp($x);
my $ans = $self->post("gloss/$offset",
gloss => decode("utf-8",$newGloss));
if ($ans) {
// ...
}
}
else {
$self->INFO("Nothing changed!");
}
If someone knows any good solution, I am grateful!
is it possible that you are not decoding your json as it comes in? In JSON::Tiny you probably should be using decode_json rather than from_json (which you might be, but without seeing the code I don't know).
Hmms, is there a difference? It seems I have to RTFM. Will say something soon. Thanks.
I confess I did not notice the difference when looking to the manpage. I though they were just aliases (similar names). But of course, it was my mistake.
Nevertheless, still not that good:
my $newGloss = $self->{term}->readline(">> ", decode("utf-8", $gloss->{gloss}));
if (length($newGloss) && $newGloss ne $gloss->{gloss}) {
chomp($newGloss);
my $x = decode("utf-8", $newGloss);
my $ans = $self->post("gloss/$offset", gloss => $x);
But got simpler, yes.
Next, try to figure out if I am using post_form from http::tiny in the wrong way (just passing the string in the hash). But still need to convert it from utf8 before feeding it to Term::ReadLine::Gnu.
And yes, the comparison works well with $newGloss. But the post needs to be decoded first...