[perl #61224] .eof returns false if last read call read the last byte of the file, but not beyond
Joshua Juran
jjuran at gmail.com
Fri Dec 12 08:46:33 UTC 2008
On Dec 11, 2008, at 5:07 PM, Allison Randal via RT wrote:
> The way to check if the byte after the last requested byte is the
> end of
> the file is to read ahead. Perl (at least 5.10) does this by actually
> reading the next character and then putting it back with 'ungetc'. Not
> the best solution. Any read ahead can be a bit expensive. I
> experimented
> with a quick patch to use 'peek' in the test for EOF in Parrot,
> just to
> see what would happen... it broke a large quantity of code (probably
> because all the code is expecting the old behavior of the EOF test, or
> possibly a bug in 'peek').
If I'm understanding correctly, unrequested read-ahead is an error.
The problem is that you can't put the toothpaste back into the tube,
so to speak. Continuing with this analogy, calling ungetc() is like
putting the extra toothpaste in a paper cup for later. If I'm the
next person to brush my teeth, then sure, I'll scrape the toothpaste
out of the cup first before I get more from the tube, but any
hypothetical roommates would regard the cup as personal to me and
ignore it, going straight for the tube.
Toothpaste is fungible, though, and it doesn't matter in what order
it's used, whereas the same is not true of streamed bytes. If
multiple processes are sharing a file descriptor and coordinating
reads from it, any non-undoable read-ahead* will break the protocol.
* Read-ahead could be undone via lseek() for files, or be done non-
destructively with recv( ..., MSG_PEEK ) for sockets.
Josh
More information about the parrot-dev
mailing list