Return to Snippet

Revision: 68126
at December 1, 2014 03:26 by jsinix


Initial Code
#!/usr/bin/perl
# This is a script that demonstrates 
# how to get input from keyboard 
# with a timeout. This can be useful
# in many places.

use strict;
use warnings;

sub ask_data {
    my ($tout) = @_;	
    my $answer;

    print "Enter the data before $tout seconds: ";
    eval {
        local $SIG{ALRM} = sub { die "timeout reading from keyboard" };
        alarm $tout;
        $answer = <STDIN>;
        alarm 0;
        chomp $answer;
    };
    if ($@) {
        die $@ if $@ ne "timeout reading from keyboard";
        $answer = 'No answer given';
    }
    return $answer;
}

my $data = ask_data('10');
print "\nThe answer is: " . $data . "n";

Initial URL
www.jsinix.com

Initial Description
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.

Initial Title
Perl get user input with timeout

Initial Tags
perl, user

Initial Language
Perl