I want to print the first 3 lines of a file. This works:
my $f = "/etc/fstab";
open ( my $fh, "<", $f );
while ( <$fh> ) {
print if (1..3) ;
}
But if I print everything, rewind the file to the beginning, and print the first three lines again, it does not work:
my $f = "/etc/fstab";
open ( my $fh, "<", $f );
while ( <$fh> ) {
chomp;
print "$_\n";
}
print "=" x 78, "\n\n";
print "after 1st while loop\n";
seek( $fh, 0, 0);
while ( <$fh> ) {
print if (1..3) ;
}
using warnings, strict, autodie (implicit in the code).
I can solve it using a counter inside the latest while loop, but I am just curious why it does not work as expected.
Thanks for any insights.