solar_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, How can I run a timer counting how long it takes a user to input a response? This is runnung on unix based system and basically I want to count to 30 seconds and if the user hasnt responded to a question by then, give up waiting and move on to the next bit of code.

Im afraid Im pretty new to perl and have had a trundle through undoubtably badly constructed google searches - any help is much appreciated.

Many thanks!!

Replies are listed 'Best First'.
Re: Timing user input
by moritz (Cardinal) on Jul 06, 2008 at 22:41 UTC
Re: Timing user input
by GrandFather (Saint) on Jul 06, 2008 at 22:03 UTC

    Probably Term::ReadKey does what you want.


    Perl is environmentally friendly - it saves trees
Re: Timing user input
by johngg (Canon) on Jul 06, 2008 at 22:52 UTC
    You could have a look at this node that has an example of how to do what you want.

    Cheers,

    JohnGG

Re: Timing user input
by starbolin (Hermit) on Jul 07, 2008 at 05:00 UTC

    #!/usr/bin/perl sub catch_alarm { die "Timeout" } for (;;) { eval { local $SIG{ALRM} = \&catch_alarm; undef $!; print "What do you want? >"; alarm 10; if (defined( $line = readline STDIN )) { alarm 0; chomp $line; print "Here's some $line!\n"; } elsif ($!) { die "Read error" ; } }; if ( $@ =~ /Timeout/ ) { print "\nI grow tired of waiting!\n"; } } What do you want? >foo Here's some foo! What do you want? >qlarch Here's some qlarch! What do you want? > I grow tired of waiting! What do you want? >


    s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}