標準 IO
可以使用 binmode
為每個控制代碼單獨設定用於標準 I / O 檔案控制代碼(STDIN
,STDOUT
和 STDERR
)的編碼 :
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 );