in reply to Timeout on STDIN

Heres another snippet.
#!/usr/bin/perl use strict; use warnings; use Term::ReadKey; sub timed_input { my $end_time = time + shift; my $string; do { my $key = ReadKey(1); $string .= $key if defined $key; } while (time < $end_time); return $string }; my @words = split ' ', timed_input(10); print "@words\n";

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re^2: Timeout on STDIN
by boleary (Scribe) on Oct 12, 2016 at 13:39 UTC

    I am trying zentaras snippet, and failing...

    my perl script got started as a child process, so the parent communicates via STDIN to my script. There is no real keyboard involved. Unfortunately I'm not really up to speed on terminals and tty. I'm wondering if Term::ReadKey should work in that case.

    This is a win32 application

    sub get_stdin_w_timeout { ######!/usr/bin/perl #got from zentara on perl monks in response to Timeout on STDIN #modifying to use ReadKey to get first key and then <STDIN> to get t +he rest of the line #this function assumes that once there is some input on stdin, #the rest of the line is available too use strict; use warnings; use Term::ReadKey; my $timeout_in_sec=shift; my $end_time = time + $timeout_in_sec; my $key=""; do { my $key = ReadKey(1); if (defined $key) { #not sure if I need to concatenate $key on the front of <STDIN> return $key. <STDIN>; } } while (time < $end_time ); return "!ERROR!:Timed out waiting on input after ${timeout_in_sec}s\ +n"; }

    when i run this like this

    $plibMsg=&get_stdin_w_timeout(5);#<STDIN>;

    I always get the Timed out message

        Thanks... I thought I had that set but I didn't I tried again with

         $|=1  $msg=<STDIN>

        still works

        but the ReadKey never seems to get characters

        $key = ReadKey(1);

        My script is being opened as a child of another process and that process prints strings to stdin (not using a keyboard)