Reading and adding to a pre-existing student password file.
We will give the teacher the choice of either creating password files herself
or having the computer do it dynamically as the class slowly logs on.
Therefore, our login program will require that we read the old student password
file, and then give the student a choice about using one of the existing names
on the class role, or loging in as a new student.
In the following, we read the old file, and then put up a scrolling list
of the existing members of the class as well as a text field which allows students
to log in as new students (We will handle guests in a different manner).
Here is the code:
#!/usr/local/bin/perl -- -*- C -*-
use CGI;
$query = new CGI;
print $query->header;
print $query->start_html('read_and_add.pl');
print "This program reads the old student file, creates a scrolling list, and asks you to either choose one (i.e. old student) or input a name (i.e. new student).
";
#last_name+first_name+password, where '+' is the delimter
$filename = "students_fall_96.dat";
open ( STUD_FILE , $filename) ;
$i = 0;
while(){
chop;#we are here assuming that there will be a carriage return
# at the end of each line, i.e., that we create the file using
# an editor.
# We may need to change this, since we can use students to
# self-register.
$single_student[$i] = $_;
if($single_student[$i] ne ''){
$p1 = index($single_student[$i],'+');
$last_name[$i] = substr($single_student[$i],0,$p1);
$p2 = index($single_student[$i],'+',p1+1);
$first_name[$i] = substr($single_student[$i],$p1+1,$p2-1);
$password[$i] = substr($single_student[$i],$p2+1);
#change from first, simplified incrementing:
$i++;
};
};
$i--;
close (STUD_FILE);
print $query->start_form;
print $query->scrolling_list('student_list', \@last_name);
print "
choose from above, or enter below:
";
print "Last Name = ",$query->textfield('new_student_last_name','',20,40);
print "First Name = ",$query->textfield('new_student_first_name','',20,40);
print $query->submit;
print $query->end_form;
print $query->end_html;
The important lines here are:
print $query->start_form;
print $query->scrolling_list('student_list', \@last_name);
print "
choose from above, or enter below:
";
print "Last Name = ",$query->textfield('new_student_last_name','',20,40);
print "First Name = ",$query->textfield('new_student_first_name','',20,40);
print $query->submit;
print $query->end_form;
which is the coding which contains the request for student input.
We have three input fields, 'student_list', 'new_student_last_name',
and 'new_student_first_name', and we will have to get values of these variables
and process them, but suffice it to (here) say that the screen looks good.
You can
see the screen but notice that when you
choose a student, nothing is done with this information.
In the above coding, @last_name is a LIST of the various last names
which were read in from the external file.
No matter how long the file is, i.e., how many students there are in the class,
we will be "OK", although perhaps we should limit the number of students in
some way to make the list manageable.
@last_name contains ('stalin','breznev'), and will contain
('stalin','breznev','new student last name') when we are done; then
we will write out all the names to the student file, so that this new student
need never login again by typing her name, but instead can find herself on
the existing class role (amongst the communists).
Typing \@last_name guarantees that the
@ sign is present in the call, i.e., that the textfield calls a list of
last names!
It should be immediately apparent that we need to prevent the student from
signing in using both methods.
Thus, we need to force the student to either one choice or another.
There are various strategies for doing this, we choose a radio_group.
#!/usr/local/bin/perl -- -*- C -*-
use CGI;
$query = new CGI;
print $query->header;
print $query->start_html('read_and_add_choose.pl');
print "This program reads the old student file, creates a scrolling list, and asks you to either choose one (i.e. old student) or input a name (i.e. new student).
";
#last_name+first_name+password, where '+' is the delimter
$filename = "students_fall_96.dat";
open ( STUD_FILE , $filename) ;
$i = 0;
while(){
chop;#we are here assuming that there will be a carriage return
# at the end of each line, i.e., that we create the file using
# an editor.
# We may need to change this, since we can use students to
# self-register.
$single_student[$i] = $_;
if($single_student[$i] ne ''){
$p1 = index($single_student[$i],'+');
$last_name[$i] = substr($single_student[$i],0,$p1);
$p2 = index($single_student[$i],'+',p1+1);
$first_name[$i] = substr($single_student[$i],$p1+1,$p2-1);
$password[$i] = substr($single_student[$i],$p2+1);
#change from first, simplified incrementing:
$i++;
};
};
$i--;
close (STUD_FILE);
print $query->start_form;
print $query->radio_group('choice_of_login_mode',['from list','adding new student'],'from list','true');
print $query->scrolling_list('student_list', @last_name);
print "
choose from above, or enter below:
";
print "Last Name = ",$query->textfield('new_student_last_name','',20,40);
print "First Name = ",$query->textfield('new_student_first_name','',20,40);
print $query->submit;
print $query->end_form;
$which_radio_button = $query->param('choice_of_login_mode');
print $which_radio_button;
print $query->end_html;
You can
see the screen, and when you `Submit Query',
you will see that either `from list' or `adding new student' is printed.