2 ideas:

a) the bad one: continue translating the rest into perl and worry later by temporarily using $key=`read -n3 key; echo \$key`;. Advantage: you stick to the task at hand.

b) the trivial one would be something like $c=""; do{$c.=ReadKey(0); last if $c=~/[\x0a]/} for (1..<small number for sequence length max>);. (0x0a/LF would also resynchronize the user's notion of 'char sequence starts' with the terminal's notion - 'the may need to press enter' bit).

both quick&dirty approaches should be only used for a very small number of expected keys. At least the user can resynch the tty by pressing enter, so it's not too bad.

The trick with b is (AFAIS not 100% properly done by read -n3) to start only outside a multi-char sequence. And to stop only when you've a full valid key, be it 'c' or CSI-LONG-LONG-STRING...LAST-CHAR-of-CONTROL-SEQUENCE. Also consider multibyte-characters (or add a warning comment like #ignoring multibyte chars for now; utf-8 characters can be quite long in bytes and you should probably test what ReadKey does see in an unicode xterm for e.g. an Umlaut. Then there's stuff like BIG5 and other older encodings).

If there's some sanity to CSI-style esc code sequence length I've yet to read about it (excluding the trivial enumeration approach: a table for all sequences for the current terminal settings; to be available for possible terminalsettings and termcap entries; weighing in at maybe a dozen MB or so ;> ).

As for curses, a quick grep shows that e.g. Curses::UI::Common::get_key has e.g. code to collect the individual ReadKey's or getch's into a variable, with a cautious comment of "I have no idea of the portability of this stuff but it works for me" :/.

Maybe there's a simple module in between ReadKey and full Curses to get the whole key sequence into a variable. Anyone has seen possible candidates?

Might be quest-level stuff for the general case:)

update:

please also include non-curses / partial terminal handling modules, if they offer abstractions beyond ReadKey.

Thx to bichonfrise for his pointer Curses::Simple.

My own hoard of never-used just-in-case-examples lists perlmenu, Curses/Perl::UI, and Curses::Widgets, all of which are rather complete user interfaces on a level far higher than ReadKey. Lower than ReadKey and of historic interest is the approach in Larry Wall's process-killer zap of camelbook fame. Might have appeared already in the 1st Ed as it was pure perl4 and eminently hackable.


In reply to Re: Translating Terminal Input Code from Bash to Perl by jakobi
in thread Translating Bash to Perl by Anonymous Monk

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.