Perl 6 错误消息:块 <unit> 中格式错误的 UTF-8

Perl 6 error message: Malformed UTF-8 in block <unit>

我正在尝试读取下载的 html-文件

my $file = "sn.html";
my $in_fh = open $file, :r;
my $text = $in_fh.slurp;

我收到以下错误消息:

Malformed UTF-8
  in block <unit> at prog.p6 line 10

如何避免这种情况并访问文件内容?

如果您在打开文件时没有指定编码,它将采用 utf8。显然,您要打开的文件包含无法解释为 UTF-8 的字节。因此出现错误消息。

根据您要对文件内容执行的操作,您可以设置 :bin 命名参数,以二进制模式打开文件。或者您可以使用特殊的 utf8-c8 编码,它将假定为 UTF-8,直到遇到无法编码的字节:在这种情况下,它将生成临时代码点。

有关详细信息,请参阅 https://docs.raku.org/language/unicode#UTF8-C8

对于slurp,如果你对编码有一些想法,也可以专门添加编码。

来自文档 (https://docs.perl6.org/routine/slurp):

my $text_contents   = slurp "path/to/file", enc => "latin1";

我今天用它来处理一个用 ISO-8859-1 编码的愚蠢文件。