in reply to y/n input in a captive interface

Hi apotheon,

I'm assuming, since you mentioned bash and tcsh that you're in Linux or Unix.

I've always used stty in conjunction with dd for this kind of thing.  For example, the following should help you get there:

use strict; use warnings; $| = 1; print "Enter some characters below, <Escape> to quit\n"; chomp(my $stty = `stty -g`); # Save state system("stty -icrnl -icanon -echo min 0 time 0"); # Raw mode while (1) { my $k = `dd bs=1 count=1 <&0 2>/dev/null`; next unless $k; # No character entered last if (27 == ord $k); # Finish if char is <Escape> printf "Ascii = %d\n", ord($k); } system("stty $stty"); # Restore cooked mode (thanks, sgt!) print "\nDone!\n";

It reads characters one at a time, displaying their ascii value, until you type an Escape.  Of course, you could easily modify it to read only a single character, or as many as you need.

Good luck!

Update:  Good catch sgt.  I've added the line system("stty $stty"); to restore "cooked" mode.


s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

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

    just noticing that you need another stty (with input $stty) to bring back the previous terminal settings..

    cheers --stephan
Re^2: y/n input in a captive interface
by apotheon (Deacon) on Feb 08, 2007 at 05:08 UTC

    Thanks for the suggestion, but unfortunately something that requires backticks or something like system() tends to be exactly the sort of thing (other than non-core modules) I wanted to avoid. It really hurts portability.

    Your assumption about using a Unixlike OS is correct -- I'm on FreeBSD -- but I don't want to limit the usefulness of this script to OSes that include the standard unixy core utilities. It's bad enough requiring a specific language interpreter (Perl's, specifically) to run the thing.

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