Mike Gossland's Perl Tutorial Course for Windows


Intro to CGI | First Page | Printing HTML | Serving HTML | Serving Edited


Chapter 5. Introduction to CGI

Serving HTML Files

Very often in CGI programming, you'll want to call up a particular HTML file and use it as the basis of your response. In its simplest form, this is accomplished with nothing more than sending a header, opening the file and printing it.

Try saving some text inside a page as "serving.html" in your cgi-bin directory. You could actually save it anywhere on your hard drive, but putting it in your cgi-bin directory will allow you to leave out the path to the file in the example.

Next, run this perl script:

print "Content-Type: text/html\n\n";
open HTML, "serving.html" or die $!;
while( <HTML> ) {
  print;
}
close HTML;

Pretty simple, huh. You could display other files just by modifying the path. As an exercise, try serving other html files on your disk. Then try displaying a pure text file, with a header of text/html, and then with text/plain.

Now you've seen how to open and serve files, you're ready to see how to edit files on the fly and use them as templates.