Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input Timer

Status
Not open for further replies.

CJason

Programmer
Oct 13, 2004
223
US
Here is my current code:
Code:
$remark = "";
print "Remark: ";
chomp($remark = <STDIN>);
Is there a way to put some sort of timer on this, so that, if the user doesn't type something like within 10 seconds, the process will move on?

Thanks!
 
Oooh, I think I found a good example (this is untested, but I think it's pretty close):
Code:
$remark = "";
$SIG{ALRM} = goto skip_it;
alarm 10;
print "Remark: ";
chomp($remark = <STDIN>);
skip_it:
alarm 0;
Anyone see any major issues?
 
Only works well on non windows systems.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
What do you mean by "works well"? Are you referring to the timer accuracy? If that's the case, it's no problem for what I'm doing. If the timer is 9.5-10.5 seconds (when I have it set to 10 seconds), it really doesn't matter to me.
 
travs69, do you have an alternative way of doing it?

Thanks!
 
No.. never found a good way to do it on windows. I have seen some people say they got alarm to work but I never have. Unix/Linux it works great.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Please excuse the side-tracking here, but TclTk works *very* well (for me) at event programming. I'm an absolute devotee to perl, but there are some languages that simply are more suited for certain applications. I've never tried perl's tk extensions, so I can't apply any good information from that perspective. But I can (and often do) get these two languages to work hand-n-hand.
 
So, as travs69 has stated...I got this to work great on UNIX, but not on Windows. It seems that alarm() works on Windows (perl 5.8+)...but not for all scenarios. For example, this works:
Code:
eval {
  local $SIG{ALRM} = sub { die "alarm\n" };
  alarm 5;
  sleep 100;
};
alarm 0;
But, this does not:
Code:
eval {
  local $SIG{ALRM} = sub { die "alarm\n" };
  alarm 5;
  print "Remark: ";
  chomp($remark = <STDIN>);
};
alarm 0;
I wonder if it has something to do with waiting for <STDIN>?

Like I said, both scenarios above work on UNIX, but only the first works on Windows.

Any ideas or suggestions????
 
Try IO::Select...


I've never used it on STDIN or other local filehandles; I've only used it with sockets (which are filehandles themselves, so I don't see why it shouldn't work)...

Add *STDIN to a Select object and do can_read with a timeout. If they type something by the time the timeout expires, can_read will return the filehandle which you then need to sysread() or read() or recv() on (i.e. the traditional <STDIN> syntax won't work when combined with IO::Select, you'll need to do something a little lower-level here).

Alternatively, you could also just thread it too...

Code:
use threads;
use threads::shared; # threads by default don't share

my $remark : shared; # so we share this variable

# spawn a new thread to wait for input
my $in = threads->new (sub {
   # Respond to kill signals
   $SIG{KILL} = sub { threads->exit(); };

   # Wait for input
   print "Remark: ";
   chomp ($remark = <STDIN>);
});

# control continues here immediately, even while the thread is waiting
sleep 5;

# if we have no input
if (not defined $remark) {
   # then we can kill off the thread.
   $in->kill('KILL')->detach;
}
else {
   # we did get some input, do whatever with it
   print "we got $remark!";
}

See the pages on CPAN for threads and threads::shared. This isn't tested though and ActivePerl might not like you killing a thread that's waiting for STDIN very much either.

-------------
Cuvou.com | My personal homepage
Project Fearless | My web blog
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top