开发者

How do I fix the perl syntax error "missing right curly or square bracket" using VIM?

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.


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
{
...
}


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


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


Use padre. Click Perl->Find Unmatched Brace. It moves your cursor right to the problem.

I came to this page with the same problem. Using perltidy and vim were of no help, the indenting looked fine after perltidy. I had an extra open brace before a 'sub' keyword for some reason. Padre solved the problem in one-click.

http://padre.perlide.org/


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

:syntax on


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.


If you don't see any missing braces :
Replacing \r with \n can help to fix this issue if the file created in windows and trying to use in Linux.


I got this error in a script that appeared to have perfectly matched curly and square braces... except that I had used bash-style syntax which actually commented out the second half of one line (with the second half of a curly brace pair).

The error went away when I changed this line:

$data_len="${#insert_data}";

to this:

$data_len=length($insert_data);


Use regular expression to narrow down where offending code. Example using shell, pcregrep

cat index.pl |pcregrep '[\{]{1}[\w\W^}^{]{90,}'

varying 90 as needed for limiting block length before expecting an ending brace.


As a simple re-statement and combination of @Ether and @Matthew Glidden:

Intent shown like this ( vi command like this )

  1. Go to top of file ( 1G )
  2. Search for brackets ( /[[({] then press "Enter" )
    • That's "slash" "open square brace" "open square brace" "open round brace" "open curly brace" "close square brace"
    • As a regex, we're searching for a custom character class which is all opening braces.
  3. Find end matching bracket ( % )
  4. Find starting bracket again ( % )
  5. Find next opening bracket ( n )
  6. Repeat from 3 until you've checked all brackets in file.

Works as far back as vi on HP-UX 11.11 (like ~2001).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