in reply to Weirdness with IO::Select and IPC::Open3

Why did IO::Select brings GLOB(0x880fe78) up as ready for reading at all, when it was opened as the write (input) filehandle?

select actually signals when a file handle needs to be serviced. In addition to "ready to read" for can_read, that includes EOF and error conditions.

In this case, the OS is ready to communicate to you that you can't read from a write-only handle.

In general, you want two select objects, one for readers and one for writers. If you want to wait for either, you'd use select instead of can_*.

my ($r, $w) = IO::Select::select($readers_sel, $writers_sel, undef);

I don't understand why the program terminates after writing the answer to the first question.

You only call can_read once. You appear to be missing a loop.

Replies are listed 'Best First'.
Re^2: Weirdness with IO::Select and IPC::Open3
by rastoboy (Monk) on Mar 22, 2011 at 01:41 UTC
    In this case, the OS is ready to communicate to you that you can't read from a write-only handle.

    Ikegami, could you by any chance point me to documentation that explains what you're talking about, here? The more I think about it, the less sense it makes, and I'm dying for a doc that just tells me how this system works. I don't even know what "system" I'm talking about, here! :-)

    I mean, it seems to me I tell IO::Select to watch these two file handles, one of which has been specified as the STDOUT filehandle for the child process--I just can't grasp how that filehandle should ever show up in can_read().

    I know I'm wrong, but I'd like to get properly educated on the subject--I feel like I'm stabbing in the dark, still, know what I mean?

      The purpose of select is to parallelise a few things including reading from the handles identified by its first argument. As such, select should return whenever sysread would return for any of those handles.

      sysread obviously returns when provided a write-only handle.

      $ perl -E' open(my $fh, ">", "file") or die $!; my $rv = sysread($r[0], $buf, 100); if (!defined($rv)) { say "Error: $!"; } elsif (!$rv) { say "eof"; } else { say "Got $rv bytes"; } ' Error: Bad file descriptor

      As such, select should do the same.

      $ perl -MIO::Select -E' open(my $fh, ">", "file") or die $!; my @r = IO::Select->new($fh)->can_read(); my $rv = sysread($r[0], $buf, 100); if (!defined($rv)) { say "Error: $!"; } elsif (!$rv) { say "eof"; } else { say "Got $rv bytes"; } ' Error: Bad file descriptor

      You're saying select should block forever in the event of an error even though sysread would not. That makes no sense.

        Awesome, and very helpful, thanks!