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

when a user submits a form to look the resultant webpage, this fprm is received by the webserver and different cgi parameters are created by the webserver as per cgi protocol. These parameters are handed over to the script amd the perl script is run to produce HTML output. this output is sent to STDOUT, is to the command prompy console on server. How is this output sent to client browser which made the request for the HTML document ?
To my understanding, once the output is diaplayed on STDOUT, then execution finishes there. In case of server, where there is no monitor screen, how would the output be displayed ?

Replies are listed 'Best First'.
Re: display of output from cgi script
by almut (Canon) on Dec 21, 2009 at 13:14 UTC
    ... In case of server, where there is no monitor screen, how would the output be displayed ?

    I think your notion of STDOUT is too limited (or specialised).  From what you write, it seems you think STDOUT has to be connected to a terminal (so the output would typically be displayed on the monitor screen). This is not necessarily the case. STDOUT is just a file handle that can be opened to anything a file handle can in principle be opened to. In this particular case, the CGI script's standard handles (out/err/in) are connected to pipes set up by the webserver, which reads the data output by the script and passes it on to the browser (via the socket that connects the webserver with the browser).

      1) So STDOUT is just a file handle to store any output. How is the output transfered to browser from STDOUT is still not clear.
      "CGI script's standard handles (out/err/in) are connected to pipes set up by the webserver" what does this mean ? Can you pls explain more on this ?

      2) when data from form is transferrd by browser to webserver, webserver stores this data in environment variables. Are these environment variables specific to CGI interface or are they standard for all interfaces ? In other words, if I am using CGI protocol to generate dynamic pages then I am using these environment variables to get the data. But if I am using any other way than CGI, will I still be using same environment variables or will it be diferent ?
      Are these environemnt variables set by the webserver or OS ?
        "CGI script's standard handles (out/err/in) are connected to pipes set up by the webserver" what does this mean ?

        A pipe is an OS-supplied means used for one-way interprocess communication.  That is, a pipe has two ends, one for reading and one for writing. Both ends are like normal file handles (except that they aren't seek-able).

        Here's a quick sample in Perl to illustrate what a webserver typically does to run a CGI script (this is heavily simplified, and webservers like Apache of course do implement this in C, but you get the idea...):

        #!/usr/bin/perl pipe RH, WH; # Perl's interface to the system call of the same name my $pid = fork(); die "fork failed" unless defined $pid; if ($pid) { # parent - the webserver process that handles the request close WH; # read the CGI's output from the pipe while (my $line = <RH>) { print STDERR "CGI said: $line"; # (this would be passed on to the browser...) } } else { # child - a new process which runs the CGI script close RH; close STDOUT; # connect stdout (more precisely: file descriptor 1) to # the writing end of the pipe open STDOUT, ">&WH" or die "open to pipe failed: $!"; exec "perl mycgi.pl" or die "exec of CGI failed: $!"; }

        With the following sample mycgi.pl

        #!/usr/bin/perl print <<'EOCGI'; Content-Type: text/html <html> <header><title>Pipe Demo</title></header> <body> <pre> foo bar baz </pre> </body> </html> EOCGI

        the output produced by th above snippet would be

        $ ./813729.pl CGI said: Content-Type: text/html CGI said: CGI said: <html> CGI said: <header><title>Pipe Demo</title></header> CGI said: <body> CGI said: <pre> CGI said: foo CGI said: bar CGI said: baz CGI said: </pre> CGI said: </body> CGI said: </html>