简体   繁体   English

为什么Perl常用于编写CGI脚本?

[英]Why is Perl commonly used for writing CGI scripts?

I plan to add a better search feature to my site, so I thought that I would write it in C and use the CGI as a means to access it. 我计划在我的网站上添加一个更好的搜索功能,所以我认为我会用C语言写它并使用CGI作为访问它的方法。 But it seems that Perl is the most popular language when it comes to CGI-based stuff. 但就基于CGI的东西而言,Perl似乎是最受欢迎的语言。 Why is that? 这是为什么? Wouldn't it be faster programmed in C or machine code? 用C或机器代码编程是不是更快?

What advantages, if any, are there to writing it in a scripting language? 如果有的话,用脚本语言编写它有什么好处?

Thanks. 谢谢。

Back in the day when CGI was becoming popular, Perl was the easiest language to use. 在CGI变得流行的那一天,Perl是最容易使用的语言。 People could pick up "baby Perl" very quickly, and since the program was a text file, they could easily upload it and pass it around. 人们可以很快拿起“宝贝Perl”,由于程序是一个文本文件,他们可以轻松上传并传递它。 Since Perl started life as a system administration language, lots of servers already had it installed. 由于Perl起初是一种系统管理语言,许多服务器已经安装了它。 When it came time to make a CGI script on some hosting service, Perl was most likely already there. 当在某些托管服务上制作CGI脚本时,Perl很可能已经存在。 Not only that, a Perl script is pretty much the same on any platform, so what you wrote locally most likely worked exactly the same on a different machine. 不仅如此,Perl脚本在任何平台上几乎都是相同的,因此您在本地编写的内容很可能在不同的机器上完全相同。

It was faster to program for "accidental programmers" in the big scheme of things because they had less to learn before they could make a useful program; 在大型计划中为“偶然程序员”编程更快,因为他们在制作有用的程序之前学习的东西较少; they could start with nothing and have a Perl program running in an hour, even if they were just cargo-culting it. 他们可以从零开始,并在一小时内运行一个Perl程序,即使它们只是货物结果。 They didn't have to worry about all the things that come with writing and compiling a C program, then transferring it to another host (which might be a different platform). 他们不必担心编写和编译C程序所带来的所有事情,然后将其转移到另一个主机(可能是一个不同的平台)。

Perl got a quick foothold, and you still see the effects of that today. Perl有一个快速的立足点,你仍然可以看到今天的影响。 If Perl had to start from scratch today, I don't think it would necessarily win out over anything else. 如果Perl今天必须从头开始,我认为它不一定能胜过其他任何东西。 PHP has certainly taken over the low-end, quick-startup crowd (and for most of them, it's probably the right tool at first). PHP肯定已经接管了低端,快速启动的人群(对于大多数人来说,它可能是最初的正确工具)。

It didn't hurt that Perl had a lot of text processing features, either. Perl也有很多文本处理功能,这并没有受到伤害。 Some people talk about CPAN, but that barely existed when Perl started to get noticed for CGI programming. 有些人谈论CPAN,但是当Perl开始注意到CGI编程时,这几乎不存在。

However, Perl's not as special for CGI programming as it used to be. 但是,Perl并不像过去那样特别适合CGI编程。 It still does all of the great things it always has, but now various other languages have caught up in both functionality, availability, and community awareness. 它仍然可以完成它始终拥有的所有优秀功能,但现在各种其他语言都在兼顾功能,可用性和社区意识。

I started programming CGI stuff in 1994, and I still see how amazingly and mind-boggling hard most frameworks make it. 我在1994年开始编写CGI的东西,我仍然看到大多数框架都是如此令人难以置信且令人难以置信。 I really wish we had Seaside back then because you never even know about all the stupid things other frameworks make you do. 我真的希望我们之前有Seaside,因为你甚至都不知道其他框架让你做的所有愚蠢的事情。 How much better the world would have been if we'd all learned Smalltalk instead. 如果我们都学会了Smalltalk,世界会变得多好多少。 :) :)

