When you use readline
(aka <$fh>
), you read the next line after where you left off. You left off at the end of the file.
Sure, you might be able to use seek
to rewind the file handle (assuming it's not a pipe), but why would you want to read from the file again? You already have the whole thing in memory, and it's already decoded too! Just split it into lines.
my $file_contents; { local $/; $file_contents = <$file_handle>; }
utf8::decode($file_contents)
or die 'Not a valid UTF-8 file';
for (split /^/m, $file_contents, -1) {
chomp;
...
}
Or since you're chomping anyway,
for (split /\n/, $file_contents) {
...
}
I avoided do
as it causes an extra copy of the file to be created in memory.