in reply to prompt and timeout in tests

Have a look at alarm. Timing out input is the example in the documentation.

Update: Code:

printf "Answer: %d\n", yn(); sub yn { my $question = shift; $question ||='Your answer? '; my $val = undef; until (defined $val){ print "$question (y/n): "; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm 3; $val = <STDIN>; chomp $val; }; if ($@) { die $@ if $@ ne "alarm\n"; warn "No answer for three seconds.\n"; $val = 'n'; } # $val = # $val eq 'y' ? 1 : ( # $val eq 'n' ? 0 : undef # ); if ($val eq 'y'){ $val = 1; } elsif ($val eq 'n'){ $val = 0; } else { $val = undef; } } return $val; }

I tried this a couple times. If I don't answer, it looks like this:

Your answer? (y/n): No answer for three seconds. Answer: 0

...and that takes three seconds.

With an answer, it looks like:

Your answer? (y/n): y Answer: 1