next up previous contents
Next: The CGI module Up: Perl on the Web Previous: Data Tainting   Contents

A Simple CGI program

To get familiar with the basics of CGI programming, we'll illustrate how to create a simple program which will just print the current date and time to a browser which calls it.
print "Content-type: text/html\n\n";
print "<h1>This is the time!</h1>";
print qq{<html><body bgcolor="blue"><title>The Time</title></body>\n};
printf qq{The time is now %s<br>Thanks for visiting},scalar localtime;
print "</html>";
The line which prints the Content-type must end with two newlines -- this is the signal to your web browser that the headers are complete, and the actual page follows. The value of text/html tells the browser that what follows is HTML, and should be interpreted accordingly. Without this or a similar header, most browsers will not be able to read your output. The rest of the program simply prints out some text, which you may recognize as simple HTML. If you know how to write HTML, you may not need much from the CGI module; if you don't, you'll find that the CGI module eliminates the need to learn too much about HTML.



Phil Spector 2002-10-18