CSCI 4230.1
Internet Application Development
Fall 2001
Mid-Term ExaminationName: _________________________________
Time allowed: one hour and 20 minutes. Total score: 100%.
Open: text book, lecture notes, every file I wrote and posted in my Web page and your project assignments. No other materials are allowed.
Answer all questions. Turn in both question and answer sheets. Plan your time well.
Academic honesty policy will be followed strictly. Cheating will result in a failing grade and a permanent academic record. Your question paper will not be the same as your neighbor's.
(1) (20%) A number tree is defined as a Perl array where each argument is either a number or a reference to a number tree. For example,
@numTree = (1, [2, 3, 4, [5, [6, 7], 8, [9], 10]], 11, [12, 13]);
Write a Perl function sum(@numTree) to return the sum of all numbers in a number tree. For example, executing:
@num = (1, [2, 3, 4, [5, [6, 7], 8, [9], 10]], 11, [12, 13]);
print sum(@numTree);prints 91 (which is the sum from 1 to 13).
(2) (25%) Write a Perl program t1aqb.pl to read in a file of student records and print out student names categorized by majors. The input file name is obtained in the command line, for example:
t1aqb.pl t1aqbdat.txt
The file stores one record per line:
Major: columns 1-4
SS Number: columns 6-16
GPA: columns 19-22
Name: variable length starting at column 24.Names are always enclosed by a pair of double quotes (").
The program output is sent to the standard output stream to list all students in alphabetical order under majors.
For example, if the contents of the input file t1aqbdat.txt are:
CSCI 111-11-1112, 3.67 "Joseph Giarratano"
CSCI 222-33-4456, 2.67 "Mary Jones"
SWEN 666-66-6666, 2.56 "Alice Sean Eaton"
CSCI 444-44-4444, 3.33 "David Hart"
CSCI 222-33-4444, 3.44 "Susan King"
SWEN 666-66-6666, 2.33 "Albert Johnson"
BIOL 888-88-8888, 3,60 "Philip Elliot"
SWEN 777-77-7777, 3.88 "George Lee"the program should output exactly:
BIOL:
Philip Elliot
CSCI:
David Hart
Joseph Giarratano
Mary Jones
Susan King
SWEN:
Albert Johnson
Alice Sean Eaton
George Lee(3) (a) (7%) What will be printed, if any, when the following Perl statement is executed?
perl -e "$_ = 'aaabbaa'; /.*(ba*)/; print $1;"
(b) (8%) Consider the following URL:
http://dcm.uhcl.edu/yue/q3.pl:
The HTML code generated by this CGI program contains only a form:
<form method="get">
Name:
<input type="text" size="20" name="user">
<br>
<input type="submit" name="submit" value="submit">
</form>If the user types in "Bun Yue" as the name and clicks the submit button, what will be the URL request submitted?
(4) (40%) Fill in the missing parts of the CGI program t1aqd.pl, which allows a user to enter an URL. The program checks whether the URL is broken. If it is a good link, it is added to the end of the file t1aq4.txt. If not, an error message is given. The list of all URL in the file is then displayed. No error handling capability is needed.
For example, initially, the program may display (http://dcm.uhcl.edu/yue added earlier):
![]()
After the user submits the URL http://cs.stanford.edu/Degrees/, which is a good link, the program displays:
![]()
If the user submits a broken link http://dcm.uhcl.edu/nosuchuser:
![]()
the program displays:
![]()
The skeleton of the program is given below. You only need to give the definitions of the functions processURL and showForm. Do not write a separate program yourself.
use strict;
use CGI;
use LWP::UserAgent;
$|++;# Global porting variables.
my $filename = "t1aq4dat.txt";# Constants:
my $URL_PARAM = "url";my $q = new CGI;
# Get user parameters: url.
my $url = $q->param($URL_PARAM);# Print response header and starting HTML.
print $q->header,
$q->start_html(-title => "URL lister");if ($url) {
# Process the URL. If it is good, append it
# to the end of the file. If not, print
# error messages.
processURL($url);
}
else {
# No initial parameter. Print the form to
# get url parameter
showForm();
}
# Show the result of the URL.
showURL();
print $q->end_html();
exit 0; # main# Show the URL stored. Use the global variable $filename.
sub showURL {
open IN, "<$filename" ||
die "Sorry, can't open file $filename.";
print "<h3>URL stored:</h3>\n<ul>\n";
while ($_ = <IN>) {
chomp;
print "<li><a href=\"$_\">$_</a></li>\n";
}
print "</ul>\n";
close IN;
} # Show URL.######
### Your job is to give the definitions of the
### subroutines processURL and showForm.
######