Posted By

jsinix on 12/01/14


Tagged

get input perl with user timeout


Versions (?)

Perl get user input with timeout


 / Published in: Perl
 

URL: www.jsinix.com

Author: jsinix([email protected])

This is a script that demonstrates how to get input from keyboard with a timeout. This can be useful in many places.

  1. #!/usr/bin/perl
  2. # This is a script that demonstrates
  3. # how to get input from keyboard
  4. # with a timeout. This can be useful
  5. # in many places.
  6.  
  7. use strict;
  8. use warnings;
  9.  
  10. sub ask_data {
  11. my ($tout) = @_;
  12. my $answer;
  13.  
  14. print "Enter the data before $tout seconds: ";
  15. eval {
  16. local $SIG{ALRM} = sub { die "timeout reading from keyboard" };
  17. alarm $tout;
  18. $answer = <STDIN>;
  19. alarm 0;
  20. chomp $answer;
  21. };
  22. die [email protected] if [email protected] ne "timeout reading from keyboard";
  23. $answer = 'No answer given';
  24. }
  25. return $answer;
  26. }
  27.  
  28. my $data = ask_data('10');
  29. print "\nThe answer is: " . $data . "n";

Report this snippet  

You need to login to post a comment.