[~] perl -e 'use GetWord;$a=Get_Word();print"$a\n";' Enter command > abc your command was abc [~] perl -e 'use GetWord;$a=Get_Word();print"$a\n";' Enter command > o ->auto generated->one [~] perl -e 'use GetWord;$a=Get_Word();print"$a\n";' Enter command > tw ->auto generated->two #### #/usr/bin/perl package GetWord; use Carp; our @ISA = qw(Exporter); our $VERSION = 1.001; my $get_rid_of_warning = $VERSION; our @EXPORT = qw( Get_Word ); sub Get_Word { my @valid_commands = qw(one two three four five six seven); my $flush = $|; $| = 1; # flush output to screen byte by byte my $pass = ""; my $bs = "\b"; print "Enter command > "; while (1) { my $ch = getone(); # Return character, end of getting word $ch =~ /[\r\n]/ && do { print "\n"; $| = $flush; # return to previous setting print "\nyour command was $pass\n"; return; }; # Backspace/delete key entered $ch =~ /[\b\x7F]/ && do { if ( length($pass) > 0 ) { chop $pass; print ("$bs"," ",$bs) x length($pass),$bs," ",$bs, "$pass"; } next; }; # tab key entered $ch =~ /[\t]/ && do { my $num = (my @res = grep {/^$pass/} @valid_commands); if ($num == 1) { print "\n"; print "->auto generated->$res[0]\n"; return; } next; }; # Warn for non-ascii characters if ( ord($ch) < 32 ) { print "\7"; next; } # Valid character enetered $pass .= $ch; print $ch; } } # ============================================================================ # Terminal I/O program to get un-echoed unbuffered input # # Take from the Camel Book (3rd edition) # (Programming Perl, Larry Wall, O'Reilly), p.906 # ============================================================================ BEGIN { use POSIX ":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() }