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


In reply to Determine encoding of STDOUT by Dirk80

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.