Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Timed wait for input

by saskaqueer (Friar)
on Oct 23, 2005 at 19:25 UTC ( [id://502332]=note: print w/replies, xml ) Need Help??


in reply to Re: Timed wait for input
in thread Timed wait for input

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";

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-03-29 14:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found