There's two ways I can think of to do it - one is via threading your perl, to make your 'reading STDIN' run in parallel. Something like:

#!/usr/bin/perl use strict; use warnings; use threads; use Thread::Queue; my $input_q = Thread::Queue -> new(); sub stdin_reader { while ( my $line = <STDIN> ) { if ( defined $input_q -> pending() ) { $input_q -> enqueue ( $line ); } else { last; } } } sub doing_stuff_thread { my $count = 0; while ( $count < 10 ) { my $item = $input_q -> dequeue_nb(); if ( defined $item ) { print "Got: $item"; $count++; } else { print "Doing something else, because nothing on STDIN\n"; print "Pending items:", $input_q -> pending(), "\n"; sleep 1; } } $input_q -> end(); } threads -> create ( \&stdin_reader ); threads -> create ( \&doing_stuff_thread ); foreach my $thr ( threads -> list() ) { print "Waiting for ",$thr -> tid()," to join\n"; $thr -> join(); }

That works, although bear in mind that 'STDIN' is being read line by line, so you need to put carriage returns in. (you can't do 'readkey' style). (And also - you may find 'Thread::Queue' isn't 'standard', but I'm sure you can figure out how to do something useful with thread::shared and lock instead.

The other way I can think of involves using IO::Select. Or perhaps like this node: Here

#!/usr/bin/perl use strict; use warnings; use IO::Handle; use IO::Select; my $nb_stdin = new IO::Select( *STDIN ); while ( 1 ) { if ( $nb_stdin -> can_read(0) ) { my $line = <STDIN>; print "Got: ", $line,"\n"; } else { print "Nothing on STDIN, doing something else\n"; sleep 1; } }

This doesn't seem to work properly on Win32/Activestate though - I believe I've seen something to the effect that IO::Select doesn't work on non-sockets on W32. YMMV


In reply to Re: Checing presence of input on standard input in non-blocking way, sans user interaction by Preceptor
in thread Checing presence of input on standard input in non-blocking way, sans user interaction by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.