Perl Programm一直在等待使用utf8打开文件

时间:2013-01-17 12:15:16

标签: perl

我正在尝试读取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编码输出。

1 个答案:

答案 0 :(得分:0)

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

当我添加

use utf8;
use open qw(:std :utf8);
但是,这个问题已经消失了。所以这必定是一个环境问题。