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


in reply to Re^2: Interview Prepration
in thread Interview Prepration

That's a really thin line to walk -- thin enough that I think it's almost meaningless and definitely more confusing than useful. Is this a for loop?

foreach ( my $i = 0; $i < 10 ; $i++ ) { print "\$i is $i\n"; }

Is this a foreach loop?

for my $user (@names) { print "Found user $user\n"; }

Replies are listed 'Best First'.
Re^4: Interview Prepration
by merlyn (Sage) on Apr 04, 2005 at 18:04 UTC
    Yes, and yes. Spelled "f-o-r", pronounced "foreach". Spelled "f-o-r-e-a-c-h", pronounced "for".

    If you start handwaving "for and foreach are the same" in front of people enough, they wonder why the variable "didn't localize in my for loop... I thought for did that!".

    It really is important to distinguish the two kinds of loops, even though 90% of the time we write "for", we prounounce it "foreach".

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Actually I think the community has selected a third way to avoid this debate, whatever it actually says in Perlsyn. The way they solved this debate is they say for LIST and 3 arg for loop or c-style for loop or if you are like me the while loop that likes to crossdress and call itself a for loop.

      I agree with chromatic, your usage, while strictly correct is confusing and misleading, especially when it leads you to obvious contortions like saying

      foreach(;1;) { }
      is a "for" loop and
      print for @list;
      is a "foreach" loop.

      Hopefully youll drop this piece of pedantry as its just confusing.

      ---
      demerphq

        I don't know what's confusing about calling something what it is, especially when there's an authoritative source that most people should have already read, or will read now. In fact, I find it more confusing when people are inventing their own terms when perfectly good terms exist.

        No, I'll stand by the reference docs here, unless I hear otherwise from the man himself.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.