in reply to Timed wait for input

If you want to ignore modules you could use the alarm function:

#!/usr/bin/perl -w use strict; $SIG{ALRM} = sub { warn "too late..."; exit; }; print "Gimme some input in 10 seconds\n"; alarm 10; print ">"; my $input = <>; print "your input was: $input\n";

Replies are listed 'Best First'.
Re^2: Timed wait for input
by saskaqueer (Friar) on Oct 23, 2005 at 19:25 UTC

    Forgive me if I am incorrect, but if you added more code that needed executing after the "your input was" printout, your script there would still be forced to quit after 10 seconds, as you do not reset the alarm in your code. Here's how I'd throw it together:

    #!/usr/bin/perl -w $| = 1; use strict; my $answer = 'n'; eval { local $SIG{ALRM} = sub { die("timed out\n"); }; print "Invoke script? (y/n) "; alarm(10); chomp( $answer = <STDIN> ); alarm(0); # reset the alarm so it won't go off later }; # user entered text other than 'y' or else the 10s is up if ($@ eq "timed out\n" or lc($answer) ne 'y') { die("No positive answer. Aborting execution.\n"); } # some other error blew within the eval - best to die() elsif ($@) { die($@); } # we got the 'y' answer, so now we continue: print "Executing remainder of script.\n";