Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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?Name=Fred&Age=25 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?First+Name=xx ? 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 " \n"; print " Simple Form Processing \n"; print " \n"; print ”Query string: ",$ENV{'QUERY_STRING'}," \n\n"; ($field_name, $data) = split (/=/,$ENV{'QUERY_STRING'}); print "The field: ",$field_name, "\n"; print "The data: ",$data, " \n"; print " \n";

5 Perl - Identifiers $Name: a scalar variable ($field_name in form1) @Name: an array (@pairs in form2) @ is the array, $ an element @Name = (“mary”, “fred”, “joe”) $Name(0) has the value “mary” %ENV: an associative array Strings for subscripts, not integers: $ENV{‘SERVER_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?firstName=Fred&lastName=Smith... 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 " \n"; print " Form Processing 2 \n"; print " Form Processing, Multiple Inputs \n"; print " \n"; print " Query string: ",$ENV{'QUERY_STRING'},"\n\n"; print " Field Value \n"; @pairs = split (/&/, $ENV{'QUERY_STRING'}); foreach $pair (@pairs){ ($field_name, $value) = split(/=/,$pair); print $field_name," ",$value,"\n"; } print " \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 = $ENV{’HTTP_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 $grades{‘key’} 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 = ){ #read one line and store in $line chop($line); #gets rid of CR ($student, $grade) = split(" ", $line); #split $line at the first blank space $grades{$student} =$grade; #$grades is the assoc.array with key $student print "$student $grade $grades{$student}\n"; } print "\nThe data as stored:\n"; #print name/grade pairs from the associative array $total=0; foreach $stud (keys(%grades)) { #$stud is one of the keys print "$stud: $grades{$stud}\n"; $total += $grades{$stud}; $count++; } $average = $total / $count; print "The average of the grades is $average\n";


Download ppt "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."

Similar presentations


Ads by Google