这是我的源代码:
use strict;
use warnings;
use diagnostics;
use utf8::all;
print join ' ',map{as_code_point($_)}sort qw/b 1 a A 日 本 国/;
sub as_code_point{
my $char=shift;
die"Only characters!"if length($char)>1;
return "U+".uc sprintf "%04x",ord $char;
}
我不知道出了什么问题。我需要你的帮助。
最佳答案
该问题表明标量包含无效值。当源未使用 UTF-8 编码但您告诉 Perl 它是(因为它不执行有效性检查)时,可能会发生这种情况。
如果文件编码正确,您就不会遇到这个问题。
$ perl a.pl | perl
U+0031 U+0041 U+0061 U+0062 U+56FD U+65E5 U+672C
a.pl
:
use strict;
use warnings;
use utf8::all;
my $chars = qq{qw/b 1 a A \x{65E5} \x{672C} \x{56FD}/};
while (<DATA>) {
s/<<<CHARS>>>/$chars/g;
print;
}
__DATA__
use strict;
use warnings;
use diagnostics;
use utf8::all;
print join ' ',map{as_code_point($_)}sort <<<CHARS>>>;
print "\n";
sub as_code_point{
my $char=shift;
die"Only characters!"if length($char)>1;
return "U+".uc sprintf "%04x",ord $char;
}
关于perl - C :/Strawberry/perl/lib/Carp. pm 第 324 行的格式错误的 UTF-8 字符(致命),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969554/