No correction in fact. I knew they were aliases all
along. But I find it clearer to indicate my thoughts by
saying "for" for the classic C loop and "foreach" for
iterating over a list. Particularly when I am trying to
tell C programmers why they should use foreach style
loops rather than for loops. ;-) | [reply] |
I was correcting you because you said that foreach is faster, etc. I think it would have been better to say that foreach is Perl Style and easier to understand, and better to use for those reasons...
just my $0.02
| [reply] |
Read what I wrote exactly as I wrote it the first time.
I said that foreach loops are faster than classic C-style
loops. Yes, you can write a classic C-style loop using
foreach. But nobody thinks that way.
And indeed iterating directly over a list is
*significantly* faster, less error prone, etc, than doing
an explicit loop over the indices, looking up array values
inside the loop.
That is not disinformation, that is simple fact and I have
found that that factual information carries a lot more
weight with C programmers starting out with Perl than saying,
"Well the standard Perl idiom is..."
Have you ever said, "This is Perl Style and easier to
understand." and been told, "Well I am a C programmer, I
have been using for loops for 15 years and they are easier
for me to understand because I am used to them."? You
won't win that argument...
| [reply] |
Why confuse them though? I would think that telling them up front
that they are the same would be easier for them in the long run.
Bad enough that some perl programmers still think that they are
somehow different. I never use "foreach" myself, but that's because I am
exercising the first Perl Virtue. :)
| [reply] |
What makes you think I confuse them?
I show them the idiom as I write it and encourage them to
write it the same way. If the question of whether they
are the same comes up, I just say, "Yeah, they are aliases
for each other, but I like to write them differently
because I think it is clearer that way." I have yet to
see anyone find that a huge hurdle. I have seen getting
used to the different type of looping be a hurdle.
As for telling people things up front, do you think that
up front I tell people that blocks are really hashes, REs
are really done by recursion, or many other minor details
of the language? Perl is a big language, with many
corners, and you shouldn't present it all at once.
| [reply] |