afile.dat" || die "Could not open afile.dat\n"; print AFILE "junk\n"; print AFILE "more junk\n"; close AFILE; file2.pl #!/usr/bin/perl # append to an existing file open AFILE, ">>afile.dat" || die "Could not open afile.dat\n"; print AFILE "junk appended\n"; print AFILE "more junk appended\n"; close AFILE;"> afile.dat" || die "Could not open afile.dat\n"; print AFILE "junk\n"; print AFILE "more junk\n"; close AFILE; file2.pl #!/usr/bin/perl # append to an existing file open AFILE, ">>afile.dat" || die "Could not open afile.dat\n"; print AFILE "junk appended\n"; print AFILE "more junk appended\n"; close AFILE;">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Class 10 Programming plan for today: More files Saving data from a form.

Similar presentations


Presentation on theme: "CS 330 Class 10 Programming plan for today: More files Saving data from a form."— Presentation transcript:

1 CS 330 Class 10 Programming plan for today: More files Saving data from a form

2 More Files Recall opening a file for reading in avggrd.pl open GRADES, "grades.dat” GRADES is the file handle (internal name), grades.dat is the file Alternate: open GRADES, ”<grades.dat” (< indicates data in) Creating a new file (file1.pl) : open FILEHANDLE, “ > filename”; write FILEHANDLE “stuff” Appending to an existing file (file2.pl): open FILEHANDLE, “ >> filename”; Reading from a file (file3.pl): open FILEHANDLE, “ < filename”;

3 file1.pl #!/usr/bin/perl #create a new file open AFILE, ">afile.dat" || die "Could not open afile.dat\n"; print AFILE "junk\n"; print AFILE "more junk\n"; close AFILE; file2.pl #!/usr/bin/perl # append to an existing file open AFILE, ">>afile.dat" || die "Could not open afile.dat\n"; print AFILE "junk appended\n"; print AFILE "more junk appended\n"; close AFILE;

4 file3.pl #!/usr/bin/perl # read an existing file open AFILE, "< afile.dat" || die "Could not open afile.dat\n"; while ( ) { print; } close AFILE; Caveat: the file must have read or write access as appropriate

5 Saving data A major use of CGI is to maintain data from processing a form between transactions. form4.htm - accepts form data and passes to guest4.cgi../data/guest.dat - first and last names, one name per line../data/access.dat - an integer form4.cgi –saves first and last names to guest.dat –adds one to the number in access.dat form4r.cgi –reads the names from guest.dat and sends to client access.cgi –reads the integer in access.dat and passes to client

6 CGI Communication Client Server HTTP server form4.cgi (5) guest.dat (1) Client requests form4.htm (2) Server sends form4.htm (3) Client returns form data with GET request to form4.cgi (4) Server executes form4.cgi (5) form4.cgi reads and writes to guest.dat and access.dat (6) Server translates form4.cgi into HTTP response to client (2) (3) (4) (1) access.dat (6)

7 form4.cgi #!/usr/bin/perl @pairs = split (/&/,$ENV{'QUERY_STRING'}); foreach $pair (@pairs){ ($field_name, $value) = split(/=/,$pair); $form{$field_name}=$value; } #add the new visitor to the file if (open GUESTFILE, ">>../data/guest.dat") { print GUESTFILE "$form{firstName}\n"; print GUESTFILE "$form{lastName}\n"; close GUESTFILE; } else{ print "Content-type: text/html\n\n"; print " \n"; print " Form Processing \n"; print " \n"; print "Guest data not saved.\n"; print " \n"; exit; }

8 form4.cgi (cont) #update the number of accesses to the file if (open ACCESSFILE, "</home/cs330/public_html/scripts/access.dat") { $no_accesses= ; $no_accesses++; close ACCESSFILE; open ACCESSFILE, ">/home/cs330/public_html/scripts/access.dat"; print ACCESSFILE "$no_accesses"; close ACCESSFILE; } else { open ACCESSFILE, ">../data/access.dat"; print ACCESSFILE "1"; close ACCESSFILE; } print "Content-type: text/html\n\n"; print " \n"; print " Form Processing \n"; print " \n"; print "Guest data saved.\n"; print " \n";

9 form4r.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print " \n"; print " Retrieving Form Data from a File \n"; print " Visitors to this form: \n"; print " \n"; open INFILE,"<../data/guest.dat" || die "Could not open guest,dat\n"; while ( ) { $first = $_; chop($first); $last = ; chop($last); print $first," ",$last, "\n"; } print " \n";

10 access.cgi #!/usr/bin/perl print "Content-type: text/html\n\n"; print " \n"; print " Retrieving Data from a File \n"; print " \n"; open ACCESSFILE,"<..data//access.dat" ; $accesses = ; print "The number of visitors to this form: $accesses"; print " "; print " \n"; If time: pizza.htm


Download ppt "CS 330 Class 10 Programming plan for today: More files Saving data from a form."

Similar presentations


Ads by Google