繁体   English   中英

如何在Perl中打开包含CJK字符的文件

[英]How to open a file contains CJK characters in perl

如何使用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";  }
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而不指定文件句柄,则需要设置标准输入的编码,如上所示。 如果您使用文件句柄,那么我就没主意了-它对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM