UTF-8 encoding on filehandle in Perl
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-argumentopen
,readpipe
(akaqx//
) 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 } }
精彩评论