in reply to The last command is like the break statement in C

The "loop flow modification" keywords in Perl are "redo", "next", and "last" because they allow you to "redo" the loop for the same input, skip to the "next" input, or make this the "last" input. "break" doesn't fit the pattern. You'll also note that Perl's == vs. eq is very much like /bin/test's = vs. -eq except that in Perl, eq is for strings while with /bin/test -eq is for numbers. But it makes more sense to use the "stringy" eq for strings.

Larry took ideas from a lot of languages and tools in writing Perl. But he also improved on many of these ideas. "break" isn't a very descriptive name for a keyword. C doesn't have "redo" nor "next" loop flow modification. In this case Perl chose to be consistant with itself over being consistant with C. This is a good thing IMHO. (:

        - tye (but my friends call me "Tye")
  • Comment on (tye)Re: The last command is like the break statement in C

Replies are listed 'Best First'.
(jcwren) RE: (tye)Re: The last command is like the break statement in C
by jcwren (Prior) on Sep 27, 2000 at 18:46 UTC
    'C' most certainly does have 'next' loop flow modification! It's called 'continue'.
    #include <stdio.h> main (int argc, char **argv) { int i; for (i = 0; i < 10; i++) { if (i == 5) continue; printf ("i = %d\n", i); } } [jcw@linux jcw]$ gcc t.c [jcw@linux jcw]$ ./a.out i = 0 i = 1 i = 2 i = 3 i = 4 i = 6 i = 7 i = 8 i = 9 [jcw@linux jcw]$
    --Chris

    e-mail jcwren

      Thanks. I forgot about "continue" in C, which I don't recall ever using. But this strengthens the story...

      Now that Perl has "redo" and "next", both of these "continue" the loop processing so they really both had to be named something other than "continue". Plus, Perl lets you write for(a;b;c) as a;while(b){}continue{c} (I hope I got that right). It makes sense to use "continue" here since that block is what is executed whenever you decide to continue looping. So having "continue" introduce that block as well as be a loop flow modifier would probably lead to more parsing confusion. So we had to rename "continue", we might as well rename "break" as well.

      I also wondered if "break" was being saved for when Perl adds a real "case" statement. I hate this in C:

      while( bool ) { switch( input ) { case END: break; } }
      What I really want to do is break out of the while loop. The work-arounds for this overloading of "break" in C are usually a bit ugly (depending on the specifics). So if Perl uses "break" for "case" statements, then I could code "last" above and get what I often want very easily.

              - tye (but my friends call me "Tye")
        Just to confuse C programmers, Perl has a continue keyword that is used in looping. If after your main loop block you have a block labelled continue, it will be executed between iterations. For giggles and grins, within a continue block the word next will restart the continue block. Combining continue, redo, and next properly you can write a triple-loop in one loop. I have no idea why someone would want to do this, but you can.