Perl Unicode 美食馆:使所有 I/O 默认使用 UTF-8
℞ 18: 使所有 I/O 和 args 默认使用 utf8
Perl 中 Unicode 处理的核心规则是“始终在程序的边缘进行编码和解码”。
如果您已配置所有传入和传出数据都使用 UTF-8 编码,则可以令 Perl 为您执行适当的编码和解码。如perldoc perlrun 文档所述,有 -C
标志和 PERL_UNICODE
环境变量可用。使用 S
选项使标准输入、输出和错误文件句柄使用 UTF-8 编码。使用 D
选项使所有其他文件句柄使用 UTF-8 编码。使用 A
选项将 @ARGV
元素解码为 UTF-8
$ perl -CSDA ...
# or
$ export PERL_UNICODE=SDA
在您的程序中,您可以使用 open 前置声明在文件句柄上设置默认编码,并使用 Encode 模块解码 @ARGV
的元素
use open qw(:std :utf8);
use Encode qw(decode_utf8);
@ARGV = map { decode_utf8($_, 1) } @ARGV;
系列索引: 标准序言
下一页: ℞ 19: 指定文件的编码
标签
反馈
这篇文章有什么问题吗?通过在 GitHub 上打开一个问题或拉取请求来帮助我们
LATEST COMMUNITY ARTICLES
- More commenting... maybe?
github.polettix.it - Perl Weekly Challenge 121: Invert Bit
blogs.perl.org - Web nostalgia: MojoX::Mechanize
github.polettix.it - On the eve of CPAN Testers
blogs.perl.org - PWC121 - The Travelling Salesman
github.polettix.it - PWC121 - Invert Bit
github.polettix.it - Floyd-Warshall algorithm implementations
github.polettix.it - Perl Weekly Challenge 120: Swap Odd/Even Bits and Clock Angle
blogs.perl.org - How I Uploaded a CPAN Module
blogs.perl.org - App::Easer released on CPAN
github.polettix.it