Perl获取用户输入超时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Perl获取用户输入超时相关的知识,希望对你有一定的参考价值。

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 " The answer is: " . $data . "n";

以上是关于Perl获取用户输入超时的主要内容,如果未能解决你的问题,请参考以下文章