I've written a perl program to quiz myself using a simple text file as
input.

Input file:

answer = question
answer = question
etc...

When I try to convert this program to a cgi program to use from a web
browser, it will not pass the parameters back to the program after
drawing the page.  I've tried saving the parameter to a variable.  I
even tried pulling it from the param array, which it appears in.

The program starts and presents a menu of test chapters, then after user
selection, it exits the program as if I finished the exam.  

Could someone give me some guidance?

Thank you,

Jeff Borders

http://www.jeffborders.com/cgi-bin/korean-cgi.pl

Listing below:

************************************************

#!/usr/bin/perl

use warnings;
use CGI qw/-debug :standard/;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Pretty qw(:html3);

print header,
        start_html('Korean Terminology'),
        h1('Korean Terminology'),
        start_form, 
        p,hr,p;

foreach $name ( param() ) {
        $value = param($name);
        print "The value of $name is $value\n"
        }

$test=param('test');

if (param()) {
        if      ($test eq "9th Gup") { $test="9gup.txt"; }
        elsif   ($test eq "8th Gup") { $test="8gup.txt"; }
        elsif   ($test eq "7th Gup") { $test="7gup.txt"; }
        elsif   ($test eq "6th Gup") { $test="6gup.txt"; }
        elsif   ($test eq "5th Gup") { $test="5gup.txt"; }
        elsif   ($test eq "4th Gup") { $test="4gup.txt"; }
        elsif   ($test eq "3rd Gup") { $test="3gup.txt"; }
        elsif   ($test eq "2nd Gup") { $test="2gup.txt"; }
        elsif   ($test eq "1st Gup") { $test="1gup.txt"; }
        elsif   ($test eq "1st Dan") { $test="1dan.txt"; }
        else    { print "no such test name"; }

        &main_section();
        
        }
else    {
        print h2('Choose test:');
        &choose_test();
        &process_test();
        }


print p,hr,p;
print end_form, end_html;


sub choose_test 
{
print radio_group(-name=>'test', -value=>['9th Gup','8th Gup','7th
Gup','6th Gup','5th Gup','4th Gup','3rd Gup','2nd Gup','1st Gup','1st
Dan'], -cols=>4);
print p;
print submit(-name=>'test',-label=>'Enter'),
reset(-name=>'reset',-label=>'Cancel');
print p;
$test=param('test');
return $test;
}

sub process_test
{
        print p;
        print "test = $test";
        print p;
        print param('test');
        $number_of_questions=0;
        open (DICT, $test) || die "Cannot open '$test' $!";
        while (<DICT>) { 
                chomp;
                $number_of_questions++;
                ($answer, $question) = split /\s*=\s*/;
                $answer[$number_of_questions] = $answer;
                $question[$number_of_questions] = $question;
                }
        close DICT;
        print p;
        print "number_of_questions = $number_of_questions\n";
        return $number_of_questions;
}

sub main_section
        {
        $total=0;
        $total++;
        while ($total <= $number_of_questions || $response eq 'Cancel')
# || $total > $number_of_questions) 
                {
                &pick_question();
                &pick_order();
                &print_question();
                &get_response();
                }
        &print_result();
        print p; print "\ntotal number of questions = $number_of_questions\n";
        print p; print "\ntotal = $total\n";

        }

sub pick_question
        { #Pick a unique question from list
        do {
        $random_question = 1 + int(rand($number_of_questions));
        } while ( grep (/$random_question/, @answered) );
        push @answered, $random_question;
        return $random_question;
        }

sub pick_order
        { #Pick which position to insert the correct answer.
        $random_order = 1 + int(rand(4));       #returns an integer from (0..3) 
+ 1. 
        return $random_order;
        }

sub print_question 
        {
        print start_form,
        "Question number $total of $number_of_questions",hr,p,"$question
[$random_question]",p;  #Print the question.
        my @temp_answers;
        my $temp_answer;
        push @temp_answers, $random_question;    #This keeps the answer from
showing up twice on the same question.
        for my $i (1..4)   #Print the possible answers.
                {
                if ($i == $random_order) 
                        {
                        #print correct value from array.
                        push @temp_answers, $answer[$random_question];
                        } 
                else
                        {
                                ### Make sure that bogus answer does not equal 
answer or other 2
bogus answers ###
                                do 
                                {
                                $temp_answer =  1 + 
int(rand($number_of_questions));
                                } while ( grep (/$temp_answer/, @temp_answers) 
);
                        #print incorrect value from array.
                        push @temp_answers, $answer[$temp_answer];
                        }
                }
        print radio_group(-name=>'answer', -value=>[EMAIL PROTECTED], -cols=>1);
        $answers=param('answer');
        print p;
        print submit(-name=>'answer',-label=>'Enter'),
        reset(-name=>'reset',-label=>'Cancel');
        print p;
        print end_form;
        }

sub get_response 
        {
        $response=param('answer');
        if ($response eq 'Cancel') 
                { 
                print p; 
                $total--;
                } 
        elsif ($response != $random_order) 
                {       #print correct value from array.
                print "-----> $question[$random_question] is $answer
[$random_question] <-----"; 
                print p;
                $incorrect++; 
                }       
        return $response;
        return $incorrect;
        }

sub print_result
{
  print p,hr,p;
  if ($total == 0) { 
        print "No questions answered.";
        }
  else
        {
        my $percent=eval (($total-$incorrect)/$total)*100;
        printf "Out of %2d questions, you missed %2d for a grade of %3d%%",
$total, $incorrect, $percent;
        print p;
        print "You answered $total questions";
        }
}







-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to