views:

1291

answers:

5

Compiling (or executing) a perl program with unmatched array braces ("[ ]") or scope brackets ("{ }") causes the "missing right curly or square bracket" syntax error. Perl often reports the source as your last code line ("at EOF"), which could be far removed from the actual missing piece.

Example perl error message:

Missing right curly or square bracket at ./foo.pl line 100, at end of line
syntax error at ./foo.pl line 100, at EOF
Execution of ./foo.pl aborted due to compilation errors.

How do vi or Vi IMproved (VIM) users troubleshoot this error message? I've added an answer with some VIM enhancements. Please add your own ideas, practices, or vi plugins.

NOTE: Original question posted with VIM version that didn't highlight perl braces and brackets. Many newer versions do this; see vim.org for more info.

+1  A: 

use syntax highlighting, vim almost always get this right, and has a very sophisticated perl syntax highlighting scheme.

:syntax on

Evan Carroll
How will syntax highlighting help spot missing braces?
innaM
I don't suppose it will. Syntax highlighting really only makes unclosed quotes apparent.
Evan Carroll
+7  A: 

You can use the % command to match braces/brackets/parentheses in vim. So use that to search for unmatched characters.

Ether
+4  A: 

How to troubleshoot this error right now:

  1. In VIM, pick an opening {, [, or ( symbol. The % command jumps between matching { }, [ ], and ( ) pairs. Mismatches will jump to an unexpected location.

  2. Install perltidy, run it, and look for oddly indented code blocks.

How to prevent future errors:

  1. StackOverflow question 719476 shows how to modify VIM brace/bracket syntax coloring for braces/brackets. (Some versions don't do this by default.)

  2. Karl Guertin's AutoClose plugin auto-matches [, (, {, ", ' symbols when typed.

  3. perltidy script reformats perl for readability, which can expose mismatched symbols.

  4. User a paid nerd said: "Use perltidy within VIM editor with nmap."

    nmap \g mt:%!perltidy<CR>'t

  5. Use consistent {} matching indentation (general tip, not specific to this perl error).

 

sub foo {
...
}

or

sub bar
{
...
}
Matthew Glidden
+4  A: 

I constantly use perltidy to reformat my code. When I reformat code that's missing a terminator, further code indents strangely and I can quickly trace upward to locate the problem.

Bonus: I use this mapping to instantly reformat the file and not lose the cursor position:

nmap \g mt:%!perltidy<CR>'t
a paid nerd
A: 

It sounds like you may be talking about a teaching situation, in which case teaching good indentation will solve most of these issues.

However, sometimes I'm on a server with no niceties like vim and perltidy, and I use a quick'n'dirty technique to find the syntax error - just add a right-curly at different points in your code until the line number changes, and there's your trouble spot.

John O'Rourke
For context, I got this error editing a script at work and spent an hour or so debugging. Found a few hints on-line, but not a complete answer. Hope this helps for both production and instruction work!
Matthew Glidden