Thank you Ronald. Your explanations bring much clarification.

On Thu, 6 Feb 2003 10:00:16 -0500,  Ronald J Kimball wrote:

>  An eof without an argument uses the last file read as argument.

>You have not read from any files at this point in your script, so calling 
>eof without an argument is not useful.  Perhaps you meant to call eof(IN) 
>instead.

Quite right. I thought opening the file was enough. Indeed, eof(IN) is the correct 
thing.

>perldoc -f eof:
>And perldoc -f read:

Where did you get this ? I have tried on a unix server; it doesn't know the perldoc 
command. Where is it located ?

>So, you could write your loop like this instead:

>while ($p = read IN, $_, $n) {
>  $k++;
>}

When exiting this loop, $p is always zero.

>or perhaps:

>while ($p = read(IN, $_, $n) and $p == $n) {
>  $k++;
>}

This one exits with $p = number of bytes actually read.

>But, instead of read(), you can actually read blocks with <>, by setting $/
> to a reference to an integer:

>$/ = \100000;

>while (<IN>) {
>  $k++;
>}

That's neat. I didn't know of this feature. I tried it several times, but 
unfortunately, I got "out of memory" on each run. More disturbing is the fact that it 
happened as well with values of 10000 and 1000.

The file is 3509313 bytes, bigger than my MacPerl can swallow. However, I had no 
trouble with 100000 byte blocks and the "read" command. I even tried 1M blocks without 
harm. Does this mean that the reading or buffering process is quite different when 
using <> ? Or is it MacPerl 5.20r4, which I'm still using ?

Best regards

As an update, the trial script is now:

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

Reply via email to