如何使用perl脚本打开包含CJK
个字符的文件:
use utf8;
use open ':encoding(utf8)';
binmode STDOUT, ':utf8';
上面的代码我用来打开文件而我找不到CJK
个字符而且它是空的。输入文件包含文本:
\para[para]Details of the electronic structure of the metal the loss to
electron-hole-pair excitations \characters{刘安雯} only
depends weakly on the metal. The metals exhibit large variation in the
work function, yet the translational in elasticity is similar in all
cases. This suggests electron transfer forming a transient
H\textsuperscript{{\textminus}} is not important. The simulation allows
us to construct \characters{胡水明} a universal sticking
function for H and D on metals, which depends only on the H atom
incidence translational energy and incidence angle as well as the mass of
the solid's atoms.\endp
我发现了这种方式:
while($str=m/(\p{InCJK_Unified_Ideographs})/xg)
{
print "Char: --> $&\n";
}
有人可以在我的代码中指导我做错的地方:谢谢。
更新
我不知道,但这个程序运行正常并打印
CJK
字符
use utf8;
my $str = "\characters{刘安雯胡水明}";
while($str=~m/(\p{InCJK_Unified_Ideographs}){1,}/xg) { print ":: $&\n"; }
答案 0 :(得分:1)
use utf8;
这一行告诉Perl源代码包含UTF-8,因此它与从文件读取无关。
use open ':encoding(utf8)';
这相当于
use open IO => 'encoding(utf8)';
设置输入和输出流的编码,即它不会改变标准输入和输出的编码。为此,您需要添加:std
:
use open IO => ':utf8', ':std';
显示的最后一行
binmode STDOUT, ':utf8';
设置STDOUT的编码,如果它使用:std
,则前一行已经覆盖了该编码。
您没有显示如何打开文件。如果您在未指定文件句柄的情况下使用<>
或readline,则需要设置标准输入的编码,如上所示。如果你使用了文件句柄,我就没有想法 - 它对我有用。