Eradicatore has asked for the wisdom of the Perl Monks concerning the following question:

I have a quick question, that I don't have time to test right now but I figured someone might have a quick reply. If I use IO::Select to tell me when I can read from STDIN, does that mean it will tell me as soon as anything is typed on STDIN, or just when there is a whole line to read (i.e. the user hit ENTER. Is this all just depending on if STDIN has autoflush set or not? Is that really what autoflush does? (I'm not sure I fully understand autoflush I guess).

Thanks!!!!!

Justin Eltoft

"If at all god's gaze upon us falls, its with a mischievous grin, look at him" -- Dave Matthews

Replies are listed 'Best First'.
Re: IO::Select question
by castaway (Parson) on Jan 21, 2003 at 13:38 UTC
    In the default state as soon as you've read something from STDIN, a loop doing a $sel->can_read() will always say it can read something from STDIN, even if you don't type anything more.
    Example:
    $sel = IO::Select->new(); $sel->add(\*STDIN); while(1) { print "waiting to read:\n"; my @handles = $sel->can_read(); print "can read\n"; foreach $h (@handles) { if($h == \*STDIN) { my $line = <STDIN>; my $finish = do_something_with_line($line); } } if($finish) {last;} }
    This will at first wait until you type something, and after that scroll with 'waiting to read' 'can read', even if you don't type anything more.
    (I've got a sneaky feeling it also 'can read' from STDIN as soon as you output something to STDOUT, as well.)
    If you take out the two 'print' lines the code still works as expected however, (i.e. it only calls do_something_with_line when it has a whole line including a newline from STDIN)

    C.

      Ok, I've read a little more now today, and your explanation seems to go against the purpose of IO::Select. I thought it was supposed to help you to only bother reading from a particular handle if there was something to read on that handle? Is that not right?

      Anyway, the bi directional client server application I want to write should not be affected by this question I know realize, because I'm the one writing both sides, and I'll just aways send a whole line at a time. Not just a few chars, which is what I was worried about happeneing and then causinng my ($line = <$handle>) to just sit there.

      Thanks for the feedback though! I'd be interested to hear what your thoughts are on if this is really how IO::select works.

        I won't pretend to know why it actually does that, maybe someone can answer that (it surprised me at the time too). It does only do it for STDIN as far as I can see, Sockets work exactly as you would expect (really waits until the other side sends some data).
        So maybe it's something to do with a special case for STDIN..

        C.