Create Free Websites
Create a Free Website at Ucoz
GrrooP.com
Jimdo.comSign Up
MoonFruit


Create Your Own
Designs

Without Tables

Artisteer - Web Design Generator

Use your knowledge of HTML to turn your template into a website.

Download a FREE Trial

PERL CGI Tutorial
for Writing Interactive Form Scripts

Saving Information from an HTML Form to a File

There are 2 methods of writing files when using PERL. You can overwrite a file or you can append data to the end of an existing file.

Overwriting an Existing File

This method of file writing will replace the contents of an existing file with new data. If the file does not exist an error occurs and nothing happens unless you've provided an error trapping routine.

open (INFO,">filename.txt");
print INFO "$data\n";
close(INFO);

Appending to a File

This method of writing a file will add data to the end of an existing file. If the file does not exist PERL creates it for you.

open (INFO,">>filename.txt");
print INFO "$data\n";
close(INFO);

Adding the Code

The Append method will work best with the CGI scripts that we've already created. Copy and paste the code below into parse_form.pl. (Add to bottom of script) Save parse_form.pl. Start server and load form.pl into the editor. Run form.pl several times. Then open names.txt with NotePad and note how the names are stored in the file. In the next lesson we'll learn how to open the file and restore the values to their original form.

open (INFO,">>names.txt");
print INFO "$last|$first\n";
close(INFO);

More Information

For information on adding additional fields see CGI Script #2 in the CGI Script Gallery.