Tutorial by Examples

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>) { pri...
If you have a list in memory already, the straightforward and usually sufficient way to process it is a simple foreach loop: foreach my $item (@items) { ... } This is fine e.g. for the common case of doing some processing on $item and then writing it out to a file without keeping the data ...

Page 1 of 1