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

hi , i'm a novice at perl so my prob might b very simple.iwas trying out this simple example for creating a form .when i do by simple html making the code like:
<html> <head> <title> form </title></head> <body bgcolor="red"> <form> <input type="text" name ="foo" value="bar"/> <select name="sport"> <option> water balloon shotput</option> <option> 17-legged race</option> </select> <P>choose ur fav. types of wrestling:</P> <input type ="checkbox" name ="wrestling" value ="mud"/>mud<br /> <input type ="checkbox" name ="wrestling" value= "jello" />jello<br /> </form> </html>
the output page is ok but when i do with cgi programming and code looks like:
#!/usr/bin/perl -w print "content-type:text/html\n\n"; print "<html><head><title> form </title></head>; print "<body bgcolor="red">"; print "<form> <input type="text" name ="foo" value="bar"/>"; print "<select name="sport">"; print "<option> water balloon shotput</option>"; print "<option> 17-legged race</option>"; print "</select>"; print "<P>choose ur fav. types of wrestling:</P>"; print "<input type ="checkbox" name ="wrestling" value ="mud"/>mud<br +/>"; print "<input type ="checkbox" name ="wrestling" value= "jello" />jell +o<br />"; print"</form>"; print "</html>;"
the page is not clean and the print commands and the first two lines r also appearing.how do i remove this?? VAIBHAV

update (broquaint): added formatting and changed title (was NOT gettin clean page)

Replies are listed 'Best First'.
Re: Not getting clean HTML using print
by gjb (Vicar) on Jul 03, 2003 at 12:18 UTC

    In the strings to print, you should escape the double quotes, i.e. print "hello \"world\""; would print hello "world".

    This is the short answer, the longer and better one is that you could have a look at the CGI module that makes generating HTML a lot less painful. Various tutorials can be found in the monastry. A number of templating systems exists, HTML::Template comes to mind.

    Hope this helps, -gjb-

      >In the strings to print, you should escape the double quotes

      or use alternative quotes:

      print 'hello "world"'; print qq{hello "world"};
Re: Not getting clean HTML using print
by tcf22 (Priest) on Jul 03, 2003 at 13:16 UTC
    Within a double quoted string, you must escape double quotes("). Also, if you aren't using variables, or special characters(\n,\t,etc.) that need interpolation, you should probably use single quotes('). It is slightly more efficient.

    Additionally, if you are printing out a large chunk of HTML, you can use a multiline print instead of a bunch of print statements, like this
    print <<_MARKER_ <html> <head> <title> form </title></head> <body bgcolor="red"> <form> <input type="text" name ="foo" value="bar"/> <select name="sport"> < option> water balloon shotput</option> <option> 17-legged race</option> </select> choose ur fav. types of wrestling: <input type ="checkbox" name ="wrestling" value ="mud"/>mud <input type ="checkbox" name ="wrestling" value= "jello" />jello </form> </html> _MARKER_
Re: Not getting clean HTML using print
by JoeJaz (Monk) on Jul 03, 2003 at 14:37 UTC
    Hi, I can think of a few things that might be a problem. First of all, you must escape the quotation marks included in your print statements. For example, you have:
    print "<select name="sport">";
    Perl doesn't know that the quotation mark after name= is not an ending quotation for the entire string. To fix this, you need to escape the quotation marks with the backslash. The new code would look like this:
    print "<select name=\"sport\">";
    See if this works for you. If it does not solve your problem, perhaps apache (or whatever webserver you are using) is not set up to to execute CGI programs. You may need to add a ExecCGI directive to the appropriate place in your httpd.conf. See if these work. Hope this helps.