CS 330 Class 8 - PowerPoint PPT Presentation

1 / 9
About This Presentation
Title:

CS 330 Class 8

Description:

CS 330 Class 8 – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 10
Provided by: cshil
Category:
Tags: class | keys

less

Transcript and Presenter's Notes

Title: CS 330 Class 8


1
CS 330 Class 8
  • 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 regular expressions
  • More Perl

2
Review How data gets into a script
  • As built-in environment variables (env.cgi,
    env2.cgi)
  • Form fields are appended to the URL and passed in
    the
  • environment variable QUERY_STRING
  • 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
  • Multiple fields name/data pairs are separated by
    (form2.htm)
  • scripts/form2.cgi?NameFredFieldMath

3
  • 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"

4
  • 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 "lthrgtn"
  • 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/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
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
  • To replace pluses in form2.cgi user input with
    blanks
  • (field_name, value) split(//,pair)
  • value tr// /

7
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 ENVREMOTE_ADDR'
  • if (remotehost /10\.3/)
  • print "Location ../examples/local.htm",
    "\n\n"
  • else
  • print "Location ../examples/remote.htm",
    "\n\n"
  • server redirection
  • How is binding done in JavaScript?

8
Associative Arrays, Again
  • avggrd.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 avgrd.pl work correctly?

9
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 print '
student/grade /gradesstudent' total0 co
unt0 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