1

我有一个perl script运行一堆命令重定向stdinstderr一个文本文件。脚本完成后,我使用 cgi 将文件的内容显示到调用它的网页上。

写入文件的至少一行包含宽字符,例如:

保存到:“rabbitmq-server-3.3.5-1.noarch.rpm”

我尝试在输出到网页时对文件进行 html 转义,但无济于事:

use CGI;
.
.
.
    my $filename = 'tempoutput.txt';
    open(my $fh, '<:encoding(UTF-8)', $filename)
        or die "Could not open file '$filename' $!";
    while (my $row = <$fh>) {
        chomp $row;
        print $cgi->p("");
        print $cgi->escapeHTML("$row");
    }

我也尝试过更改我在谷歌中找到的 CGI 参数,但它也没有用。

use CGI '-utf8';

关于我需要做些什么来完成这项工作的任何想法?

4

2 回答 2

2

打开对 UTF8 打印的支持binmode STDOUT, ":utf8";

于 2014-12-05T02:50:03.793 回答
2

binmode STDOUT, ":utf8";没有直接为我工作,但它为我指明了正确的方向。我在文件句柄上使用了 binmode 并解决了这个问题:

open(my $fh, '<:encoding(UTF-8)', $filename)
    or die "Could not open file '$filename' $!";
binmode($fh);
于 2014-12-05T17:51:21.737 回答