When reading a potentially large file, a while
loop has a significant memory advantage over foreach
. The following will read the file record by record (by default, "record" means "a line", as specified by $/
), assigning each one to $_
as it is read:
while(<$fh>) {
print;
}
The diamond operator does some magic here to make sure the loop only terminates at end-of-file and not e.g. on lines that contain only a "0" character.
The following loop seems to work just the same, however it evaluates the diamond operator in list context, causing the entire file to be read in one go:
foreach(<$fh>) {
print;
}
If you are operating on one record at a time anyway, this can result in a huge waste of memory and should thus be avoided.