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

Hi all, when a request is sent to the apache server to call a perl program , how is the print statement in the perl script sent to the browser . is it something like this when the server calls the perl script it closes the std out and opens the std out to the socket connected by the browser ...please throw some light on this

Replies are listed 'Best First'.
Re: Apache invoke perl
by liz (Monsignor) on Jan 02, 2004 at 11:54 UTC
    From your question, it is not clear whether you're using a "normal" CGI script or a mod_perl handler. The site to find answers to questions related to Perl and Apache is http://perl.apache.org.

    In case you're using mod_perl, you might find this interesting.

    Liz

Re: Apache invoke perl
by fizbin (Chaplain) on Jan 02, 2004 at 18:50 UTC
    In how much detail do you want this answer?

    The essential spawning of the child process is done in the file mod_cgi.c, viewable at http://cvs.apache.org/viewcvs.cgi/apache-1.3/src/modules/standard/mod_cgi.c?view=markup

    The function you're looking for is cgi_handler. You might also be interested in the function ap_bspawn_child which is, I'm certain, somewhere in the ap/ source subdirectory.

    From the source, it appears that what happens is that the child CGI process is opened with pipes connected to its standard input and standard output - the apache process then feeds stuff to the child process over these pipes and acts as a middleman between the process and the network (for example, unless you have a "nph" CGI script, the apache process will add in certain header fields to the response). Standard error of the CGI process appears to remain connected to the error log file (directly, without going through a pipe).

    For more on communicating with child processes through pipes, consult any good book on unix programming if you want to do this in C (especially look for something that describes the proper use of the dup2 function). If you want to do this kind of stuff in perl, look at the peripc manpage.