Consider the following code. This way I get "Wide character in syswrite" for a file, and garbage in a browser:
use Mojolicious::Lite;
use Mojo::UserAgent;
use Mojo::File;
get '/' => sub {
my $c = shift;
my $ua = Mojo::UserAgent->new;
$res = $ua->get('https://...')->result;
Mojo::File->new('resp')->spurt($res->dom->at('.some-selector')->text);
$c->render(text => $res->body);
}
app->start;
But this way it works:
use Encode qw/encode_utf8 decode_utf8/;
Mojo::File->new('resp')->spurt(encode_utf8($res->dom->at('.some-selector')->text));
Mojo::File->new('resp')->spurt($res->body);
$c->render(text => decode_utf8($res->body));
Can you explain what's going on here? Why do the two of the statements not work without Encode
module? Why does the second one work? Is there a better way to handle it? I've skimmed over perluniintro and perlunicode, but that's as far as I could get.