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?
The script will be run on any OS
n0rd
2010-06-06 13:32:14
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
2010-06-06 14:56:51
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
2010-06-06 16:03:54
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
2010-06-06 22:20:34
`chcp /?` output: "Displays or sets the active code page number."
n0rd
2010-06-08 08:24:43
And it can't "detect" anything by design: encoding is directly specified by caller.
n0rd
2010-06-08 08:29:15
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
2010-06-11 09:08:35