Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

CGI program

by manishrathi (Beadle)
on Sep 23, 2009 at 14:41 UTC ( [id://796987]=perlquestion: print w/replies, xml ) Need Help??

manishrathi has asked for the wisdom of the Perl Monks concerning the following question:

I am confused with word CGI. 1) Is it a language or protocol ? If its a protocol, then how can there be programs in CGI ? I was going thru http://hoohoo.ncsa.illinois.edu/cgi/intro.html and I see CGI program words there. 2) If CGI is language, how is it different from PERL ? If I am writing perl code, where does CGI come in from ? 3) CGI stands for Common Gateway Interface, what kind of interface does it provide ? 4) If we provide CGI specifications in perl code, does it become a CGI program or stay as perl code ? Do we need any software installed to use CGI ? Is this software installed on web server or we have to install it separately ? 5) Please list different specifications, which turn perl code in CGI. When I was reading about CGI (which Idid not understand much), I came across only couple of parameters to make CGI program. What other parameters are there ? I have tried to do lot of reading on this, but did not understand CGI. So can someone please explain this basic idea of CGI. Pls dont point to another link. Thanks in advance

Replies are listed 'Best First'.
Re: CGI program
by moritz (Cardinal) on Sep 23, 2009 at 15:02 UTC

    1) CGI can mean many things, but first and foremost it's an API. It specifies how a web server can pass data to arbitrary programs. There can be no programs "written in CGI", but programs can use some CGI functionality.

    2) not applicable. (and there is no PERL, just Perl (the language) and perl (the compiler)).

    3) See answer in 1). Don't you think that other websites (like Wikipedia) have already answered this question?

    4) A program can be both a "perl program" (ie written in Perl) and a "CGI program" (a program using the CGI API).

    5) I don't understand that question. Are you referring to modules that help you writing CGI programs? If yes, there's CGI.pm, CGI::Minimal and other that can be found on CPAN.

    Perl 6 - links to (nearly) everything that is Perl 6.
      I did a lot reading before posting questions here and I am still confused.
      In the book "CGI programming with Perl", it was stated that CGI is a protocol. Does it mean that its something like HTTP ? But for using HTTP or LDAP or SQL, we dont use any API. We just open the interface on client machine for LDAP and SQL and comunicate. For HTTP, we dont need to do anything as its default protocol. So my confusion is about CGI being protocol. How can CGI protocol be used ? If we put a perl script in cgi-bin dir, will it automatically use CGI protocol ?
      Is CGI protocol and CGI API, two different things ?
      CGI APIs are created using what language ?
      There are a lot of Perl APIs available, then why CGI APIs are required ? If a program is written in Perl and CGI API is used in it, is that a Perl program or CGI Program ? What does it mean when .cgi extension is used ? When shall we use .cgi extension ?
      Is CGI API in-built in a web server for use ?
      What do you mean by "CGI specifies how a webserver can pass data to arbitrary programs" ? what is meant by "arbitrary programs" ?
      How is CGI API created ? Why do we have to use CGI functionality ? What is it that CGI has which a Perl does not ?
      Is it mandatory to use CGI to pass data to web server ? If we have not used CGI, how will data transfer to "arbitrary programs" ?
      Please excuse me , if some questions are primary. ButI am confused and want to get rid of this confusion.
        It is important to note that the Common Gateway Interface, or CGI, is language independent. Moritz points this out in his point #1. Because it is language independant, you can use Perl or any language that can run under the interface to provide web pages (i.e. bash+CGI, lisp+CGI, python+CGI, etc.).
        CGI states things like:
        • URL parameters are passed as command line parameters to the program
        • Certain environment variables contain other information, like the remote host, the request URL, the user (if authenticated via HTTP basic authentication) etc.
        • HTTP POST data is passed to the standard input of the program

        So you see that CGI is, like HTTP, a specified form to exchange data.

        Unlike HTTP the web server provides all information without asking first. Because CGI is not a networking protocol.

        Perl 6 - links to (nearly) everything that is Perl 6.
        I think most of your confusion is because you don't completely understand the difference between a protocol and a programming language.

        Here on perlmonks you will find many examples of Perl which don't use CGI. If you google 'CGI bash' or 'CGI BAT', you'll also find examples of CGI which don't use Perl.

        Another possible way to get the concepts clear in your head is to try using HTTP without a browser. Run telnet google.com 80 then type GET / and see what happens.

        If all else fails, just start using the language and the protocol, and come back when you have a more specific question.

        In the book "CGI programming with Perl", it was stated that CGI is a protocol. Does it mean that its something like HTTP ?

        Not quite. CGI is a protocol, but it is not a network protocol. It is a protocol defining a method which is used by web server software to interact with other software running on the same host.

        But for using HTTP or LDAP or SQL, we dont use any API. We just open the interface on client machine for LDAP and SQL and comunicate.

        And how do you "open the interface on the client machine"? You run a piece of software which uses the appropriate API to access the underlying protocol to communicate with the HTTP/LDAP/SQL server. Or, if you're writing the client software which does so, you either use an existing API to access the protocol or you implement the protocol yourself.

        If a program is written in Perl and CGI API is used in it, is that a Perl program or CGI Program ?

        Yes. It is both. It is a Perl program which uses CGI. It is also a CGI program written in Perl.

        What does it mean when .cgi extension is used ? When shall we use .cgi extension ?

        The .cgi extension is used to indicate to the web server software (Apache, IIS, etc.) that the file should be executed through the CGI interface instead of sending the file itself to clients requesting that URL. For Apache, this is established by the command "AddHandler cgi-script .cgi" in httpd.conf.

        Is CGI API in-built in a web server for use ?

        It depends on the server you're running. Apache implements CGI as an external module, mod_cgi. Other servers build it into the core feature set.

        What do you mean by "CGI specifies how a webserver can pass data to arbitrary programs" ? what is meant by "arbitrary programs" ?

        "Arbitrary" just means that it can be anything. CGI can be used by a web server to pass data to any program you choose without the server having to know anything special about the program that will be receiving the data.

