perl - C :/Strawberry/perl/lib/Carp. pm 第 324 行的格式错误的 UTF-8 字符(致命)

标签 perl utf-8

这是我的源代码:

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/

相关文章:

perl - post-for/if 语法是什么?

perl - 我怎样才能使用map来清理这个Perl代码?

使用 UTF-8 数据识别字符串的 JAVA 代码

python - python中\n和\r\n的区别

php - UTF-8贯穿始终

php - 定位mysql数据库中不可见的utf8字符

Perl 单行单引号

perl - 在 perl 中评估 Expect

perl - 为什么我的 Perl 源代码显示在浏览器中?

mysql - 在 MySQL 中将复合字节转换为 Unicode