[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Perl Problems



I'm working through a Perl tutorial, and I am having problems with
one of the sample problems.  I figured out most of Perl's error
messages, but the remaining FIFTY or so are driving me crazy.  Would
someone please critique the following, and tell me what the @#$%^&*!
interpreter is trying to say.

----------------------------------------------------------------------
#! /usr/bin/perl -w

use strict;

# declare variables

  my $first_integer  # random between 0 and 9
  my $second_integer  # random between 1 and 9
  my $solution  # product of integers
  my $question	  # function of integers and solution
  my $coin_toss  # 1 for multiplication, 0 for division
  my $response  # user's response to question
  my $quit  # t/f for "quit"
  my $validity  # t/f for valid characters
  my $count_correct  # No. of correct answers
  my $count_incorrect # No. of incorrect answers

# begin quiz!

  print "This is Math Quiz!\n" ;
  $quit = 0 ;
  until ($quit) {

  # formulate a question

    $first_integer = int(rand(10)) ;
    $second_integer = int(rand(9)) + 1 ;
    $solution = $first_integer * $second_integer ;
    $coin_toss = int(rand(2)) ;

    if ($coin_toss == 1) {
       $question = "$first_integer * $second_integer = ?" ;

    }  else {
       ($first_integer, $solution) = ($solution, $first_integer) ;
       $question = "$first_integer / $second_integer = ?" ;
    }

  # display question and prompt for a valid response

    $validity = 0 ;
    until ($validity) {
      print "$question\n" ;
      chomp ($response = <STDIN>) ;

      if ($response eq 'q' or response -= m/^\d+$/) {
         $validity = 1 ; 

      }  else {
         print "Enter a number or a 'q' to quit\n" ;
      }
    }

  # act on the response

      if ($response eq 'q') {
         $quit = 1 ;

      }  elsif ($response == solution) { 
         print "You are correct!\n" ;
         $count_correct += 1 ;

      }  else {
         print "Incorrect! $question $solution\n" ;
         $count_incorrect += 1 ;
      }

# exit quiz!

  }
  Print "You gave $count_correct correct answers and $count_incorrect incorrect answers.\n" ; 
  print "Bye-bye!\n" ;

----------------------------------------------------------------------

"my" variable $quit masks earlier declaration in same scope at ./math_quiz line 22.
"my" variable $quit masks earlier declaration in same scope at ./math_quiz line 23.
"my" variable $first_integer masks earlier declaration in same scope at ./math_quiz line 27.
"my" variable $solution masks earlier declaration in same scope at ./math_quiz line 29.
"my" variable $first_integer masks earlier declaration in same scope at ./math_quiz line 29.
"my" variable $second_integer masks earlier declaration in same scope at ./math_quiz line 29.
"my" variable $coin_toss masks earlier declaration in same scope at ./math_quiz line 30.
"my" variable $coin_toss masks earlier declaration in same scope at ./math_quiz line 32.
"my" variable $question masks earlier declaration in same scope at ./math_quiz line 33.
"my" variable $first_integer masks earlier declaration in same scope at ./math_quiz line 33.
"my" variable $second_integer masks earlier declaration in same scope at ./math_quiz line 33.
"my" variable $first_integer masks earlier declaration in same scope at ./math_quiz line 36.
"my" variable $solution masks earlier declaration in same scope at ./math_quiz line 36.
"my" variable $solution masks earlier declaration in same statement at ./math_quiz line 36.
"my" variable $first_integer masks earlier declaration in same statement at ./math_quiz line 36.
"my" variable $question masks earlier declaration in same scope at ./math_quiz line 37.
"my" variable $first_integer masks earlier declaration in same scope at ./math_quiz line 37.
"my" variable $second_integer masks earlier declaration in same scope at ./math_quiz line 37.
"my" variable $validity masks earlier declaration in same scope at ./math_quiz line 42.
"my" variable $validity masks earlier declaration in same scope at ./math_quiz line 43.
"my" variable $question masks earlier declaration in same scope at ./math_quiz line 44.
"my" variable $response masks earlier declaration in same scope at ./math_quiz line 45.
"my" variable $response masks earlier declaration in same scope at ./math_quiz line 47.
"my" variable $validity masks earlier declaration in same scope at ./math_quiz line 48.
"my" variable $response masks earlier declaration in same scope at ./math_quiz line 57.
"my" variable $quit masks earlier declaration in same scope at ./math_quiz line 58.
"my" variable $response masks earlier declaration in same scope at ./math_quiz line 60.
"my" variable $count_correct masks earlier declaration in same scope at ./math_quiz line 62.
"my" variable $question masks earlier declaration in same scope at ./math_quiz line 65.
"my" variable $solution masks earlier declaration in same scope at ./math_quiz line 65.
"my" variable $count_incorrect masks earlier declaration in same scope at ./math_quiz line 66.
String found where operator expected at ./math_quiz line 72, near "Print "You gave $count_correct correct answers and $count_incorrect incorrect answers.\n""
	(Do you need to predeclare Print?)
syntax error at ./math_quiz line 8, near "$first_integer	# random between 0 and 9
  my "
Global symbol "$second_integer" requires explicit package name at ./math_quiz line 9.
syntax error at ./math_quiz line 72, near "Print "You gave $count_correct correct answers and $count_incorrect incorrect answers.\n""
Bareword "response" not allowed while "strict subs" in use at ./math_quiz line 47.
Bareword "solution" not allowed while "strict subs" in use at ./math_quiz line 57.
Execution of ./math_quiz aborted due to compilation errors.

-
To unsubscribe, send email to majordomo@silug.org with
"unsubscribe silug-discuss" in the body.