Perl Unicode Cookbook: Decode Standard Filehandles as UTF-8
℞ 15: Declare STD{IN,OUT,ERR}
to be UTF-8
Always convert to and from your desired encoding at the edges of your programs. This includes the standard filehandles STDIN
, STDOUT
, and STDERR
.
As documented in perlrun, the PERL_UNICODE
environment variable or the -C
command-line flag allow you to tell Perl to encode and decode from and to these filehandles as UTF-8, with the S
option:
$ perl -CS ...
# or
$ export PERL_UNICODE=S
Within your program, the open pragma allows you to set the default encoding of these filehandles all at once:
use open qw(:std :utf8);
Because Perl uses IO layers to implement encoding and decoding, you may also use the binmode operator on filehandles directly:
binmode(STDIN, ":encoding(UTF-8)");
binmode(STDOUT, ":encoding(UTF-8)");
binmode(STDERR, ":encoding(UTF-8)");
Previous: ℞ 14: Decode @ARGV as Local Encoding
Series Index: The Standard Preamble
Tags
Feedback
Something wrong with this article? Help us out by opening an issue or pull request on GitHub