Security, for one thing. 安全,一方面。 If you write in C, you have to be very careful to make sure all your string handling is correct so you don't introduce buffer overflows, etc. In any decent scripting language, someone else has already done that for you. 如果你用C语言编写,你必须非常小心,以确保你的所有字符串处理都是正确的,这样你就不会引入缓冲区溢出等。在任何体面的脚本语言中,其他人已经为你做过。 You may be able to have other security holes, but unless there's a bug in the runtime or an extension module, you won't have a buffer overflow. 您可能有其他安全漏洞,但除非运行时或扩展模块中存在错误,否则您将不会有缓冲区溢出。 This benefit is not limited to scripting languages; 这种好处不仅限于脚本语言; compiled languages such as Java and C# provide it as well, and it is obtainable (albeit frequently more difficultly) in C++ with std::string and C with a good string library. Java和C#等编译语言也提供了它,并且可以在C ++中使用std::string和C以及良好的字符串库获得(尽管通常更加困难)。

Securitywise, Perl has another useful feature not seen in many other systems: "taint" mode. 在Securitywise中,Perl还有另一个在许多其他系统中看不到的有用功能:“污点”模式。 This keeps you from blindly passing user input to other systems as part of a database query, command line, etc. This is an excellent benefit when writing CGI scripts, as your script will die cleanly before it passes uninspected user input off to the shell for execution. 这使您不会盲目地将用户输入作为数据库查询,命令行等的一部分传递给其他系统。这在编写CGI脚本时是一个很好的好处,因为在将未经检查的用户输入传递给shell之前,脚本会干净地死掉。执行。 Taint mode is not perfect, as the untainting process depends on the programmer doing things correctly, but it at least helps catch code paths you missed. 污点模式并不完美,因为无污染的过程取决于程序员正确地执行操作,但它至少有助于捕获您错过的代码路径。

Also, at this point, Perl has been used for a long time for CGI scripting, so there is a large body of libraries, frameworks, etc. already in existence to make writing new scripts easier. 此外,在这一点上,Perl已经被用于CGI脚本很长一段时间了,因此已经存在大量的库,框架等,以使编写新脚本更容易。 Plus CPAN has code to do just about anything. 加上CPAN可以执行任何操作。

OK, the rest of the answers gave pretty good objective reasons. 好的,其余答案给出了很好的客观原因。 Just for completeness, here's a subjective evaluation to give it some color: 为了完整起见,这是一个主观评价,给它一些颜色:

I wrote: 我写:

  • CGI software in pure C (for money, professionally). 纯C中的CGI软件(专业用于金钱)。 This included creation of the entire CGI library (that was in the days before CGI libraries were available). 这包括创建整个CGI库(就在CGI库可用之前的几天)。
  • CGI libraries of my own in Perl 我在Perl中的CGI库
  • CGI stuff in Perl using CPAN. 使用CPAN在Perl中使用CGI。

Based on those experiences, the pure C one gave the most satisfaction as far as "Look at this cool technical achievement I made" angle. 基于这些经验,纯粹的C对于“看看我所做的这个很酷的技术成就”的角度给予了最大的满足。 Especially as that was in the days when CGI was brand shiny new and static HTML was the main content everywhere. 特别是当CGI是品牌闪亮的时代,新的静态HTML是各地的主要内容。

The roll-my-own Perl CGI was a lot easier technically than C one due to all the objective reasons listed in other answers. 由于其他答案中列出的所有客观原因,我自己的Perl CGI在技术上比C更容易。

And the CPAN Perl projects were the only ones that provided fairly decent delivery turnaround time and allowed me to concentrate on building the business logic as opposed to the plumbing. 而CPAN Perl项目是唯一能够提供相当不错的交付周转时间的项目,并使我能够专注于构建业务逻辑而不是管道。

The biggest advantage of using Perl is CPAN . 使用Perl的最大优势是CPAN

In addition to the answers mentioned already, for basic web applications network transfer speed is a more common bottleneck than language choice. 除了已经提到的答案之外,对于基本的Web应用程序,网络传输速度是比语言选择更常见的瓶颈。 It is usually easier to write web applications in Perl than C, so a small difference in runtime speed is not worth the extra effort needed to create the application. 在Perl中编写Web应用程序通常比C更容易,因此运行时速度的微小差异不值得创建应用程序所需的额外工作。 C is in fact sometimes used for certain parts of very computation-intensive web applications. 实际上,C有时用于计算密集型Web应用程序的某些部分。

