我有以下程序:
#! /usr/bin/perl
use strict;
use warnings;
use utf8;
print "x{00a0}n";
当我运行它时,它会产生错误的UTF-8编码:
$ ./nbsp.pl | od -tx1
0000000 a0 0a
0000002
我的期望是:
$ printf 'u00a0n' | od -tx1
0000000 c2 a0 0a
0000003
为什么00a0
被编码为a0
而不是c2a0
?
当我尝试解析JSON数据时,也会发生同样的情况:
#! /usr/bin/perl
use strict;
use warnings;
use JSON::Parse qw(parse_json);
my $json = parse_json ('{"nbsp":"u00A0"}');
print $json->{nbsp}, "n";
它不需要utf8
杂注,而是一个语句来编码输出。最佳使用开放式杂注
use strict;
use warnings;
use open ":std", ":encoding(UTF-8)";
print "x{00a0}n";