Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Time-limited input on STDIN

by jamesduncan (Novice)
on Aug 08, 2003 at 22:33 UTC ( #282332=note: print w/ replies, xml ) Need Help??


in reply to Time-limited input on STDIN

I may be reading the problem wrong, but you could try using alarm and $SIG{ALRM} inside an eval block.

#!/usr/bin/perl use strict; use warnings; my $text; print "Prompt: "; alarm(10); eval { local $SIG{ALRM} = sub { die }; $text = <STDIN>; }; print "done accepting text\n";
James.


Comment on Re: Time-limited input on STDIN
Download Code
Re: Re: Time-limited input on STDIN
by bobn (Chaplain) on Aug 08, 2003 at 22:42 UTC

    You should use alarm(0) after the entry of data to turn off the alarm. Ottherwise, your program will be alarm'd even though you entered text in the allowed amount of time. See perldoc -f alarm for details.

    --Bob Niederman, http://bob-n.com
Re: Re: Time-limited input on STDIN
by ajdelore (Pilgrim) on Aug 11, 2003 at 17:58 UTC

    Thanks, this worked great. ++ to jamesduncan and bobn.

    One minor note: I did have to set local $/ = undef; within the eval block -- otherwise, it was finishing as soon as I pressed enter after a word.

    </ajdelore>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://282332]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2014-12-26 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls