views:

572

answers:

6

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.)

+5  A: 

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

Paul Tomblin
He stopped being weird?
DVK
Well, I meant he was being weird when he named it, but point well taken.
Paul Tomblin
Why was he being weird when he named an operator after a word that normal people would use to describe it? It's the computer geeks who think we should think like microprocessors who are weird.
brian d foy
@brian: Good point. However, if it ain't broke - don't fix it!
0A0D
@surfrbum that begs the question that it wasn't broke. 'break' doesn't fit in with next or redo. I know you are just trying to be glib, but that doesn't help thing.
brian d foy
"last" to me says "do the last iteration of the loop", not "break out of the loop entirely". That's what's weird about it.
Paul Tomblin
@brian: Hey man, that's cool. To each his own. It makes sense to most of us to be 'break' and not 'last' but we could probably debate it ad infinitum
0A0D
@surfrman: break would never make sense to you unless you learned it from a previous programming language. "Last" makes perfect sense to people who haven't been infected.
brian d foy
@brian: If you say so. Have a nice day.
0A0D
Okay, let's take a break. At last.
Leonardo Herrera
@Leonardo: Took me a minute, but I got it now! ;)
0A0D
@Paul - To me, "last" says "that was the last time we did this loop." Perhaps that's because Perl was my first language and I'm just used to it's funky loop control.
Chris Lutz
@brian d foy - don't bother! it's like an infectious disease of programming. for somereason, i often find that anything that doesn't fit into a "C mindset" is considered weird ... (just how often do you use the phrase "break free" in afk life: 1. when breaking out of jail, 2. ... ?)
ldigas
@ldigas: Words in English have multiple meanings. Just because the most common use of a word isn't the meaning used in a programming language doesn't mean it doesn't make sense. What about 'for'? Programming Languages are not natural languages. 'break' meaning 'break out of the current block' (break is not only used for loops in C) is perfectly logical. You break out of jail, you break free from problems, you take a break from work, you break away from the pack... The meaning is perfectly clear. It may not be the most obvious choice for that verb in C, but it is not ambiguous or confusing.
Mr. Shiny and New
"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"
0A0D
+30  A: 

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

0A0D
+1 for the photo. That's answer enough for me ;)
ThisSuitIsBlackNot
I thought they'd burned all the pictures of him in other than Hawaiian shirts.
ysth
Larry's photo is weird ? In comparison with these guys ?http://upload.wikimedia.org/wikipedia/commons/3/36/Ken_n_dennis.jpg
ldigas
@Idigas: Ok, you got me there!
0A0D
+17  A: 

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).

Dave Sherohman
From Break free?
larelogio
"Perl was created by a linguist" is that why it looks like noise?
LiraNuna
Can we please skip the language bashing? I don't like syntactically significant whitespace, so I don't use Python - but I also don't go around looking for opportunities to tell everyone that Python sucks. If you don't like sigils (my best guess at what you mean by "looks like line noise"), then that's perfectly fine. Don't use Perl, PHP, Ruby, or any other language which uses them. But do you really have to go around the net making off-topic comments about your dislike for them?
Dave Sherohman
@Dave - don't bother. I actually like the language bashing - it's like in real life - only shows who feels threatened by who.
ldigas
@LiraNuna - because it was made with intention of being hard to learn, and easy to use. After all, you need only learn it once.
ldigas
@larelogio - "make a getaway", with the break keyword being replaced by "getaway" ? ;-)
ldigas
+10  A: 

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.

Kent Fredric
In this case, actually I think "break" does make sense - we're breaking out of the block, to get to whatever comes next, in the same way that we break out of a loop to get to whatever comes next. It's just that C's syntax can't usefully support this use of last/break, because of how if statements work and the absence of `break <label>` in C.
Steve Jessop
+14  A: 

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 ...".

brian d foy
You see, that's what I consider weird about it. "last LINE" doesn't go to LINE for one last time, it goes to the end brace of whatever control LINE is labeling. It's going somewhere that has a very tenuous connection to the label it mentions.
Paul Tomblin
Well, I see it differently. It says to me "The thing you're working on is the last one". It's just like in poker if I say "this is my last hand"; it doesn't mean that I'm going to play one more.
brian d foy
@Paul: I think the understand there has to come from your existing programming experience. I don't know of any languages that have a statement that says "Okay, exit the current iteration, then start one more iteration, but stop after that." So I would not expect "last" to do that. Given the choice between something like "continue" and something like "break", it's more obvious what Perl's "next" and "last" must mean.
Adam Bellaire
@Adam: I agree with continue vs next, but break to me means 'break out of the loop'. last seems to have multiple meanings.
0A0D
+5  A: 

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. :)

Pascal
I think this explains why Perl is used as a golf language.
LiraNuna
You can use say with Perl 5.10 too. `use feature qw'say'` or `use feature ':5.10` or `use 5.010` or `use 5.10.1` ( `use 5.10.0` gives a warning on Perl 5.10.0 )
Brad Gilbert