On Sat Jan 29 06:17:10 2005, leo wrote: > Matt Diephouse <[EMAIL PROTECTED]> wrote: > > I already use that workaround in some of my code, but that sorta > > defeats the purpose of testing the filehandle itself. The filehandle > > should become false after it returns the last line of the file - not > > the last line plus an empty string. > > Looked again at that stuff. You are testing for EOF before the readline, > which implies that EOF should be set along with returning data. This > seems to be wrong. > > So the correct way seems to be: > > unless file goto END > LOOP: > $S0 = readline file > unless file goto END > $I0 += 1 > goto LOOP
Leo is right. Compare the equivalent Perl 5 code: my $file; open $file, @ARGV[0] or die "can't open file"; my $count = 0; while ($file) { my $string = readline $file; print "Count is $count: $string"; $count++; last if $string eq ""; } print "\nTotal count: $count\n"; which prints out: Count is 0: line 1 Count is 1: line 2 Count is 2: line 3 Count is 3: Total count: 4 There's no way the filehandle can know that it has reached the last line of the file before it reads the last line of the file. The extra "line" isn't a problem with readline, it's simply a result of the loop control structure you've chosen. Perl 5's while (<FILEHANDLE>) {...} construct isn't a simple while loop on a file handle, it's syntactic sugar for a filehandle iterator. Which reminds me that we need to either make sure that the default Iterator object can be applied to ParrotIO objects, or provide one that can (or perhaps an Iterator role). So, I'm leaving this ticket open and attached to the I/O PDD ticket. Allison