#!/perl/bin/perl -w use strict; my $input = &my_stdin( prompt =>"Enter some text: ", timeout => 3, handler => \&my_alarm ); print "You entered: $input \n"; sub my_stdin { use Term::ReadKey; $| = 1; #turn buffering off so we can see our print below my %args = @_; my ($output,$key ); my $prompt = $args{'prompt'}; my $timeout = $args{'timeout'}; my $toh = $args{'handler'}; # ref to a handler $SIG{ALRM} = $toh || sub { print "Timeout\n"; exit;}; alarm($timeout); print $prompt; ReadMode 4; while(1){ if(defined($key = ReadKey(-1))){ my $val = ord $key; # 8 is backspace 10 and 13 are $output .= $key unless $val == 8 || $val == 10 || $val == 13; #echo to the screen print $key if length $output > 0; alarm($timeout); #reset the alarm #last if the user hits return last if ( $val == 10 || $val == 13); # chop one if we get a backspace if( $val == 8 ){ chop $output; print " "; print chr(8); } } } ReadMode 0; print "\n"; $output; } sub my_alarm{ print "I just died because you waited too long!"; exit; }