Perl/CGI Exercises for 95.205
(Last update: 9:30 EST, 23 Feb 2002)
Exercise 3:
Log in to apex from home, with the local port 8000 tunnelled to the remote
port sigma10:8000 (instructions can be found here).
Secure shell to one of the sigma machines {sigma01, sigma02, ..., sigma09,
sigma11, sigma12, ..., sigma20}, by typing the following:
ssh sigma17
(this lets you run perl programs from the command line; this is a feature
disabled from apex since everyone logs in there)
Create a new directory called "course_html" off the root of your SCS
account.
Execute "chmod 0711 course_html" – this makes your directory externally
visible.
Create a new html file called "hello.cgi" in your SCS course_html directory.
Edit "hello.cgi" to have the following content: #!/usr/local/bin/perl -w
use strict;
print "Content-Type: text/html\n\n";
print <<HERE;
<html>
<head>
<title>Hello, World! web page</title>
</head>
<body>
<h2>Hello, World!<h2>
</body>
</html>
HERE
Exit your UNIX editor, and execute "chmod 0755 hello.cgi" to make your file
executable.
Run your CGI script from the command line, by typing: "hello.cgi" in full.
You have to type the whole filename on the first execution because your shell
only updates it’s tab-completion list after that first execution. (BTW, the tab
key will try to auto-complete the filename that you’re typing in Linux. So you
can type "hell<TAB>", and the C shell will complete the text to
"hello.cgi".)
While PuTTY is still open, type "http://localhost:8000/~myaccount/hello.cgi"
into your web browser, and see your first CGI script executing at school from
home. Note that PuTTY needs to be open for this to work, as it’s providing the
tunnelling support for your local port 8000.
Exercise 4:
Make an HTML page in your course_html directory that has a form element with
a submit button, and some local data. Something like the following will do: <html>
<head>
<title>CGI example</title>
</head>
<body>
<form action="respondToForm.cgi" method="GET">
<p>Enter your name:</p>
<input type="text" name="name">
<p>Enter your friend's name:</p>
<input type="text" name="friend">
<input type="submit" value="Go, baby!">
</form>
</body>
</html>
Save this file as "cgiExample.html" in your course_html directory, and make
it externally visible. Note that it needs to be put in your course_html
directory rather than your public_html directory, since it will call a CGI
script, which will only run from your course_html directory.
Save the following CGI script as "respondToForm.cgi": #!/usr/local/bin/perl -w
use strict;
print "Content-Type: text/html\n\n";
my $postString = <STDIN>;
print <<HERE;
<html>
<head>
<title>Form response web page</title>
</head>
<body>
<h2>Hello, World!<h2>
<p>My parameters from GET are: $ENV{QUERY_STRING}.</p>
<p>My parameters from POST are: $postString.</p>
</body>
</html>
HERE
Change the permissions of "respondToForm.cgi" with "chmod 0755
respondToForm.cgi".
Try form data access by typing
"http://localhost:8000/~myaccount/cgiExample.html" into your browser, fill in
the fields, then clicking on "Go, baby!".
Exercise 5:
The data comes into our form; but all as one big string. It would be nice if
we could parse it. Write perl code to split the input source line on "&" to
make several strings of the form "key=value". Write the response as HTML and
return the split strings to the user as a CGI script.
Exercise 5:
Edit the above HTML file to use "POST" to submit your form.
Exercise 6:
Make a new HTML file like the above with two different form elements, one
which uses "GET" to submit data; one which uses "POST" to submit your form. Send
it to the same CGI script as the above.
Exercise 7:
Make a new HTML file like the above with two different form elements, one
which uses "GET" to submit data; one which uses "POST" to submit your form. Send
them to two different CGI scripts, similar to the above. Make one script
"parseGet.cgi" only parse GET information, and make the other "parsePost.cgi"
only parse POST information.
Exercise 8:
Try switching the two inputs (GET to parsePost.cgi, POST to parseGet.cgi)
and see what happens.
Exercise 9:
Okay, so the data comes into our form; but it would be nice if we could
parse it. Write perl code to split the input source line on "&" to make
several strings of the form "key=value". Store them in a perl array. Write the
response as paragraph text (delimited by <p>, </p>
) and return the split strings to the user as a
CGI script.
Exercise 10:
Let’s make the output pretty: write the response as an HTML table and return
the split strings to the user as a CGI script.
Hint: If you add the line: local $_= "</td> </tr> <tr> <td>";
...to the start of your script, you will be able to output the whole table
using array string interpolation by writing: print "<table> <tr> <td> @array </td> </tr> </table>";
... or the equivalent. Or you can output the array the long way.
Exercise 11:
Output the array the long way, using a foreach loop.
Exercise 12:
Output the array the REALLY long way, using a for (my $j = 0; $j != scalar @array; $j++) { ... }
... loop.
Exercise 13:
Okay, now we have some key-value pairs. Split them into keys and values
using another split command, and output the keys and values as a 2-dimensional
HTML table.
Exercise 14:
Split them into keys and values as before, and store the key-value pairs in
a hash. Then iterate through the hash (using each) and output into a table. Note
that the output order is unusual (easier to see if you add lots of input fields
to your form.)
Exercise 15:
Iterate through the hash (using foreach my $key (sort keys %hash)
) and output into a table. Note that
the output order is unusual (easier to see if you add lots of stuff to your form
elements.)