Hi,

I want to read a file by blocks of a certain size. So, "read" is my best friend. But I 
have trouble with the eof flag.
 It seems that eof is true on the beginning (script 1).
 Putting an <IN> command up front seems to clear eof (script 2):
 Another way is *not* to use eof (script 3):

But I'd like to understand how eof really works.

Thanks

#!/usr/local/bin/perl -w
# script 1
$\="\n";
my ($k,$p,$q) = (0,0,0); my $n = 100000;
open IN, '<truc' or die 'truc not open';
print 'file info: ', -s 'truc',' chars'; # 3509313
print 'eof: ',eof;
until (eof) {$p = read IN,$_,$n; $k++};
print "$k blocks: ",($k-1)*$n+$p+$q,' chars'; # 36 3509313
__END__
file info: 3509313 chars
eof: 1
0 blocks: -100000 chars

#!/usr/local/bin/perl -w
# script 2
$\="\n";
my ($k,$p,$q) = (0,0,0); my $n = 100000;
open IN, '<truc' or die 'truc not open';
print 'file info: ', -s 'truc',' chars'; # 3509313
$q = length <IN>;
print 'eof: ',eof;
until (eof) {$p = read IN,$_,$n; $k++};
print "$k blocks: ",($k-1)*$n+$p+$q,' chars'; # 36 3509313
__END__
file info: 3509313 chars
eof: 
36 blocks: 3509313 chars

#!/usr/local/bin/perl -w
# script 3
$\="\n";
my ($k,$p,$q) = (0,0,0); my $n = 100000;
open IN, '<truc' or die 'truc not open';
print 'file info: ', -s 'truc',' chars'; # 3509313
print 'eof: ',eof;
while (read IN,$_,$n) {$p = length; $k++};
print "$k blocks: ",($k-1)*$n+$p+$q,' chars'; # 36 3509313
__END__
file info: 3509313 chars
eof: 1
36 blocks: 3509313 chars

Reply via email to