in reply to while or foreach?

I tried Benchmarking these, but it's just too fast to get much of a feel for what's going on inside. I'm guessing that the speed difference between the two is pretty minimal.

Nate's in the mainstream of Good Perl Taste here. while is good for loops and reading from filehandles, while foreach is good for iterating over some sort of list, whether is is the keys of a hash or the elements of an array.

If you're *really* into cool things, don't forget that you can do this. $_ (the current line) is assumed in those contexts. Neat, huh?

Replies are listed 'Best First'.
RE: Speed and Readability
by Anonymous Monk on Dec 31, 1999 at 02:20 UTC
    Even worse than simply violating the "thou shalt not violate the while(<>) idiom for file iteration", I'd bet that the foreach route will slurp the entire file into an array before starting to loop over it -- something you'd almost certainly not want to do with a large (read: many-lined) file!
RE: Speed and Readability
by BBQ (Curate) on Dec 24, 1999 at 16:51 UTC
    Good note on $_! Which actually brings me to another question (and this ought to be a poll thing!):
    What do you prefer?
      <il>Shorter code, but cryptic reading?</il> <il>Readability but "longer self-explanatory" code</il>