Debugging internal server errors (premature end of script headers) part 6

Syntax errors

The dreaded syntax error. This is where you need a crash course on programming. There's something wrong in there somewhere which needs changing, and you have to know what it is. Here's where line numbers are everything, and notepad just won't cut it. Pick up a good html editor that gives you the option to display line numbers, it will save you a hell of a lot of time counting the lines out (especially when the error is on line 2075!!).

syntax error at test.cgi line 12, near "my "

Line 12 is: my $browser;
This one is usually a missing semi-colon(';') at the end of the previous line. As it's hard to have an error at the start of the line unless there's something desperately wrong, chances are it's at the end of the previous line. Check the line number before the line number stated in this error to see if it has a semi-colon at the end of it.

syntax error at test.cgi line 41, near ""black":"

The code:
    41: my $color = "black":

The problem here is as the error states - an error with ""black":" The problem here is a colon was used instead of a semi-colon at the end of the line. Easy to do, but it will throw an error every time.

It should be:
    my $color = "black";

syntax error at test.cgi line 50, near ")
("

The code:

    50: (
not a lot to go on there, let's look up one line:
    49: foreach $line(@lines)
    50: (
Now that's more like it - when you use a 'foreach', it should be:
'foreach $variable(@variables){...}'.
The error states that a '(' has been used instead of a '{'.These lines should look like:
    foreach $line(@lines)
    {

Unmatched right curly bracket at test.cgi line 144, at end of line

You have either an extra '}' in where it shouldn't be, on line 144, or you have missed an opening '{' further up. If there were no other errors reported before this error, you could safely assume that there is no opening one missing further up, or else it would have thrown errors before this. In this case, just remove the extra one at line 144. If it threw earlier errors, check what line it occured on and put the '{' in where it's needed (usually afer an if(...) or foreach(...)

 

syntax error at test.cgi line 239, near "else"
syntax error at test.cgi line 242, near "}"

The lines are:

    235: foreach ($line(@lines){
    236: if ($color eq "black")
    237: {
    238: $color = "white";
    239: else
    240: {
    241: $color = "black";
    242: }
    242: }
If you look at the above code on line 239, you will see only the word 'else'. Nothing wrong with that, so look up a line. There is a missing '}' in the 'if($color eq "black"){$color = "white"' bit. Change it to
    foreach ($line(@lines){
    if ($color eq "black")
    {
    $color = "white";
    }
    else
    {
    $color = "black";
    }
    }

and it will fix this problem.

 

The error at line 242 was reported because the balance of opening and closing parenthesis was messed up.