The function
select (four argument) checks for input (and more), with a timeout specifiable. What you need to do is tell select to peek at STDIN. You specify it via creating a bit vector, with the file descriptor for a handle you want to check.
fileno returns the file descriptor for a file handle.
select will block, and if some input is available it'll work.
Note that buffering may do funny stuff though... On my MacPerl "terminal" select will not detect input until a newline is entered, because select is not stdio aware. Which brings me to it's caveat: don't use it with stdio unless you're well aware of how the buffering works. You should usually use it with
sysread (and
syswrite when testing for output readiness). Provided that you haven't read on STDIN yet, this example should work (it may work if you already read, but i can't guarantee):
my $rin = '';
vec($rin,fileno(STDIN),1) = 1;
my $rout;
my $val = "Default";
if (select($rout = $rin,undef,undef,5)){
my $val = <STDIN>;
} else {
print "Default used\n";
}
print "\n\nThe value: $val\n";
Update: I'm sorry, I didn't realize I didn't answer your question at all... =P
In general, to stop blocking, you have to get something on the outside to tell you to stop. You need to allow the blocking thread to receive such a message, from someone else, telling it when to stop. The eval and alarm pairing is the most common for timeouts, and it has already been described.
alarm gets the kernel to tell you when to stop. I think, however, that win32 has alarm prollums. Try
Super Searching on alarm and windows... If I recall
Win32::Events may be of aid aswell.
-nuffin
zz zZ Z Z #!perl