I agree,
while(1) loops can be bad. I'm fond of doing something similar to what you are doing instead of
while(1). I have spent much more time than I care to remember debugging code that was due to poor
while(1) implementations.
I have a few minor nits to pick with your example.
- You don't close any of your file handles. Why keep em around once you are done? I would inject a close right before your delete
- Intead of repeatedly checking every file in the list you are processing, why not just check the list of files currently open? The foreach my $file (@files) loop is easy to swap out with a foreach my $fh (keys %fh)
The change would look something like this:
while (keys %fh) {
foreach my $fh (keys %fh) {
if (defined(my $line = <$fh>)) {
chomp $line;
print $line;
} else {
close($fh);
delete($fh{$fh});
}
}
}