为什么是“最后”?称为“最后”在 Perl 中?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
来源
另外,拉里·沃尔有点奇怪。你看过他的照片吗?
(来源:wired.com)
Source
Plus, Larry Wall is kinda weird. Have you seen his picture?
(source: wired.com)
我认为这是因为 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).
当您记得“最后一个”一词不仅仅可以与立即循环控件一起使用时,它就更有意义了。您可以将其应用于上面一层或多层的标记块
它所在的块:
当您将其读为“如果匹配...则为最后一行”时,用英语说“最后”会更自然。
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:
It reads more naturally to say "last" in english when you read it as "last line if it matches ...".
您可能还需要考虑另一个原因:
Last 的作用不仅仅是循环控制。
Last 是一种通用的流控制机制,不限于循环。当然,您可以将上面的内容概括为最多运行 1 次的循环,但对我来说,没有循环就意味着“中断?我们要打破什么?”
相反,我认为“
last
”是“跳转到last大括号的位置”,出于这个目的,语义上更合理。Theres an additional reason you might want to consider:
Last does more than just loop control.
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.我向达米安·康威(Damian Conway)询问了同样的问题。 Perl 6 将引入
say
,它无非就是自动添加换行符的print
。我的问题是为什么不简单地使用echo
,因为这就是echo
在 Bash(也可能在其他地方)中所做的事情。他的回答是:
echo
比say
长 33%。他说得有道理。 :)
I was asking the same question to Damian Conway about
say
. Perl 6 will introducesay
, which is nothing more thanprint
that automatically adds a newline. My question was why not simply useecho
, because this is whatecho
does in Bash (and probably elsewhere).His answer was:
echo
is 33% longer thansay
.He has a point there. :)
因为它到达了循环的最后。因为拉里·沃尔是个奇怪的人。
Because it goes to the last of the loop. And because Larry Wall was a weird guy.