为什么是“最后”?称为“最后”在 Perl 中?

发布于 2024-08-13 01:14:10 字数 365 浏览 7 评论 0原文

Perl 中调用 last 而不是 C 中调用 break 的历史原因是什么?

Perl 的设计受到 C 的影响(此外还有 awk、sed 和 sh - 请参阅下面的手册页),因此不采用熟悉的 C 风格命名 Break/last 肯定有一些原因。

Perl 1.000(1987 年 12 月 18 日发布)手册页的一些历史记录:

[Perl] 结合了(无论如何,在作者看来)C、sed、awk 和 sh 的一些最佳功能,因此熟悉这些语言的人应该不会有什么困难。 (语言历史学家还会注意到 csh、Pascal 甚至 BASIC|PLUS 的一些痕迹。)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

ぃ双果 2024-08-20 01:14:10

“break”或“last”的语义是
由语言定义(在本例中
Perl),不是你做的。

为什么不将“最后”视为“这是
运行的最后一个语句
循环”。

我总是觉得很奇怪
“C”中的“继续”语句开始
下一次循环。这是
绝对是一个奇怪的用法
“继续”的概念。但这是
'C' 的语义,所以我接受它。

通过尝试映射特定的
将编程概念转化为单个
具有现有含义的英语单词
总会有某种
不匹配的奇怪现象

来源

另外,拉里·沃尔有点奇怪。你看过他的照片吗?

拉里·沃尔
(来源:wired.com

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)

素年丶 2024-08-20 01:14:10

我认为这是因为 Perl 是由语言学家而不是计算机科学家创建的。在正常的英语用法中,声明您已完成循环的最后一次传递的概念与单词“最后”(“这是最后传递”)的联系比与单词“ break”(“打破循环”?“打破跳出循环”? - 甚至不清楚“break”与退出循环有何关系)。

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

荒路情人 2024-08-20 01:14:10

当您记得“最后一个”一词不仅仅可以与立即循环控件一起使用时,它就更有意义了。您可以将其应用于上面一层或多层的标记块
它所在的块:

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

当您将其读为“如果匹配...则为最后一行”时,用英语说“最后”会更自然。

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

何必那么矫情 2024-08-20 01:14:10

您可能还需要考虑另一个原因:

Last 的作用不仅仅是循环控制。

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

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

Last 是一种通用的流控制机制,不限于循环。当然,您可以将上面的内容概括为最多运行 1 次的循环,但对我来说,没有循环就意味着“中断?我们要打破什么?”

相反,我认为“last”是“跳转到last大括号的位置”,出于这个目的,语义上更合理。

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.

淤浪 2024-08-20 01:14:10

我向达米安·康威(Damian Conway)询问了同样的问题。 Perl 6 将引入 say,它无非就是自动添加换行符的 print。我的问题是为什么不简单地使用 echo,因为这就是 echo 在 Bash(也可能在其他地方)中所做的事情。

他的回答是:echosay 长 33%。

他说得有道理。 :)

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

迷途知返 2024-08-20 01:14:10

因为它到达了循环的最后。因为拉里·沃尔是个奇怪的人。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文