[perl #61224] .eof returns false if last read call read the last byte of the file, but not beyond

Allison Randal via RT parrotbug-followup at parrotcode.org
Fri Dec 12 01:07:46 UTC 2008


On Tue Dec 09 06:36:51 2008, jonathan at jnthn.net wrote:
> Hi,
> 
> It seems that the .eof() method on file handles can sometimes return 
> true even if there is nothing more to read.

Yes, a read that returns 0 bytes is what sets the EOF flag. (True of any
kind of read other than one that requests 0 bytes.)

This behavior is the same as the old implementation.

> This occurs when you have 
> read upto the last byte of a file (e.g. when a readline reads up to the 
> end of a newline, and that newline is the last thing in the file), but 
> not beyond (which seems to be what causes the EOF flag to be set). I'm 
> thinking this is the wrong behaviour?

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').

At the end of the day, it's a cost/benefit question. Individual
languages can implement the 1 character lookahead with the current I/O
system if they need it. Will all languages (or even most languages) want
the 1 character lookahead?

Allison


More information about the parrot-dev mailing list