Perl 中文件句柄的 UTF-8 编码

发布于 2024-09-07 16:03:36 字数 197 浏览 2 评论 0原文

我将 UTF-8 编码应用于 STDIN 和 STDOUT。但是,如何确保将 UTF-8 编码应用于传递给下面代码的文件(如果在命令行上传递文本文件,<> 将从文件而不是 STDIN 中读取)线尽可能。

use open qw(:std :utf8)

while (<>) {
    print;
}

I'm applying UTF-8 encoding to STDIN and STDOUT. However how do I make sure that I apply UTF-8 encoding to the file that I pass to my code below (<> will read from a file instead of STDIN if a text file is passed on the command line) in as few lines as possible.

use open qw(:std :utf8)

while (<>) {
    print;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错爱 2024-09-14 16:03:36

根据 open pragma 的文档,您已经获得了所需的行为:

open 编译指示充当为所有 I/O 声明默认“层”(也称为“规则”)的接口之一。在此编译指示的词法范围内找到的任何双参数 openreadpipe(又名 qx//)和类似运算符都将使用声明的默认值。即使是三参数打开,当它们未在 MODE 中指定 IO 层时,也可能会受到此编译指示的影响。

perlop 文档告诉我们 while (<>) { ... } 相当于

 unshift(@ARGV, '-') 除非 @ARGV;
   而($ARGV = 移位){
     打开(ARGV,$ARGV);
     而 () {
       ... # 每行代码
     }
   }

According to the open pragma's documentation, you've already got the behavior you want:

The open pragma serves as one of the interfaces to declare default "layers" (also known as "disciplines") for all I/O. Any two-argument open, readpipe (aka qx//) and similar operators found within the lexical scope of this pragma will use the declared defaults. Even three-argument opens may be affected by this pragma when they don't specify IO layers in MODE.

The perlop documentation tells us that while (<>) { ... } is equivalent to

   unshift(@ARGV, '-') unless @ARGV;
   while ($ARGV = shift) {
     open(ARGV, $ARGV);
     while (<ARGV>) {
       ... # code for each line
     }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文