Re: CGI program
by saberworks (Curate) on Sep 23, 2009 at 16:16 UTC
    There seems to be some misunderstanding of what CGI is and how it's utilized. Some people say it's just the way you pass parameters to and from scripts (url?some_var=some_val), but my understanding is that this is wrong. CGI is a specification for your web server to communicate with your script. The way it communicates is by a series of environment variables guaranteed to be there and with specific uses. For example, QUERY_STRING, REQUEST_METHOD, etc. These are all listed in the RFC.

    The nice thing about programming perl for CGI is that we have the nifty CGI module (or any of the other, smaller ones) that examines all those environment variables and provides functional or OO interfaces to the relevant information they contain. So instead of having to parse $ENV{'QUERY_STRING'} manually, the CGI module does it for us. In addition, the CGI specification provides a way to access things like the server name, the script name, the remote host, etc.

    The RFC describes this succinctly:
    The server acts as an application gateway. It receives the request from the client, selects a CGI script to handle the request, converts the client request to a CGI request, executes the script and converts the CGI response into a response for the client. When processing the client request, it is responsible for implementing any protocol or transport level authentication and security. The server MAY also function in a 'non-transparent' manner, modifying the request or response in order to provide some additional service, such as media type transformation or protocol reduction.
      AUTH_TYPE
      CONTENT_LENGTH
      CONTENT_TYPE
      GATEWAY_INTERFACE
      PATH_INFO
      PATH_TRANSLATED
      QUERY_STRING
      REMOTE_ADDR
      REMOTE_HOST
      REMOTE_IDENT
      REMOTE_USER
      REQUEST_METHOD
      SCRIPT_NAME
      SERVER_NAME
      SERVER_PORT
      SERVER_PROTOCOL
      SERVER_SOFTWARE
      Are these variables part of web server or are they defined by CGI ? I am um=nder thye impression that these varibles are defined as environment variables by the web server environment.
      AM I correct , or are these variables specified by the CGI only and when a CGI is used in a program , only at thah time these variables can be used in a program ? If these variables are specified by the web server, then why can they not be used in the script directly ? Why do we have to use CGI to get these values in the script ? thanks

        The CGI specification requires the web server to set these environment variables. The script is then free to do with these variables what it wants.

        You don't need to use the module CGI.pm to access these variables, but it provides a convenient way to do it.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: CGI program
