CS 330 Class 10 - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

CS 330 Class 10

Description:

Recall opening a file for reading in avggrd.pl. open GRADES, 'grades.dat' ... Appending to an existing file (file2.pl): open FILEHANDLE, ' filename' ... – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 11
Provided by: cshil
Category:
Tags: appending | class

less

Transcript and Presenter's Notes

Title: CS 330 Class 10


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, ltgrades.dat (lt
    indicates data in)
  • Creating a new file (file1.pl)
  • open FILEHANDLE, gt filename
  • write FILEHANDLE stuff
  • Appending to an existing file (file2.pl)
  • open FILEHANDLE, gtgt filename
  • Reading from a file (file3.pl)
  • open FILEHANDLE, lt filename

3
file1.pl !/usr/bin/perl create a new file open
AFILE, "gtafile.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, "gtgtafile.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, "lt afile.dat"
die "Could not open afile.dat\n" while (ltAFILEgt)
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
Server
(1)
HTTP server
(2)
Client
(3)
(6)
(4)
(4)
guest.dat
form4.cgi (5)
access.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
7
form4.cgi !/usr/bin/perl _at_pairs split
(//,ENV'QUERY_STRING') foreach pair
(_at_pairs) (field_name, value)
split(//,pair) formfield_namevalue
add the new visitor to the file if (open
GUESTFILE, "gtgt../data/guest.dat") print
GUESTFILE "formfirstName\n" print
GUESTFILE "formlastName\n" close
GUESTFILE else print "Content-type
text/html\n\n" print "lthtmlgt\n" print
"ltheadgtlttitlegtForm Processinglt/titlegtlt/headgt\n"
print "ltbodygt\n" print "Guest data not
saved.\n" print "lt/bodygtlt/htmlgt\n"
exit
8
form4.cgi (cont) update the number of accesses
to the file if (open ACCESSFILE,
"lt/home/cs330/public_html/scripts/access.dat")
no_accesses ltACCESSFILEgt
no_accesses close ACCESSFILE open
ACCESSFILE, "gt/home/cs330/public_html/scripts/acce
ss.dat" print ACCESSFILE "no_accesses"
close ACCESSFILE else open
ACCESSFILE, "gt../data/access.dat" print
ACCESSFILE "1" close ACCESSFILE print
"Content-type text/html\n\n" print
"lthtmlgt\n" print "ltheadgtlttitlegtForm
Processinglt/titlegtlt/headgt\n" print "ltbodygt\n"
print "Guest data saved.\n" print
"lt/bodygtlt/htmlgt\n"
9
form4r.cgi !/usr/bin/perl print "Content-type
text/html\n\n" print "lthtmlgt\n" print
"ltheadgtlttitlegtRetrieving Form Data from a
Filelt/titlegtlt/headgt\n" print "lth2gtVisitors to
this formlt/h2gt\n" print "lthrgtltpregt\n" open
INFILE,"lt../data/guest.dat" die
"Could not open guest,dat\n" while (ltINFILEgt)
first _ chop(first) last
ltINFILEgt chop(last) print first,"
",last, "\n" print "lt/htmlgt\n"
10
access.cgi !/usr/bin/perl print "Content-type
text/html\n\n" print "lthtmlgt\n" print
"ltheadgtltTtitlegtRetrieving Data from a
Filelt/titlegtlt/headgt\n" print "ltbodygt\n" open
ACCESSFILE,"lt..data//access.dat" accesses
ltACCESSFILEgt print "The number of visitors to
this form accesses" print "lt/bodygt" print
"lt/htmlgt\n" If time pizza.htm
Write a Comment
User Comments (0)
About PowerShow.com