String manipulation, often a large part of web development, is rather painful and error-prone in C, in parts due to the lack of automatic memory management. 由于缺乏自动内存管理,字符串操作(通常是Web开发的很大一部分)在C中非常痛苦并且容易出错。 Keep in mind that often, script execution time isn't the bottleneck or can be circumvented by proper caching mechanisms. 请记住,脚本执行时间通常不是瓶颈或可以通过适当的缓存机制来规避。 In many cases, it's a good idea to choose a language which maximizes developer productivity instead of unnecessarily sacrificing development time for performance gains which will go unnoticed by the user of the site. 在许多情况下,最好选择一种能够最大限度地提高开发人员生产力的语言,而不是不必要地牺牲开发时间来提高性能,而这些开发时间将不会被网站用户忽视。

This general principle does not, however, fully apply in your case, as a search engine might well benefit from optimized low-level code. 但是,这个一般原则并不完全适用于您的情况,因为搜索引擎可能会从优化的低级代码中受益。 This doesn't mean that you'll have to do everything in C, though: the PHP interpreter has been known to be painfully slow, but as most library functions are implemented in C, you can get away with it. 这并不意味着你必须用C语言做所有事情:已知PHP解释器的速度很慢,但由于大多数库函数都是用C语言实现的,所以你可以侥幸逃脱。 I'd recommend to write the app in the high-level language of your choice, and only re-implement the parts in C which have been identified as botlenecks. 我建议用您选择的高级语言编写应用程序,并且只重新实现C中已被识别为botlenecks的部分。

I think the benefit for using a scripting language, is most people are much more productive using a higher level dynamic language than they are using C. 我认为使用脚本语言的好处是大多数人使用更高级别的动态语言比使用C语言更有效率。

A lot of people seem to worry about speed, but in reality it's normally fine...and if it does become an issue most scripting languages have a extension mechanism where you can write modules in C and still use them in the higher level scripting langauge (Like XS in perl, or pythons c-api) 很多人似乎都担心速度,但实际上它通常很好......如果它确实成为一个问题,大多数脚本语言都有一个扩展机制,你可以在C中编写模块,并仍然在更高级别的脚本语言中使用它们(像perl中的XS,或pythons c-api)

At the time CGI was invented back in the early days of the web, it was the only way do any kind of dynamic processing of web requests, such as responding to forms being submitted or clicking on an imagemap. 当时CGI是在网络发展初期发明的,它是对Web请求进行任何动态处理的唯一方式,例如响应提交的表单或点击图像映射。 The web server software itself could deliver only static content, so external programs were needed to handle the interactive stuff. Web服务器软件本身只能提供静态内容,因此需要外部程序来处理交互式内容。

The first webmasters were probably also system administrators who were often well-versed in Perl. 第一批网站管理员可能也是系统管理员,他们通常精通Perl。 I remember the first NCSA httpd servers came with sample CGI programs written in Perl, C, and shell. 我记得第一个NCSA httpd服务器带有用Perl,C和shell编写的示例CGI程序。 The shell scripts were discarded pretty quickly because they were insecure and not good for anything other than really short CGI programs. shell脚本很快就被丢弃了,因为它们不安全,除了非常简短的CGI程序之外什么都不好。 The C programs worked fine, but Perl was so much more convenient. C程序运行良好,但Perl更方便。

My guess is Perl took off as the de facto standard language to use with CGI for several reasons: 我的猜测是Perl作为与CGI一起使用的事实上的标准语言起飞有几个原因:

  • System administrators were familiar with it. 系统管理员对此很熟悉。
  • Fast, secure libraries became widely available; 快速,安全的库变得广泛可用; the CGI.pm module is shipped standard with Perl. CGI.pm模块标配Perl。
  • Perl offered a good compromise between speed and ease of development. Perl在速度和易开发性之间提供了良好的折衷。

There's no reason why Perl has to be used; 没有必要使用Perl的原因; any language that can work with Unix environment variables is suitable. 任何可以使用Unix环境变量的语言都是合适的。

That said, CGI has fallen out of favor because it's very slow relative to the languages that run in the web server's address space, such as PHP. 也就是说,CGI已经失宠,因为它相对于在Web服务器的地址空间中运行的语言(例如PHP)来说非常慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM