CS 330 Class 9 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS 330 Class 9

Description:

CS 330 Class 9 – PowerPoint PPT presentation

Number of Views:26
Avg rating:3.0/5.0
Slides: 12
Provided by: cshil
Category:
Tags: class | locator | make | pattern | store

less

Transcript and Presenter's Notes

Title: CS 330 Class 9


1
CS 330 Class 9
  • Programming plan for today
  • More of how data gets into a script
  • Via environment variables
  • Via the url
  • From a form
  • By editing the url directly
  • More perl

2
Review How data gets into a script
  • Client passes data to the server as environment
    variables
  • Client passes data to the server as part of the
    URL
  • Similar to data format in results from sendmail
  • scripts/guest.cgi?NameFredAge25
  • Server passes data to the script. Script accesses
    it via an environment variables.
  • env.cgi, env2.cgi, form1.cgi

3
Review Input from a Form GET
  • Form fields are appended to the URL and passed in
    the environment variable QUERY_STRING
  • http//aurora.wells.edu/cs330/scripts/form1.
    cgi?FirstNamexx
  • ? Indicates beginning of the data
  • Separates the input name from the input
    value
  • Call form1.cgi from cs330 page
  • Call form1.cgi by editing the URL

4
  • form1.cgi
  • !/usr/bin/perl
  • print "Content-type text/html\n\n"
  • print "lthtmlgt\n"
  • print "ltheadgtlttitlegtSimple Form
    Processinglt/titlegt lt/headgt\n"
  • print "lth2gtSimple Form Processinglt/h2gt\n"
  • print "lthrgtltpregt\n"
  • print Query string ",ENV'QUERY_STRING',"ltB
    Rgt\n\n"
  • (field_name, data) split (//,ENV'QUERY_STRI
    NG')
  • print "The field ",field_name, "\n"
  • print "The data ",data, "ltBRgt\n"
  • print "lthrgtlt/pregtlt/htmlgt\n"

5
Perl - Identifiers
  • Name a scalar variable (field_name in
    form1)
  • _at_Name an array (_at_pairs in
    form2)
  • _at_ is the array, an element
  • _at_Name (mary, fred, joe)
  • Name(0) has the value mary
  • ENV an associative array
  • Strings for subscripts, not integers
    ENVSERVER_NAME
  • The subscripts (SERVER_NAME) are called keys
  • NAME a filehandle (later)
  • like inFile in C. Convention capitalize
    filehandles

6
Forms with multiple fields
  • Name/data pairs are separated by
  • /form2.cgi?firstNameFredlastNameSmith...
  • The list of pairs is passed in environment
    variable QUERY_STRING
  • The CGI program has to unpack the pairs
  • See form2.htm and form2a.cgi

7
  • form2.cgi
  • !/usr/bin/perl
  • print "Content-type text/html\n\n"
  • print "lthtmlgt\n"
  • print "ltheadgtltTITLEgtForm Processing
    2lt/titlegtlt/headgt\n"
  • print "lth2gtForm Processing, Multiple
    Inputslt/h2gt\n"
  • print "lthrgtltpregt\n"
  • print "ltbgtQuery stringlt/bgt",ENV'QUERY_STRING',
    "\n\n"
  • print "ltbgtField Valuelt/bgt\n"
  • _at_pairs split (//, ENV'QUERY_STRING')
  • foreach pair (_at_pairs)
  • (field_name, value) split(//,pair)
  • print field_name," ",value,"\n"
  • print "lthrgtlt/pregtlt/htmlgt\n"

8
More Regular Expressions
  • Split
  • split(/pattern/, expr) separate expr at the
    first occurrence of pattern,

  • and return the pair of substrings
  • Translate tr /search list/repl. list/
  • Replace each character in search list
  • name tr// / replace in name by
    a space
  • name tr/A-z/a-z/ make lowercase
  • form2a.htm
  • (field_name, val) split(//,pair)
  • val tr// /

9
More Regular Expressions
  • Substitute s/pattern/repl. pattern/
  • Replaces a pattern in a search list
  • name s/henry\.wells/aurora\.wells/
  • Binding string /pattern/
  • True if pattern is in string
  • restrict.cgi
  • !/usr/bin/perl
  • remotehost ENVHTTP_HOST'
  • if (remotehost /\.wells\.edu/)
  • print "Location ../examples/local.htm",
    "\n\n"
  • print Location .. called server
    redirection

10
Associative Arrays, Again
  • avggrade.pl read name/grade pairs into an
    associative array
  • grades is the whole array
  • keys(grades) is the set of all keys
  • gradeskey is the value associated with the
    key key
  • Does this work correctly?

11
avggrd.pl !/usr/bin/perl open GRADES,
"grades.dat" file handler
variable is GRADES die "Could not open
grades.dat\n" print "The data as
entered\n" while (line ltGRADESgt)
read one line and store in line
chop(line)
gets rid of CR (student, grade)
split(" ", line) split line at the first
blank space gradesstudent grade
grades is the assoc.array with key
student print "student grade
gradesstudent\n" print "\nThe data as
stored\n" print name/grade pairs
from the associative array total0 foreach
stud (keys(grades)) stud is one of
the keys print "stud gradesstud\n"
total gradesstud count
average total / count print "The average
of the grades is average\n"
Write a Comment
User Comments (0)
About PowerShow.com