Intro 1

Spring 2002, Special Note.
Due to technical difficulties, the following pages have been recovered and are being restored, but there are lots of errors, especially concerning gif files, so please, bear with me while they are repaired.
Thanks
As you go through this material, a note concerning errors you discover would be greatly appreciated. I can be reached at carl.david@uconn.edu.
Carl David
March 4, 2002
First, we need to set up the physical situation for you. I work at a terminal which is directly connected to the Internet. The server, which you are accessing right now, is somewhere else. I actually have never seen it. From my desk, I use telnet to get access to the server, using a window on my screen as a port into the server. I login to the server, and edit files there.
In another window on my screen, I bring up a browser, and with that browser, I access the same file I am editing in the first window. This allows we to see what I am doing in terms of the finished product. Said another way, this allows me to see what my students will see.
On the local terminalon the servercomments
Open a window for telnet
telnet to the server
login accountuse my account
change directories to cgi-binor below if necessary
vi Perl.fileif Perl.file doesn't exist, create it
input a question as a Perl.fileThe material typed here(*) is the stuff of this book.
ZZ the Perl filesave the file and exit vi
chmod a+x Perl.fileMake Perl.file executable
Open browser
Open in browser appropriate URLhttp://www.something.edu/~account/cgi-bin/Perl.file
The text which one enters(*) into the vi window follows below.
If you go back to the Introduction screen, and view the source, you will see a complicated text. This text is the result of a program which was written in Perl. Notice that the Document URL is, aside from directory information (more on that later), `intro.pl' which indicates that this is a Perl script (we denote Perl scripts with the `.pl' suffix).
So, we write Perl scripts which themselves create HTML which is ``printed'', i.e., sent to the server's standard output device (called STDOUT in Unix) which happens, when the server is running as a server, to be your remote browser.
It is important to understand what you are doing here, you are creating HTML code ``on the fly''. This compares to creating an HTML file, one which contains both mark up items and text. Here, we generate both on the fly. Why?
When the student answers, we need to process his answer, and to do that we need a processing language and scheme of some type. The WWW is admirably set up to accept characters, and our answer judging scheme, written in Perl, allows us to write different HTML output code depending on what the student has answered. That's all there is to the central idea, all the rest is embroidery.
What follows is the actual Perl script (for the opening page) itself, i.e., the thing you will be composing (at the bottom are some comments):
*
#!/usr/local/bin/perl -- -*- C -*-
use CGI;
$query = new CGI;
print $query->header;
print "<h1>Would you like to write Internet questions?</h1>";
print <<EOF;
So, you want to write questions (and examinations) for Internet usage,
which a student can browse, answer, and have graded.
OK.
What do you have to learn, how do you proceed etc., etc., etc.?
<p>
We will consider fill-in, multiple choice, and numeric response questions,
with either a choice line or a fill in value for the answer.
Let's say that you want a student to answer the question:
What is the sum of the first five integers?
Obviously, the answer is 1+2+3+4+5=15.
We can have the student fill in a text field:
EOF
$ans = 15;
print $query->start_form;
print "sum of first 5 integers is:",$query->textfield('ans1','',5,5);
$stu_ans1 = $query->param('ans1');
if ($stu_ans1 ne ''){
if (($stu_ans1 - $ans )**2 > 0.0) {
	print "<EM> Wrong</EM>
	<IMG SRC=../icons/checkno.gif>"}
 else {print "<EM> Right! </EM>
	<IMG SRC=../icons/check.gif>"};
};
print "<br>or we could have the student choose an answer from a list:";
print $query->scrolling_list('ans2',['1','2','3','5','10','15','20']);
$stu_ans2 = $query->param('ans2');
if ($stu_ans2 ne ''){
if (($stu_ans2 - $ans )**2 > 0.0) {
	print "<EM> Wrong</EM>
	<IMG SRC=../icons/checkno.gif>"}
 else {print "<EM> Right! </EM>
	<IMG SRC=../icons/check.gif>"};
};

print "<br>or we could have the student choose an answer from a group:";
print "<br>",$query->radio_group('ans3',['1','2','3','5','10','15','20'],'10');
$stu_ans3 = $query->param('ans3');
if ($stu_ans3 ne ''){
if (($stu_ans3 - $ans )**2 > 0.0) {
	print "<EM> Wrong</EM>
	<IMG SRC=../icons/checkno.gif>"}
 else {print "<EM> Right! </EM>
	<IMG SRC=../icons/check.gif>"};
};

print "<br>or we could have the student answer in scientific notation:";
print "<br>Choose your answer in scientific notation,
i.e., ±x.yzE{±ab}, and then press the submit query button:
", $query->scrolling_list('s1', ['+','-'],'+'), $query->popup_menu('x', ['1','2','3','4','5','6','7','8','9'],'1'), "<STRONG>.</STRONG> ", $query->popup_menu('y', ['0','1','2','3','4','5','6','7','8','9'],'0'), $query->popup_menu('z', ['0','1','2','3','4','5','6','7','8','9'],'0'), "<STRONG>E{</STRONG>", $query->scrolling_list('s2', ['+','-',],'+'), $query->popup_menu('xx', ['0','1','2','3','4','5','6','7','8','9'],'0'), $query->popup_menu('yy', ['0','1','2','3','4','5','6','7','8','9'],'0'), "<STRONG>}</STRONG>"; print "<p>Submission Buttons to send answer to computer:",$query->submit," or "; print $query->reset; print $query->end_form; print "<HR> "; if ($query->param) { print "<P><EM>Your answer, ", $query->param('s1'), $query->param('x'),".", $query->param('y'), $query->param('z'),"E{", $query->param('s2'), $query->param('xx'), $query->param('yy'),"}","</EM> "; $stu_ans = $query->param('x')+(10*$query->param('y')+$query->param('z'))/100; if ($query->param('s1') ne "+"){$stu_ans = - $stu_ans}; $power = $query->param('xx')*10+$query->param('yy'); if ($query->param('s2') ne "+"){$power = - $power}; $stu_ans = * (10 ** $power); $v = (($stu_ans - )**2 ); #print "<br> DEBUG **** $ans and $stu_ans and "; if ( $v - (0.01*(10**$power))**2 >= 0.0 ) {print "<EM>, was Wrong</EM>. <IMG SRC=../icons/checkno.gif>"} else {print "<EM>, was Right! </EM>. <IMG SRC=../icons/check.gif>" }; } print "<a href=intro1.pl>Continue on to next section (intro1)."; print $query->end_html;
Some comments on this listing.
  1. the icons' addresses are `absolute', and must be changed before this script can be used,
  2. if you view source on this particular screen, you will see that none of the html brackets, `<' and `>', are contained in the source, although they appear on the screen properly.
  3. in the same sense, all the dollar signs in the source, are really backslash-dollar signs (which you can not see in the source or the interpreted source)!
  4. Assuming your hone diretory is called home, and that you are set up in tghe standard manner, you want to save this program in /home/public-html/cgi-bin as a program_name.pl. THen you have to make it executable using chmod, i.e., chmod a+x from the cgi-bin directory.
  5. when you address this on a web browser, you would enter myserver.edu/~your_account_name/cgi-bin/program_name.pl.
What this means is that more care about what is being interpreted by Perl or HTML must be exercised (more about this later).

Meanwhile, notice right now that lots of what appears above does not appear on the screen when interpreted by the net browser. What that means is that `printing' puts stuff on the screen, in general, although that may be controlled by `if/then/else' constructs.
Further, notice that the little drawings (icons) are obtained by displaying image files (img src=../).
If you make your Perl files executable, then it is possible to do some elementary debugging on your terminal without going to the browser. In fact, if there are errors in your Perl script, the browser will not show anything, so it is important to remove the Perl errors before attempting to browse the file. To do this one issues the command

perl filename.pl
(assuming that filename.pl is the file's name created using vi). Alternatively, you cound type
filename.pl
if the first line of the file is
#!/usr/local/bin/perl -- -*- C -*-
and provided this line agrees with what your system administrator has done in installing Perl, Perl would execute your script (filename.pl).
In any case, usually, Perl will report errors, by line number, and it will be your job to find those errors and correct them. Finally, when all the Perl errors are found, instead of seeing error messages you will see
(offline mode: enter name=value pairs on standard input)
which will be as cryptic as a message can be. Your response should be
^D
where the `^' symbol is shorthand for holding down the control key on your terminal. What you will see, since you are STDOUT, is the HTML text being sent to STDOUT. You will be your own personal browser, in a sense.
At this point, point your browser at the same file, and see it come up, and start debugging the HTML (and the CGI.pm Perl) rather than the Perl. Good luck!

Do not start charging ahead creating material. From long, hard, unhappy experience, I can tell you that this will mean repairing what you've done, over and over again. Rather, make up a prototype or two as you proceed, and restrain yourself until near the end, when we've discussed all the parts needed. This, plus some defensive coding, will allow you to write code once, and except for your own errors, never have to change it again! What a blessing that would be. Just think, when you have two or three hundred questions, and you want to change them all in a certain way, what will you do?


Just as some food for thought, what if you wanted the student to enter 5*(5+1)/2? This is just n*(n+1)/2 for n=5, but you can see that the above coding will not suffice for such a complicated problem. We will need lots of tools which are not yet apparent, hence the recommendation that you hold off temporarily on creating your own courseware.
Continue on to next section (intro2).
Return to the last section (intro).
Return to the main book TOC.

The last time this file was changed (edited) was Tuesday, March 12, 20124