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

Hi,

I'm going to try and make a switch from PHP to Perl for my next web project. I've just been going through the basics and was wondering why there is the need to output an HTTP header line as the first line of output from a perl script. I run apache and it seems that a certain level of intelligence takes place before passing the data back to the client because Apache seems to cut out any duplicate HTTP headers, that is to say it appears that the ouput from Perl is not just added to the headers and sent without first being checked. For example, if the first line of my Perl output is to simply output "\r\n", that works fine and a content-type header is added automatically by Apache as I would expect. If I output a Content-Type header in Perl, Apache doesn't then try and add this header twice. Therefore, I would have though that it would be simple enough for Apache to add the extra line break needed before sending back the data. I am hoping someone can help me understand how it is all formed as It always helps me to know why things behave how they do. Hope that made some sense.


regards

cyril

Replies are listed 'Best First'.
Re: cgi theory
by hardburn (Abbot) on Sep 15, 2003 at 20:42 UTC

    Content-type needs to be printed because you may want to output things other than text/html. You could also do application/pdf or image/jpeg.

    Apache needs that blank line because it has no good way of otherwise knowing when you're done printing headers and want to send the actual content. There could be more headers to send other than Content-type.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

      thanks for the reply

      It does make sense really, I think its just a case of PHP making things easy for the developer rather than exposing more of the realities of how things are put together. PHP much automatically generate the blank line if no additional headers are added by the programmer.


      cyril
        Cyril

        PHP does a good job of hiding much of the mechanics from the programmer, which is one reason it is so popular. But it doesn't take long before you realise that in fact it hides too much. All too often PHP has only one way to do it! Instead of the Perl way - Their is ALWAYS more than one way to do it.

        Perl, with its seemingly endless variety of modules will give you so many more ways to do things, it will give you so much flexibility that it can be rather frightening. In fact it will offer you so many choices as to be totally confusing. Sometimes when asked what Perl has that PHP doesn't the reply is 'CPAN'. Many years of dedicated work by some great computing minds.

        If you are writing CGI scripts then make sure you search here in the monastery and read some of the tutorials and responses addressing various aspects of that subject, they are a veritable gold-mine.

        jdtoronto

Re: cgi theory
by bear0053 (Hermit) on Sep 15, 2003 at 20:46 UTC
    you can print out more than one header the following way:
    print "Content-Type: text/html\n"; print "Set-Cookie: $cookie_object\n\n";
    notice you can print individual headers out by ending them with a simple \n the last header must have \n\n to finish prionting all the headers. depending on whethere you are using strict or warnings printing out multiple:
    print "Content-Type: text/html\n\n"; print "Content-Type: text/html\n\n";
    will produce some warnings. Printing out a header informs the program of the type of data being processed
Re: cgi theory
by Taulmarill (Deacon) on Sep 15, 2003 at 20:51 UTC
    a thing i always do at the begining of a cgi is:
    print "Content-type: text/html\n\n";
    if you whant to use the cgi module you can do this:
    use CGI; my $cgi = new CGI; print $cgi->header( "text/html" ); .... do stuff here .... print $cgi->end_html;
    maybe you want to take a look in the O´Reilly Perl CGI book.
    it will teach you clean perl/cgi and O´Reilly books are always worth the price when it comes to Perl.

    so, have a nice time coding perl...

      If you want to use the CGI.pm module but only to print a header you can do this:
      use CGI qw(header); print header;
      Note that calling header() without any arguments returns the header for an HTML file.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      
Re: cgi theory
by tcf22 (Priest) on Sep 15, 2003 at 20:47 UTC
    All webservers behave differently. Using Perl under IIS, if you don't print out the content-type you end up with a

    CGI Error
    The CGI script didn't return a complete set of HTTP headers.

    So to be safe, adding a content type will make sure that the script will run under various webservers.

    - Tom