如何修复 Perl 语法错误“缺少右大括号或方括号”使用 VIM?
使用不匹配的数组大括号(“[ ]”)或范围括号(“{ }”)编译(或执行)perl 程序会导致“缺少右大括号或方括号”语法错误。 Perl 通常将源代码报告为最后一个代码行(“at EOF”),这可能与实际缺失的部分相去甚远。
Perl 错误消息示例:
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.
vi 或 Vi IMproved (VIM) 用户如何解决此错误消息?我添加了一个带有一些 VIM 增强功能的答案。请添加您自己的想法、实践或 vi 插件。
注意:使用 VIM 版本发布的原始问题没有突出显示 perl 大括号和方括号。许多较新的版本都这样做;请参阅 vim.org 了解更多信息。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如何立即解决此错误:
在 VIM 中,选择一个左 {、[ 或 ( 符号)。
%
命令在匹配的 { }、[ ] 和 ( ) 对之间跳转。将跳转到意想不到的位置。安装perltidy,运行它,并查找奇怪的缩进代码块。
如何防止将来出现错误:
StackOverflow问题 719476 显示了如何修改 VIM 大括号/中括号的语法着色。 (某些版本默认情况下不执行此操作。)
Karl Guertin 的 AutoClose 插件 键入时自动匹配 [, (, {, ", ' 符号。
perltidy 脚本会重新格式化 Perl 以提高可读性,这可能会暴露不匹配的符号。
用户付费书呆子说:“使用perltidy 在 VIM 编辑器中使用 nmap。”
nmap \g mt:%!perltidy't
使用一致的 {} 匹配缩进(一般提示,并非特定于此 Perl 错误)。
或者
How to troubleshoot this error right now:
In VIM, pick an opening {, [, or ( symbol. The
%
command jumps between matching { }, [ ], and ( ) pairs. Mismatches will jump to an unexpected location.Install perltidy, run it, and look for oddly indented code blocks.
How to prevent future errors:
StackOverflow question 719476 shows how to modify VIM brace/bracket syntax coloring for braces/brackets. (Some versions don't do this by default.)
Karl Guertin's AutoClose plugin auto-matches [, (, {, ", ' symbols when typed.
perltidy script reformats perl for readability, which can expose mismatched symbols.
User a paid nerd said: "Use perltidy within VIM editor with nmap."
nmap \g mt:%!perltidy<CR>'t
Use consistent {} matching indentation (general tip, not specific to this perl error).
or
你可以使用 % 命令来匹配 vim 中的大括号/中括号/圆括号。所以用它来搜索不匹配的字符。
You can use the % command to match braces/brackets/parentheses in vim. So use that to search for unmatched characters.
我经常使用 perltidy 来重新格式化我的代码。当我重新格式化缺少终止符的代码时,进一步的代码会奇怪地缩进,我可以快速向上追踪以找到问题。
额外:我使用此映射来立即重新格式化文件并且不会丢失光标位置:
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:
使用神父。单击 Perl-> 查找不匹配的大括号。它将您的光标移至问题所在。
我带着同样的问题来到这个页面。使用 perltidy 和 vim 没有帮助,perltidy 后缩进看起来很好。由于某种原因,我在“sub”关键字之前有一个额外的左大括号。 Padre一键解决了这个问题。
http://padre.perlide.org/
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/
使用语法高亮,vim 几乎总能做到这一点,并且有一个非常复杂的 perl 语法高亮方案。
:语法
use syntax highlighting, vim almost always get this right, and has a very sophisticated perl syntax highlighting scheme.
:syntax on
听起来您可能正在谈论一种教学情况,在这种情况下,教学良好的缩进将解决大部分问题。
然而,有时我使用的服务器没有像 vim 和 perltidy 这样的精细功能,我使用一种快速的技术来查找语法错误 - 只需在代码中的不同点添加一个右卷曲,直到行号发生变化,这就是你的麻烦点。
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.
如果您没有看到任何缺失的大括号:
如果文件在 Windows 中创建并尝试在 Linux 中使用,则将 \r 替换为 \n 有助于解决此问题。
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.
我在一个似乎有完美匹配的大括号和方括号的脚本中遇到了这个错误...除了我使用了 bash 风格的语法,它实际上注释掉了一行的后半部分(带有一对大括号的后半部分) 。
:时,错误消失了:
当我将此行更改为
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:
to this:
使用正则表达式来缩小违规代码的范围。使用 shell 的示例,pcregrep
变化
90
在需要结束大括号之前根据需要限制块长度。Use regular expression to narrow down where offending code. Example using shell, pcregrep
varying
90
as needed for limiting block length before expecting an ending brace.作为 @Ether 和 @Matthew Glidden 的简单重述和组合:
意图如下所示(
vi 命令如下
)1G
)/[[({]
然后按“Enter”键)%
)%
)n
)可以追溯到 HP-UX 11.11 上的 vi(例如 ~2001)。
As a simple re-statement and combination of @Ether and @Matthew Glidden:
Intent shown like this (
vi command like this
)1G
)/[[({]
then press "Enter" )%
)%
)n
)Works as far back as vi on HP-UX 11.11 (like ~2001).