views:

116

answers:

2

I have a perl script that prints some information to console in Russian. Script will be executed on several OSes, so console encoding can be cp866, koi8-r, utf-8, or some other. Is there a portable way to detect console encoding so I can setup STDOUT accordingly so the text is printed correctly?

+1  A: 

If you're on Windows, there's Win32::Codepage.

Snake Plissken
The script will be run on any OS
n0rd
On a Windows console, this is the best solution I know of. I don't know of a way of detecting the output encoding on Linux or other Unix-like systems. I usually set the terminal to UTF-8 encoding and use that for Perl. On Windows, as far as I know it is not possible to set the codepage of the terminal window. As for a portable solution, that would be great but I don't know if it exists.
Snake Plissken
On windows console encoding is set with `chcp` program, I suppose there is a programmatic solution also. Anyway, forcing some particular console encoding is not a solution.
n0rd
This doesn't force an encoding, it detects the encoding. I don't know of a way to even change the encoding in the Windows console.
Snake Plissken
`chcp /?` output: "Displays or sets the active code page number."
n0rd
And it can't "detect" anything by design: encoding is directly specified by caller.
n0rd
A: 

use open ':locale';

daxim
Doesn't work on Windows. At least I failed to make it work. I believe it relies on environment variables to detect console encoding which usually are not set on Windows.
n0rd