开发者

UTF-8 encoding on filehandle in Perl

开发者 https://www.devze.com 2023-01-05 18:59 出处:网络
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 tex

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 lin开发者_JAVA百科e) in as few lines as possible.

use open qw(:std :utf8)

while (<>) {
    print;
}


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
     }
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消