in reply to Re^6: open3 and illegal seeks
in thread open3 and illegal seeks

Does this pass muster:

When a reading error occurs, you don't say which error.

When the data received doesn't match the data you expected to receive, you incorrectly report a reading error (rather than a protocol error and specifying what you expected to receive).

Programming Perl, 3rd Edition book has the $! icon next to the readline function

readline does set $! when an error occurs. Unfortunately, it has no way of letting you know when an error occurred. That's where eof comes into play.

The docs for readline have been updated for 5.12.

Replies are listed 'Best First'.
Re^8: open3 and illegal seeks
by brainsick (Sexton) on Jul 08, 2009 at 16:28 UTC
    Since I'm now only reading from $PROG_STDERR when there's data to be read (!eof), reading nothing does indicate an error, right?

    It then stands to reason that the error will be stored in $! since the failure happened during the readline, right?

    If I read between the lines, I think you're saying that by not checking eof after the read, I don't know whether I read all of the data. What conditions would cause me to be able to read some data and then fail midway through? Is that a likely scenario?

      Since I'm now only reading from $PROG_STDERR when there's data to be read (!eof), reading nothing does indicate an error, right?

      It it was a file... But we're talking about a pipe. It might not be closed when you check eof, but it could close after you started trying to read from it.

      Or so I thought.

      eof apparently does a read to find out if more data is coming.

      $ perl -wle' open(my $fh, "-|", sleep => 5) or die $!; print eof($fh)?1:0; print defined(scalar(<$fh>))?1:0; print eof($fh)?1:0 ' <<eof blocks for 5 seconds here>> 1 0 1

      This behaviour is very different than the behaviour of stdio's feof, but it's documented: eof "Returns 1 if the next read on FILEHANDLE will return end of file, or if FILEHANDLE is not open."

      So, you're right. Reading nothing in that situation would indicate a reading error (and thus, you can print out $!).

      (No consideration was given to non-blocking handles.)

        If the data read from $PROG_STDERR consisted of "0" and nothing else, or any other string that evaluates to false in Perl then an error might be reported when there wasn't one.