On Mar 25, 4:25 pm, Saurabh Jain <hundredr...@gmail.com> wrote:
> Hi,
> Is there any difference in reading a file using a while or a
> foreach in perl?
The foreach version seems to first read the whole of the file into an
array, and then go through it line by line:
#!/usr/bin/perl
#use warnings;
use strict;
open (handle,"testangleop.pl") or die "$0 Could not open $!";
my $line = <handle>; #read a line till \n or eof
print "0 line $line";
foreach (<handle>) { # Not as expected
#while(<handle>){ # Works as expected
print $_;
$line =<handle>; #read a line till \n or eof
print "1 in side $line";
$line =<handle>; #read a line till \n or eof
print "2 in side $line";
$line =<handle>; #read a line till \n or eof
print "3 in side $line";
}
The while seems to increment through the loop.
See also
http://www.unix.org.ua/orelly/perl/prog3/ch02_11.htm