I normally consider console-based captive interfaces "evil", though once in a while they're quite useful (Debian's debfoster tool comes to mind). As such, I haven't really dealt with this issue before. Yesterday, however, I found myself writing something with a captive interface as glue for a set of separate tools and utilities because I was tired of having to rethink what I'm doing every time I want to download a YouTube video (yes, really). I do so rarely enough that the series of steps I generally take isn't cemented in my mind, but commonly enough that it's still annoying to have to remind myself every time.

The wall I've run into is in answering yes or no to questions. To make this script really slick, I want to take a y/n/Enter keystroke as input, and not have to use the Enter key as a cue that input is done when entering either a y or n answer. Single-keystroke input is what I'm after.

The caveat is that I don't want to use an entire module that isn't part of the core Perl distribution, either. Thus, Term::Readkey doesn't seem to be the answer I seek. I also don't want to have to rely on system-specific shell commands, however -- I want to do it with Perl, not with bash or tcsh.

This doesn't seem like the sort of thing that should be terribly difficult, but I'm having a lot of trouble figuring out how it can be accomplished. Am I missing some global variable that, set to an empty string, would solve the endline requirement for input from STDIN? Is there a core function, or a function in a core module, that can be fit into this round hole?

Is there simply no way to do it without going outside the core Perl distribution (or reproducing a significant chunk of Term::Readkey)?

print substr("Just another Perl hacker", 0, -2);
- apotheon
CopyWrite Chad Perrin


In reply to y/n input in a captive interface by apotheon

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.