in reply to y/n input in a captive interface

Firstly, Term::Readkey is a pretty small module so it's no great overhead to install and use it and it seemed to do everything right whenever I used it. My advice would be to go with that.

However, if you are unable to do that you probably have to go with changing terminal settings by hand. I copied a recipe out of the 2nd edition Camel Book a long, long time ago and I haven't used it in years. It may give you a start though so here it is.

# RawTerm.pm # # Adapted from "Programming PERL", Wall/Christiansen/Schwartz, 2nd Edn +. pp.474 # package RawTerm; use Exporter; @ISA = ('Exporter'); @EXPORT = ('getone'); BEGIN { use POSIX qw(:termios_h); my($term, $oterm, $echo, $noecho, $fd_stdin); $fd_stdin = fileno(STDIN); $term = POSIX::Termios->new(); $term->getattr($fd_stdin); $oterm = $term->getlflag(); $echo = ECHO | ECHOK | ICANON; $noecho = $oterm & ~$echo; sub cbreak { $term->setlflag($noecho); $term->setcc(VTIME, 1); $term->setattr($fd_stdin, TCSANOW); } sub cooked { $term->setlflag($oterm); $term->setcc(VTIME, 0); $term->setattr($fd_stdin, TCSANOW); } sub getone { my $key = ""; cbreak(); sysread(STDIN, $key, 1); cooked(); return $key; } } END { cooked(); } 1;

If I remember correctly you just call the getone() subroutine to get a single character without needing the <Enter> key. I hope this is of use.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^2: y/n input in a captive interface
by apotheon (Deacon) on Feb 08, 2007 at 05:04 UTC

    Firstly, Term::Readkey is a pretty small module so it's no great overhead to install and use it and it seemed to do everything right whenever I used it. My advice would be to go with that.
    It's not much overhead for the processor or RAM, but it's a fair bit of overhead for someone who isn't a Perl programmer and is thus unlikely to be familiar with the task of installing nonstandard Perl modules. It's more end-user overhead that I am to avoid, rather than system overhead, in this case.

    As things currently stand, I have a workaround built into the thing so that it uses Term::ReadKey if it exists on the system (using an eval), but it's pretty kludgey, and it ends up having less slick, intuitive behavior for the less technically savvy user as a result -- pretty much the exact opposite of a desirable state of affairs.

    As for your example code, it unfortunately looks pretty environment-specific (POSIX::Termios makes me think so, anyway). I prefer to avoid assumptions about the operating environment as much as possible when writing stuff like this, where its usefulness is not limited to a particular OS by definition. I appreciate the attempt at helping, though it ultimately doesn't really provide what I need.

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