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

Hello monks!
I'm starting with cgi with the precious (even if in idiomatic english !) Ovid's guide...
I wrote a simple html form with only a parameter: color and a submit (or stumbit ?!?) button.It use GET method to the following script:
#!perl #yes I WILL use -wT within the final code! use CGI; $|=1; # I put this on my own IS IT RIGTH ? $q=new CGI; print $q->header( -nph=>1, #unfortunatly IIS -status=>'200 OK', -type=>'text/html'), $val=$q->param('colore'); if ($val eq 'red'){print $q->h3(tomorrow we will have sun')} else {print $q->h3('Good Morning!)} $q->end_html();

Here the questions:

1-It runs but when the cgi recive red as param it prints out the value before any other things. Why?

2-Is correct the $|=1; line ? When I really need this unbuffered stuff ?

3-Can I use only cgi file to print out html output? I prefer the syntax of the module instead of the html one.

4-Why Ovid never use $|=1; ?

Thanks in advance
Lor*

Replies are listed 'Best First'.
Re: CGI newbie
by tachyon (Chancellor) on Nov 30, 2002 at 14:24 UTC

    1-It runs but when the cgi recive red as param it prints out the value before any other things. Why?

    Don't know what you mean by this, but you have two typos in your code where you forget to open and close your quotes.

    2-Is correct the $|=1; line ? When I really need this unbuffered stuff ?

    Yes this switches off buffering which is often a good idea. See Suffering from Buffering for details

    3-Can I use only cgi file to print out html output? I prefer the syntax of the module instead of the html one.

    No you can just use print if you want, see the example below.

    4-Why Ovid never use $|=1; ?

    He does ;-) But he knows when it is needed....

    Using strict and warnings will save you a lot of grief, See Use strict warnings and diagnostics or die

    -T taint mode protects you from allowing CGI input to do nasty stuff to your file system. You set it up a little different on IIS or you will get a too late for -T error. See http://gunther.web66.com/FAQS/taintmode.html

    #!perl use strict; # stop typos and other errors causing grief use warnings; # stop CGI newbie making silly errors use diagnostics; # explain warnings to CGI newbie use CGI; my $q = new CGI; print $q->header; $q->param('calore') ||= ''; # ensure param initialized before using e +q if ( $q->param('calore') eq 'red' ) { print '<h3>You said red</h3>'; } else { print '<h3>You did not say red</h3>'; }

    cheers

    tachyon

    s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Re: CGI newbie
by dws (Chancellor) on Nov 30, 2002 at 17:38 UTC
    I've deployed CGIs on IIS, and, unless your script is going to be doing something much more sophisticated than printing a page,
    print $q->header( -nph=>1, #unfortunatly IIS -status=>'200 OK', -type=>'text/html'), ^--- ;
    can safely be reduced to print $q->header(); Also, notice that you have a comma where you should have a semicolon. (Was this code copy/pasted?)

    Also, it's a good idea to get into the habit early of trying to emit syntacticly valid HTML.

Re: CGI newbie
by Ryszard (Priest) on Nov 30, 2002 at 14:46 UTC
    there are a couple of mistakes in your code i see:

    $q->h3(tomorrow we will have sun')

    should become

    $q->h3('tomorrow we will have sun')

    and

    $q->h3('Good Morning!)

    should become

    $q->h3('Good Morning!')

    You could prolly use also use something like $q->start_html.

    Question 1: not really sure..

    Question 2: Buffering, you may need it, you may not. Buffering is turned on by default in perl, what you are doing is turning it off, this means, as your script prints to standard out, it will be displayed in the browser. The alternative (autoflush off, $|=0) will buffer all the output, then send it to your broswer all at once at the end of your script. Why use buffering? have a look here

    Question 3: Not quite sure exactly what you mean, however you can form valid HTML in any method you choose, as long as it ultimately goes so STDOUT...

    Question 4: as mentioned above, its completely application dependant...