Skip to content

Instantly share code, notes, and snippets.

@vshymanskyy
Last active December 15, 2015 03:29
Show Gist options
  • Save vshymanskyy/5194697 to your computer and use it in GitHub Desktop.
Save vshymanskyy/5194697 to your computer and use it in GitHub Desktop.
Cross-platform module to detect and set binmode for standard files; Requires Encode::Locale to detect console encoding. Defaults to utf8 I/O.
package StdBinmode;
BEGIN
{
if ($^O eq 'MSWin32')
{
eval <<END;
use Encode;
use Encode::Locale;
binmode(STDIN, ((-t STDIN) and find_encoding('console_in')) ?
':encoding(console_in)' : ':encoding(utf8)');
if (find_encoding('console_out')) {
binmode(STDOUT, (-t STDOUT) ? ':encoding(console_out)' : ':encoding(utf8)');
binmode(STDERR, (-t STDERR) ? ':encoding(console_out)' : ':encoding(utf8)');
} else {
binmode(STDOUT, ':encoding(utf8)');
binmode(STDERR, ':encoding(utf8)');
}
END
die "$@\n" if $@;
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment