0

我正在尝试读取 UTF-8 编码的 xml 文件。该文件大小约为 8M,仅包含一行。

我使用下面的行来打开这个单行 xml 文件:

open(INP,"<:utf8","$infile") or die "Couldn't open file passed as input, $!";
local $/ = undef;
my $inputfile = <INP>;
print $inputfile; ## Not working..

但是在这条线程序卡住并继续等待之后。

我尝试了其他方法,例如 binmode 和 decode ,但遇到了同样的问题。

当我将上述文件打开代码更改为:

open(INP,"$infile") or die "Couldn't open file passed as input, $!";
local $/ = undef;
my $inputfile = <INP>;
print $inputfile; ## It works..


open(INP,"$infile") or die "Couldn't open file passed as input, $!";
binmode(INP, ":utf8");
local $/ = undef;
my $inputfile = <INP>;
print $inputfile; ## Not working..

你能帮我在这里做错什么吗?我需要对输入数据执行一些操作,并且必须得到 utf8 编码的输出。

4

1 回答 1

0

我在这里尝试了你的最后一个片段(Ubuntu 12.04,perl 5.14.2),它按预期工作。我唯一的问题是输入和输出之间的差异。输入文件为 UTF-8,输出为 ISO-8859-1。

当我添加

use utf8;
use open qw(:std :utf8);

不过,这个问题已经消失了。所以这一定是环境问题。

于 2013-01-17T15:02:58.560 回答