CGI/PERL Tutorial over the Web

Click to go to the PREVIOUS page. Click to go to the INDEX page. Click to go to the NEXT page.

SLIDE #10 A CGI PROGRAM WRITING TO A FILE


In this tenth slide, we introduce the CGI program writing to a file.

To write a record to the file, we open the file:
open (OUTPUT,">>user");
we print the first name:
print OUTPUT $first."\n";
we print the last name
print OUTPUT $last."\n";
we close the file:
close (OUTPUT);

1. THE CGI PROGRAM:

#!/usr/local/bin/perl
use CGI;
$query=new CGI;
$first=$query->param('first');
$last=$query->param('last');
print $query->header;
print "<html><head><title>Writing data to a File</title></head>\n";
print "<body>";
print "<b>Your name was added to the User File.<b>\n";
print "<br>".$first." ";
print $last;
open (OUTPUT,">>user");
print OUTPUT $first."\n";
print OUTPUT $last."\n";
close (OUTPUT);
print "<br><b>Reading from the User File: Current List of Users:<b>\n";
open (INPUT,"<user");
$j=0;
while (<INPUT>)
{
	$first=$_;
	$last=<INPUT>;
	$name="<br>First Name: ${first}, Last Name: ${last}";
	$hh[$j]=$name;
	$j++;
}
close (INPUT);
foreach $ii (@hh) {print $ii;}
print "</body></html>";

2. PROGRAM DESCRIPTION:

The first three lines are identical as slide #7

The next two lines read the first name and last name that were entered on the
form.

The next six lines (6-11) print the document header, the title, and the 
information entered on the form

The next 4 lines append (>>) this information to a file, called user:
open (OUTPUT,">>user");
print OUTPUT $first."\n";
print OUTPUT $last."\n";
close (OUTPUT);

The next 12 lines (16-27) read back the data from the file

The last two lines display the content of the file.


© Copyright 2002 International Customized Software Co.