http://www.perlmonks.org?node_id=743644

mattford63 has asked for the wisdom of the Perl Monks concerning the following question:

I read with interest the post, here. It discusses the scope of the indexing variable in "for" and "foreach" loops. At a working level of understanding, the indexing variable is localised to the loop. It says in the man page that "foreach" is a synonym for "for".

However, "for" is not always a synonym for "foreach". Care must be used if one is to say it's a "misspelling" or they can be used "interchangeably". "For" looks to behave differently depending on it's context: c-style vs. (er) foreach-style. If one uses c-style "for" loops you don't always get localised indexing variables as you seem to in "foreach" loops.

UPDATE: the above paragraph is wrong - "for" and "foreach" can be used interchangeably. The key point for me is that "for" is overloaded and behaves differently depending on the form of the arguments. ikegami's response sums it all up nicely in my opinion. Ironically "foreach" which is supposed to (and does) aid the programmer by letting him know explicitly what form of "for" is being used, tricked me into thinking about it in a weird way.

Anyhow, I'm interested in why Perl has the two styles of "for" constructs: convenience to the programmer seems the likely answer? I certainly appreciate the "foreach" loop. I'm assuming the "foreach" loop came after the "c-style for" loop - is this the correct Perl history? And if so, why was "foreach" implemented in a way such that the indexing variable is localised. Was this an ease of implementation decision or something else?