The encoding to be used for the standard I/O filehandles (STDIN
, STDOUT
, and STDERR
), can be set separately for each handle using binmode
:
binmode STDIN, ':encoding(utf-8)';
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
Note: when reading one would in general prefer :encoding(utf-8)
over :utf8
, see Remarks for more information.
Alternatively, you can use the 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'
Alternatively, to set all filehandles (both those yet to be opened and also the standard ones) to use :encoding(utf-8)
:
use open qw( :encoding(utf-8) :std );