Home / code / cgi

The basic

If you write Perl CGI for webpages, you always have to included the header and the complete HTML-document. The header is normally send by the Web-server, but with CGI script you have to add it manually. This happens through the field content-type followed by the type of data send (in this case text/html). The header of a CGI script must always be followed by a blank line (\n\n).

print "content-type: text/html\n\n";

Next to text/html there are thousand other types. A few example image/gif, image/jpeg, video/mpeg

A complete Perl CGI file for the web looks something like this:

print "content-type: text/html\n\n";

print "<HTML>\n";
print "<HEAD><TITLE>My first CGI script\n</TITLE></HEAD>"\n;
print "<BODY>\n";

print "<H1>I'm a cool perl hacker</H1>\n";

print "</BODY>\n";
print "</HTML>\n";
In the above example there is print function used for every line. We can do this much shorter:
print "content-type: text/html\n\n";

print <<end;
<HTML>
<HEAD><TITLE>My first CGI script\n</TITLE></HEAD>
<BODY>

<H1>I'm a cool perl hacker</H1>

</BODY>
</HTML>
end

Comment

If you write comment in your code you have to put # in front of it. Simple as that.

 

TOP

Latest script:

 

Books: