Dirk80 has asked for the wisdom of the Perl Monks concerning the following question:
Hello wise monks,
I have a script which prints a character to STDOUT. Unfortunately I do not know the encoding of STDOUT.
Here I have my emacs editor. Its compilation window is encoded in UTF8. So if I execute this script within emacs the encoding of STDOUT should be in UTF8 to display the character correctly.
On the other hand I have here the windows commandline (cmd). The encoding there is 'cp437'. So the STDOUT should be encoded in 'cp437'
If I set the encoding of STDOUT to UTF8 then it is displayed correctly in the compilation window of emacs, but it is displayed wrong in the command line of windows. If I set the encoding of STDOUT to 'cp437' then it is correct in the windows command line but wrong in the emacs compilation window.
Here my code:
#!/usr/bin/perl use strict; use warnings; use charnames ':full'; use Encode; # TODO: determine encoding of STDOUT my $enc_of_stdout; # compilation window of emacs is encoded in UTF8 $enc_of_stdout = 'utf8'; # cmd window in Windows XP is encoded in CP437 #$enc_of_stdout = 'cp437'; binmode(STDOUT,":encoding($enc_of_stdout)"); # same as: "\x{f2}" my $text_str = "\N{LATIN SMALL LETTER O WITH GRAVE}"; print "$text_str\n";
My goal is that this script is working independent of the encoding of STDOUT. So the script should be able to find out the encoding of STDOUT at the beginning.
How can I find out what the encoding of STDOUT of the caller of the script is?
Or am I thinking to difficult and there is an easier way?
Thank you for your help
Dirk
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Determine encoding of STDOUT
by moritz (Cardinal) on May 04, 2011 at 13:06 UTC | |
by CountZero (Bishop) on May 04, 2011 at 18:49 UTC | |
by Dirk80 (Pilgrim) on May 04, 2011 at 13:47 UTC | |
by moritz (Cardinal) on May 04, 2011 at 15:55 UTC | |
by ikegami (Patriarch) on May 04, 2011 at 17:03 UTC | |
|
Re: Determine encoding of STDOUT
by tchrist (Pilgrim) on May 04, 2011 at 12:50 UTC | |
by Dirk80 (Pilgrim) on May 04, 2011 at 13:59 UTC | |
by Jenda (Abbot) on May 04, 2011 at 15:46 UTC | |
by ikegami (Patriarch) on May 04, 2011 at 17:00 UTC | |
by tchrist (Pilgrim) on May 04, 2011 at 17:45 UTC | |
by ikegami (Patriarch) on May 04, 2011 at 18:44 UTC | |
by Anonymous Monk on May 04, 2011 at 18:28 UTC | |
|
Re: Determine encoding of STDOUT
by Anonymous Monk on May 04, 2011 at 12:53 UTC |