by gman (Friar) on Sep 23, 2009 at 15:10 UTC
    Manishrathi,

    I understand why you might be confused, but it's as simple as CGI defines a way for your program to interface with the user over http. CGI in not a language but more a standard. I know you did not want another link but the RFC is pretty clear. http://www.ietf.org/rfc/rfc3875

    So Perl can be used to talk to most HTTP servers, but you do not turn a perl program into a CGI "program". I always find examples help me to understand best. If you have the ability I would start off with installing apache and perl on a system and play around with it, then ask the same questions, they will probably be much clearer.

      This is what I got from the link
      "The Common Gateway Interface (CGI) is a simple interface for running external programs, software or gateways under an information server in a platform-independent manner. Currently, the supported information servers are HTTP servers"
      What I understand is "external programs (Perl program) are placed in HTTP server and its invoked by CGI" How does CGI get invoked ? IsCGI a completely separate from external perl program or is it a part of perl program ?
        You can write CGI programs in lots of different languages. Here is a simple one in Perl. If you run this code, it will output HTML. If you save that HTML to a file, you can display that file contents in a browser (use open | file).

        #!/usr/bin/perl -w use strict; use CGI; my $q = new CGI; my $timestamp = localtime; print $q->header ("text/html"), $q->start_html ( -title => "Current Time"), $q->h2 ("Current Time"), $q->hr, $q->p( "This system figures current time as: ", $q->b($timestamp) ), $q->end_html;
        You can call this short thing, say "time". Have your friendly (I hope) web administrator set this up so that it can be accessed via a URL and bingo you have your first CGI program working! Basically to output a web page, write a program that spews out HTML to stdout. There is some config stuff at the server end, but this is the idea of what your program has to do.
        Pls tell me how request flows ?

        web surfer sends request for as page
        HTTP server looks at the request and if it involves external program, then invokes external perl program which is in some dir on the web server. Passes request parameters to this program
        Perl program is executed and sends the result back to the request

        Where does CGI come in picture in this process ?
Re: CGI program
by Marshall (Canon) on Sep 23, 2009 at 15:37 UTC
    This url: http://www.perlmonks.org/?node_id=796987 is an example of the easiest way for a client to send some information to a web server. In this case the perlmonks.org server has some code that understands that the "?" means that there is some more info coming. "node_id" isn't some generic term, it is something that the permonks server specifically understands and will interpert as a request for this posting thread! That is what you get back... a page about this thread. This is called a "get" request.

    There is another way to send info to the server, a "post" request. This is not sent as a simple URL like you can type into your browser.

    This whole business can get complex in the details, but the basic idea is the web browser sends some info to the server and the web server responds with a web page that can be displayed in a browser (protocol is generically called HTTP). CGI provides the basic mechanism for this transfer of information from client to server, an API, Application Program Interface.

Re: CGI program
by leocharre (Priest) on Sep 23, 2009 at 17:43 UTC

    First and foremost you are going to have to get it into your head that CGI is a concept, not a thing. (If you need to understand what I mean by that, use a dictionary.)

    That said, this is not an appropriate place to ask what cgi means. Discussion is widely available on the web.

    If you have been doing a lot of reading on this and still do not get it, you need to ask someone in person. Asking this here is akin to trolling. (You may as well ask a journalist what the letter r means.)

    If you really still don't get it, maybe this is not your line of work.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://796987]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-24 06:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found