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

In reply to Re: "killing" perl5.8-threads by nothingmuch
in thread "killing" perl5.8-threads by strat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.