4
votes

I am learning Perl and trying to understand the difference between Perl and CGI. I found some definition from this website which says that,

"It is possible to set up the HTTP server so that whenever a file in a certain directory is requested that file is not sent back; instead it is executed as a program, and whatever that program outputs is sent back for your browser to display. This function is called the Common Gateway Interface or CGI and the programs are called CGI scripts."

So my question is, not only CGI script will be sent as a executed file, all other server script languages will be sent as executed file from the HTTP server.So, what is the main purpose of CGI?

I eagerly want to know the answer since I am really very confused. This question might be stupid one but I need to know the answer.

For example:

#!/usr/bin/perl

print "Content-type:text/html\r\n\r\n";
print '<html>';
print '<head>';
print '<title>Hello Word - First CGI Program</title>';
print '</head>';
print '<body>';
print '<h2>Hello Word! This is my first CGI program</h2>';
print '</body>';
print '</html>';

1;

If I save it in both .pl and .cgi extension and run both the programs, I get the same output. Then what would be the difference between cgi script and perl ?.

2
in short: you can use any programming language for CGI. Perl is one of programming language.jm666
The webserver can run anything executable and return the results to the browser. Perl was (and still is) a common choice. But just for a laugh, I've written a 'CGI script' in C.Sobrique
@Sobrique "But just for a laugh, I've written a 'CGI script' in C" what do you mean by that. I work on CGI programs in C for work. Is there any particular issue in using C, or does perl offer some advantages.Ghos3t
I find C is a poor choice when it comes to the type of text-processing intensive operations that most CGI scripts are doing. Usually it's a cgi script, because it's done in a scripting language - it's quite rare that you've got something that's quite as tightly performance coupled as to need C, but also ok to serve up via web.Sobrique

2 Answers

4
votes

CGI is a standard method or interface - i.e. the "Common Gateway Interface" - for applications to communicate with web servers and dynamically generate web pages for client requests.

The fact that your application might have a .cgi extension depends mainly on your webserver configuration. The application could have a .php or .pl extension and be configured to run with your web server using CGI methods. Though .cgi somewhat obfuscates the language that the script is written in, for more than a decade that language was most often perl. In fact perl included CGI library to make it easy to write CGI applications in perl. If you are running the application from the command line rather than through your web server then the interpreter will appear inside your *.cgi script and in the case of a perl CGI application would be something like: #!/usr/local/bin/perl. Because of the relative simplicity of CGI, CGI applications have been written in many many languages - even as $SHELL scripts.

CGI has largely been displaced by in process approaches or full HTTP server application stacks written in specific languages (sometimes running "behind" a proxy server or "inside" a web server process), mostly because these approaches offer improved security and performance. In the perl world, application frameworks like Plack are part of this more modern approach to dynamic web applications. Plack even includes Plack::App::WrapCGI which can "update" your CGI applications to run inside the Plack framework.

Hope that helps.

3
votes

The relationship between Perl and CGI is often confused, mostly because they are frequently seen together. This occurs so often, in fact, that the term “Perl CGI” became commonplace. In reality, any number of programming languages can be used with the Common Gateway Interface. Additionally, Perl is run through cgi-bin, suggesting a relationship. If that wasnt enough, the file extension is usually .cgi also.

CGI is a standard that provides an interface between a webserver, such as Apache, and clients through a (CGI) script, which can be written in any programming language. However, scripting languages are often used. CGI Scripts take the request from client and will call appropriate functions to return the result to the requested clients. There are many language that could function as CGI language like Perl, C, C++, Tcl, Unix Shell Script, etc. However, Perl is without a doubt the most used languages for CGI scripting.

While the PHP language has certainly eclipsed Perl as the server-side language of choice (between the two of them, not Java or ASP), PHP was itself written as a set of Common Gateway Interface (CGI) binaries in the C programming language in 1994, to replace a small set of Perl scripts he had been using to maintain his personal homepage. Today, you will find CGI Script and PHP functionality built into almost every web hosting control panel.