in reply to Need to empty STDIN

It sounds like you want to add STDIN to IO::Select. When STDIN is readable, it will run the callback which will read everything it can. I didn't write the following, fellow monk edan did, but I'll post it in case he is out golfing or something. :-)
#!/usr/bin/perl use warnings; use strict; use IO::Select; # by edan of perlmonks # I just remembered what select says about mixing # buffered reading and "select", so even though the # code works, you might want to substitute # the read via <$fh> with: # my $input; # sysread( $fh, $input, 1024); # loop every 1 second my $timeout = 1; my $s = IO::Select->new(); $s->add( \*STDIN ); while (1) { if ( my @ready = $s->can_read($timeout) ) { # we got input for my $fh (@ready) { my $input = <$fh>; print "got: $input"; } } else { # no input } # just to show that we're looping print scalar localtime; }

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

Replies are listed 'Best First'.
Re^2: Need to empty STDIN
by bofh_of_oz (Hermit) on May 20, 2005 at 16:11 UTC
    I'm not sure if the fact that I'm on Windows changes anything, but this script:
    - does not run *every second* => it runs continuously (I mean, I get LOTS of output messages per second)
    - does not read STDIN - whatever I type, is processed by the OS after the script is terminated.

    --------------------------------
    An idea is not responsible for the people who believe in it...

      on Windows

      My condolences. :-)


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