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

I was reading the perlfunc manpage and ran across that stmt. The first thing that came to my mind is: "if it is so similar, why wasn't it called break?"

I remember how I always wanted to put the word break in my Perl loops and had to stop myself --- I still do.

Can anyone think of a good reason why it is called last instead of break?

  • Comment on The last command is like the break statement in C

Replies are listed 'Best First'.
Re: The last command is like the break statement in C
by jptxs (Curate) on Sep 27, 2000 at 18:05 UTC

    Well, Perl was made by a linguist. It strikes me that 'break' is very machine oriented where 'last' is very human oriented. The computer knows that when it's looping it is in a state to be 'broken', but when a person (imagine a real newbie here) comes along and reads your code, they will try and read it through like a story. "while this is true do this. Unless you see that, which will be that last time you do this." flows very well.

    There may be better reasons than this. I am not a guts guy and this may have to do with the guts. But this seems like a very good reason to me - having just gotten through the first half of Camel ed3.

    -- I'm a solipsist, and so is everyone else. (think about it)

(tye)Re: The last command is like the break statement in C
by tye (Sage) on Sep 27, 2000 at 18:40 UTC

    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")
      '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")