The Elite Noob has asked for the wisdom of the Perl Monks concerning the following question:

Hiya! I have a syntax error. But i don't know whats causing it. Can anyone help? Heres the offending code.
#!/usr/bin/perl -w # ticTacToe.pl use warnings; use strict; my @pieces = (" ") x 9; # value of square. NULL if empty x for x and o + for o. my $input; my $outcome; # 1 for win, 2 for loss. 3 for Tie. my $playerPiece; my $aiPiece; my @markers = ("o", "x"); my $totalMarkers = @markers; sub updateScreen{ # To update screen after each move. print " $pieces[6] | $pieces[7] | $pieces[8]\n"; print "-----------\n"; print " $pieces[3] | $pieces[4] | $pieces[5]\n"; print "-----------\n"; print " $pieces[0] | $pieces[1] | $pieces[2]\n"; } sub helpGame{ } system("reset"); # reset screen. print "Welcome to my Tic Tac Toe Game!\n"; print "I hope you like it and have fun\n"; print "BTW, you can't choose who you are...\n"; print "Type quit to quit program. Help for help.\n"; $playerPiece = $markers[rand $totalMarkers]# randomly choose x or o fo +r player. if($playerPiece eq "x"){ $aiPiece = "o"; } else { $aiPiece = "x"; }; print "The player is = $playerPiece\n"; print "the Ai is = $aiPiece\n"; while($outcome == 0){ # main processing loop print "\n"; &updateScreen; print "\n"; $input = <STDIN>; if($input =~ /[^1-9]/){ # Used to check for input words. if($input eq("quit" || "exit")){ # Checks what word is. print "\n"; print "Bye! Hope you liked it!\n"; print "\n"; exit; } else if($input eq "help") { &helpGame; redo; } else { print "\n"; print "That is invalid input...\n"; redo; } } if($input =~ /[1-9]/){ print "\n"; } }
And here are the errors i get.
syntax error at ticTacToe.pl line 33, near "){"
syntax error at ticTacToe.pl line 53, near "else if"
syntax error at ticTacToe.pl line 65, near "}"
Execution of ticTacToe.pl aborted due to compilation errors.

Thanks for help! Its a tic tac toe system btw.

Replies are listed 'Best First'.
Re: Simple problem, cant fix?
by Corion (Patriarch) on Feb 27, 2011 at 19:56 UTC

    This is basically the same error as Simple Syntax Error? Can't Find?. You'll find that there are far more errors to discover when programming. Don't spend so much time repeating and repeatedly debugging the same old errors when there is so much time to discover new errors. Learn to quickly deduce (and resolve) errors from the error message, so that you can have more fun with the errors that don't even have their own message :)

Re: Simple problem, cant fix?
by Ratazong (Monsignor) on Feb 27, 2011 at 19:49 UTC
    $playerPiece = $markers[rand $totalMarkers]# randomly choose x or o fo +r player.

    A semicolon is missing after the "]".

    btw.: it would be helpful if you mark the lines with the error with a comment e.g. # <- this is line 33

Re: Simple problem, cant fix?
by toolic (Bishop) on Feb 27, 2011 at 19:52 UTC
    $playerPiece = $markers[rand $totalMarkers]# randomly choose x or o fo +r player.
    Add a semicolon before the #.
    else if
    Change it to "elsif".

    Not a syntax error, but you probably should change:

    if($input eq("quit" || "exit")){ # Checks what word is.
    to:
    if($input eq "quit" || $input eq "exit")){ # Checks what word is.
Re: Simple problem, cant fix?
by cdarke (Prior) on Feb 27, 2011 at 21:56 UTC
    The most common cause of a syntax error I see in the classroom is a missing semi-colon on the line before the one reported.

    By the way, do your tests against $input work? Hint: it's good to chomp.

    Also:
    if($input =~ /[^1-9]/){ # Used to check for input words. ... } if($input =~ /[1-9]/){ print "\n"; }
    Is probably better written as:
    if($input =~ /[^1-9]/){ # Used to check for input words. ... } else { print "\n"; }
    You don't really want to do more regular expression matches than you have to.
Re: Simple problem, cant fix?
by AnomalousMonk (Archbishop) on Feb 27, 2011 at 21:04 UTC

    The usediagnostics; pragma can also be helpful to beginners. See, in particular, the second reason in the "Common reasons" list. (Sorry for all the line-wrap.)

    >perl -le "use warnings; use strict; use diagnostics; ;; my $string = 'no semicolon at end of this statement' if (1 > 0) { print 'ok' } " syntax error at -e line 1, near ") {" Execution of -e aborted due to compilation errors (#1) (F) Probably means you had a syntax error. Common reasons include +: A keyword is misspelled. A semicolon is missing. A comma is missing. An opening or closing parenthesis is missing. An opening or closing brace is missing. A closing quote is missing. Often there will be another error message associated with the synt +ax error giving more information. (Sometimes it helps to turn on -w. +) The error message itself often tells you where it was in the line +when it decided to give up. Sometimes the actual error is several toke +ns before this, because Perl is good at understanding random input. Occasionally the line number may be misleading, and once in a blue + moon the only way to figure out what's triggering the error is to call perl -c repeatedly, chopping away half the program each time to se +e if the error went away. Sort of the cybernetic version of S<20 questions>. BEGIN not safe after errors--compilation aborted at C:/strawberry/perl +/lib/Carp/ Heavy.pm line 11. Compilation failed in require at C:/strawberry/perl/lib/Carp.pm line 3 +3.