I managed to get correct Unicode output displayed by using Win32::API to call the Console (wide version) primitives, using Encode to convert the strings to UTF-16LE. I wrapped it up using Tie::Handle and my program was able to use it without further impact.

Note that the Win32::Console module does not support Unicode. I'm supposing it only calls the narrow version of the API.

Now I'm contemplating making a polished module for this. I want feedback before I get around to making this more mature implementation and uploading it.

The prospective name is Win32::ConsoleOutput, but I'm still taking suggestions.

The primary purpose is to allow Perl programs to print output to the interactive console window, using the full character set.

Upon using this module, you can open a Perl handle (really a tie, not an actual file handle that the OS knows about) to do Console output, and it will take Perl's native encoding and make it just work.

There will be a handy way to tell it to replace STDOUT with this, if and only if STDOUT is not being redirected.

I'm also thinking about a related issue: more advanced terminal control. Other kinds of processing of the data stream are already handled by making a filter handle that sits in front of the real output, so the only business this has should be terminal-related stuff. In particular, changing colors and moving the cursor around.

Previously, it's been voiced that nobody likes the ANSI terminal stuff. Here's an idea for an extension mechanism that can handle this or whatever:

Have a hash as part of the ConsoleOutput object that maps characters to callbacks. Print a character that's listed, and the callback is invoked and the character filtered out. Use a couple chars in the Private Use zone to implement color codes, or add whatever you want. As a shortcut, a string (rather than a code ref) is a substitution. Use that to replace unprintable characters or specify alternate glyphs.

What do you think?

—John


In reply to Module idea: Win32::ConsoleOutput by John M. Dlugosz

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.