I have the following program:
#! /usr/bin/perl
use strict;
use warnings;
use utf8;
print "\x{00a0}\n";
When I run it, it produces the wrong UTF-8 encoding:
$ ./nbsp.pl | od -tx1
0000000 a0 0a
0000002
My expectation would be this:
$ printf '\u00a0\n' | od -tx1
0000000 c2 a0 0a
0000003
Why is 00a0 encoded as a0 instead of c2a0 as it should be?
The same happens, when I try to parse JSON data:
#! /usr/bin/perl
use strict;
use warnings;
use JSON::Parse qw(parse_json);
my $json = parse_json ('{"nbsp":"\u00A0"}');
print $json->{nbsp}, "\n";
CodePudding user response:
It doesn't need the utf8 pragma but a statement to encode output. Best use open pragma
use strict;
use warnings;
use open ":std", ":encoding(UTF-8)";
print "\x{00a0}\n";
