Perl 中的子例程和脚本有什么区别?

发布于 2025-01-07 16:40:30 字数 40 浏览 0 评论 0原文

我正在学习Perl,遇到一个问题。子例程和脚本之间到底有什么区别?

I am in the midst of learning Perl, and I have encountered a question. What, exactly is the difference between subroutines and scripts?

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

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

发布评论

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

评论(2

情丝乱 2025-01-14 16:40:30

脚本只是通常包含在单个文件中的(通常很短)程序的名称。它并不是一个真正的科学/技术术语,因此相当模糊——人们在讨论 3 行快速程序或 10000 行代码程序时可以指代“脚本”。

有些人将任何 Perl 程序称为“脚本” - 请参阅下面的历史原因。有些人,当他们说“Perl 脚本”而不是 Perl“程序”时,是指相对简单、相对较短的程序,通常在不使用任何子例程/类/其他代码组织方法的情况下构建。再次强调,没有标准定义。

顺便说一句,Perl 程序经常被称为“脚本”的原因是 Perl 最初是使用 Perl用于编写在 Unix shell 中执行工作的脚本,即使用 shell 脚本语言的方式。术语“脚本语言”表示用于控制应用程序的语言,在本例中为 Unix shell。

当然,从那时起,Perl 已经发展成为一种成熟的编程语言,但这个词/术语仍然存在,有时是出于惯性,有时是贬义的。


子例程(也称为过程、函数、例程、方法或子程序)是较大程序中执行特定任务并且相对独立于其余代码的一部分代码。它通常意味着包含执行需要在程序中多次完成甚至由多个程序完成的任务的代码。

子例程不是 Perl 特定的概念,尽管在极少数语言中称其为“子例程”(大多数使用术语“函数”、“方法”或“过程”)。


作为一个特殊的旁注,“方法”(在 Perl 以及其他语言中)是一种特殊类型的子例程,它与面向对象的类或该类的对象相关联。当然,它只是子例程的一个特殊情况,这一事实突出显示 - 尽管 “Modern Perl”作者 chromatic 的最深的愿望 - Perl 5 中的方法用“sub”关键字声明,相同 )


如上所述,一些人在将 Perl 程序称为“脚本”时,暗示它不包含子例程(例如,任何复杂到足以拥有子例程的内容 不再是“脚本”而是“程序”)。但这不是一个公认的或正式的定义 - 如前所述,没有什么是脚本的定义,每个人都以自己想要的方式使用这个术语。

A script is just a name for a (usually short) program usually contained in a single file. It's not really a scientific/technical term and therefore is pretty vague - people can refer to a "script" when discussing a 3-line quick program, or a 10000 lines of code program.

Some people refer to ANY Perl program as a "script" - see below for the historical reason. Some people, when they say "a Perl script" as opposed to a Perl "program", mean a relatively simple, relatively short program, frequently structured without using any subroutines/classes/other methods of code organization. Again, there's no standard definition.

As an aside, the reason why Perl programs are frequently called "scripts" is that Perl originally was used for writing scripts that perform work in Unix shell, the way shell scripting languages were used. The term "scripting language" means a language used to control an application, in this case Unix shell.

Of course, since then Perl has grown to become a full fledged programming language, but the word/term remained, sometimes used by inertia, sometimes derogatorily.


A subroutine (also known as a procedure, function, routine, method, or subprogram) is a portion of code within a larger program that performs a specific task and is relatively independent of the remaining code. It is frequently meant to contain code that performs the task which needs to be done several times in your program, or even by multiple programs.

A subroutine is NOT a Perl specific concept, though calling it "subroutine" is done in very few languages (most use the term function, method or procedure).


As a special side note, a "method" - in Perl as well as other languages - is a special type of subroutine which is associated with an object oriented class or an object of that class. The fact that it's merely a special case of a subroutine is, of course, highlighted by the fact that - despite deepest wishes by "Modern Perl" author chromatic - methods in Perl 5 are declared with "sub" keyword, same as regular subroutines.


As noted above, some people, when referring to a Perl program as a "script", imply that it does not contain subroutines (e.g. anything complicated enough to have a subroutine is no longer a "script" but a "program"). But that is not an accepted or formal definition - as stated, there is no definition of what a script is, everyone uses the term any which way they want.

灯角 2025-01-14 16:40:30

脚本通常是一个文件,其中可以包含语句和子例程。子例程是您在脚本中找到的东西。

子例程在 perlsub 手册页中有详细描述。

A script is usually a file, which can contain statements and subroutines. A subroutine is something you find within a script.

Subroutines are described in detail in the perlsub manual page.

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