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

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

"You can configure your HTTP server so that whenever a file in a particular directory is requested, the file is not sent back; instead it runs as a program, and whatever the program says is sent back to display your browser. This function called the Common Gateway Interface or CGI and the programs are called CGI scripts. "

So my question is that not only the CGI script will be sent as an executable, all other server side script languages ​​will be sent as an executable from the HTTP server. So what is the main purpose of CGI?

I eagerly want to know the answer as I am really very confused. This question may be silly, 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 keep it as extensions .pl

and .cgi

and run both programs, I get the same result. Then what's the difference between a cgi script and perl ?.

+3


source to share


2 answers


CGI is a standard method or interface, or Common Gateway Interface, for applications that communicate with web servers and dynamically generate web pages for client requests.

The fact that your application can have an extension .cgi

depends mainly on the configuration of your web server. The application can be extended .php

or, .pl

and can be customized to work with your web server using CGI techniques. While .cgi

somewhat confusing about the language the script is written in, for over a decade that language has been most commonly used by perl. In fact, perl included a CGI library to make it easier to write CGI applications into perl. If you run the application from the command line rather than through your web server, then the interpreter will appear within your *.cgi

script, as in the case with perl CGI-application will look like this: #!/usr/local/bin/perl

. Because of the relative simplicity of CGI, CGI applications have been written in many languages ​​- even as scripts $SHELL

.



CGI has largely moved in process approaches or full stack of HTTP server applications written in specific languages ​​(sometimes running "behind" a proxy or "inside" a web server process), mainly because these approaches provide improved security and performance. In the world of perl, application frameworks such as Plack

are part of this more modern approach to dynamic web applications. Plack even includes Plack::App::WrapCGI

one that can "update" your CGI applications to work within the Plack framework.

Hope it helps.

+4


source


The relationship between Perl and CGI is often confused, mainly because they often appear together. This happens so often that the term "Perl CGI" has become commonplace. In reality, any number of programming languages ​​can be used with the Common Gateway interface. Additionally, Perl runs through cgi-bin, suggesting relationships. If that's not enough, the file extension is usually also .cgi.

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



While PHP has certainly eclipsed Perl as the preferred server-side language (between the two rather than Java or ASP), PHP itself was written as a collection of Common Gateway Interface (CGI) binaries in the C programming language in 1994 to replace a small set of Perl scripts he used to maintain his personal home page. Today you will find CGI Script and PHP functions built into almost every web hosting control panel.

+3


source







All Articles