标准 IO

可以使用 binmode 为每个句柄单独设置用于标准 I / O 文件句柄(STDINSTDOUTSTDERR)的编码

binmode STDIN, ':encoding(utf-8)';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';

注意:阅读时,通常更喜欢:encoding(utf-8) 而不是:utf8,请参阅备注以获取更多信息。

或者,你可以使用 open pragma。

# Setup such that all subsequently opened input streams will use ':encoding(utf-8)'
# and all subsequently opened output streams will use ':utf8'
# by default
use open (IN => ':encoding(utf-8)', OUT => ':utf8');
# Make the (already opened) standard file handles inherit the setting 
# given by the IO settings for the open pragma
use open ( :std );
# Now, STDIN has been converted to ':encoding(utf-8)', and 
# STDOUT and STDERR have ':utf8'

或者,要设置所有文件句柄(包括尚未打开的文件句柄以及标准文件句柄)以使用:encoding(utf-8)

use open qw( :encoding(utf-8) :std );