English 中文(简体)
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 with 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}[wW^}^{]{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).





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签