Question

What is the historical reason to that last is called that in Perl rather than break as it is called in C?

The design of Perl was influenced by C (in addition to awk, sed and sh - see man page below), so there must have been some reasoning behind not going with the familiar C-style naming of break/last.

A bit of history from the Perl 1.000 (released 18 December, 1987) man page:

[Perl] combines (in the author's opinion, anyway) some of the best features of C, sed, awk, and sh, so people familiar with those languages should have little difficulty with it. (Language historians will also note some vestiges of csh, Pascal, and even BASIC|PLUS.)

Was it helpful?

Solution

The semantics of 'break' or 'last' are defined by the language (in this case Perl), not by you.

Why not think of 'last' as "this is the last statement to run for the loop".

It's always struck me as odd that the 'continue' statement in 'C' starts the next pass of a loop. This is definitely a strange use of the concept of "continue". But it is the semantics of 'C', so I accept it.

By trying to map particular programming concepts into single English words with existing meaning there is always going to be some sort of mismatching oddity

Source

Plus, Larry Wall is kinda weird. Have you seen his picture?

Larry Wall
(source: wired.com)

OTHER TIPS

I expect that this is because Perl was created by a linguist, not a computer scientist. In normal English usage, the concept of declaring that you have completed your final pass through a loop is more strongly connected to the word "last" ("this is the last pass") than to the word "break" ("break the loop"? "break out of the loop"? - it's not even clear how "break" is intended to relate to exiting the loop).

The term 'last' makes more sense when you remember that you can use it with more than just the immediate looping control. You can apply it to labeled blocks one or more levels above the block it is in:

 LINE: while( <> ) {
      WORD: foreach ( split ) {
           last LINE if /^__END__\z/;
           ...
           }
      }

It reads more naturally to say "last" in english when you read it as "last line if it matches ...".

Theres an additional reason you might want to consider:

Last does more than just loop control.

sub hello {
  my ( $arg ) = @_; 

  scope: { 
      foo(); 
      bar(); 
      last if $arg > 4; 
      baz(); 
      quux(); 
  }
}

Last as such is a general flow control mechanism not limited to loops. While of course, you can generalise the above as a loop that runs at most 1 times, the absence of a loop to me indicates "Break? What are we breaking out of?"

Instead, I think of "last" as "Jump to the position of the last brace", which is for this purpose, more semantically sensible.

I was asking the same question to Damian Conway about say. Perl 6 will introduce say, which is nothing more than print that automatically adds a newline. My question was why not simply use echo, because this is what echo does in Bash (and probably elsewhere).

His answer was: echo is 33% longer than say.

He has a point there. :)

Because it goes to the last of the loop. And because Larry Wall was a weird guy.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top