Thomas Bätzler wrote:
Brian <fulls...@me.com> asked:


        foreach $line (<LOGFILE>) {
while (my $line = <$logfile>) would be a better idea than
foreach $line.
Just curious for an explanation to this. I tend to use foreach too.
Don't they both accomplish the same thing? :)

"foreach (<LOGFILE>)" means that the whole file will be read into memory and 
split into lines before the loop commences.

The second construct will just read in a line at a time, thus trading off speed 
for memory usage.

Quite the opposite. Both use buffered IO to read the file and parse the lines. The foreach has extra processing to store the contents in an array; thus taking more time. :)


--
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy:  use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to