Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between perl as a language and CGI scripts?

Tags:

cgi

perl

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

like image 927
vidhya Avatar asked Mar 17 '23 21:03

vidhya


2 Answers

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.

like image 145
G. Cito Avatar answered Apr 08 '23 06:04

G. Cito


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.

like image 38
Parth Akbari Avatar answered Apr 08 '23 06:04

Parth Akbari