in reply to IO::Select question

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.

Replies are listed 'Best First'.
Re: Re: IO::Select question
by Eradicatore (Monk) on Jan 21, 2003 at 17:24 UTC
    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.

        One more question (maybe should be a new thread). For bidirectional, do I need to use two sockets? Or can the client and server both talk and listen on one socket (as long as they know who is talking and who is listening of course!).

        Thanks! J