Perl 新手,大括号/语法问题
我最近开始用 Perl 编程(我将跳过长篇故事),但我偶然发现了一些我似乎无法理解的错误:
syntax error at /usr/sbin/test.pl line 238, near ") {"
syntax error at /usr/sbin/test.pl line 247, near "} else"
syntax error at /usr/sbin/test.pl line 258, near ") {"
syntax error at /usr/sbin/test.pl line 276, near ") {"
syntax error at /usr/sbin/test.pl line 304, near "}"
syntax error at /usr/sbin/test.pl line 308, near "}"
syntax error at /usr/sbin/test.pl line 323, near "}"
周围的括号有关
这似乎与 if 和else 我在 C、C#、Java、PHP、Lua 等方面经验丰富,所以我有点尴尬地陷入语法错误。
我粘贴了一个生成语法错误的代码示例:
if (substr(ToString($buffer),0,4) == 'HELO') {
$contype = 'smtp';
send($client,'250 Welcome',0);
} elsif (substr(ToString($buffer),0,4) == 'EHLO') {
$contype = 'esmtp';
send($client,'250-$hostname Welcome',0);
send($client,'250 SIZE $msgmaxsize',0);
}
do {
recv($client,$buffer,1024,0);
} while (ToString($buffer) != 'QUIT') {
if (substr(ToString($buffer),0,10) == 'MAIL FROM:')
{
$sender = ToString($buffer);
$sender =~ m/<(.*?)>/;
send($client,'250 OK',0);
} else {
send($client,'503 I was expecting MAIL FROM',0);
send($client,'221 Bye',0);
break;
}
}
不幸的是,我无法显示整个计划。
Perl 版本 5.10.1
I have recently started programming in Perl (I'll skip the long story) but I've stumbled upon a few errors that I can't seem to understand:
syntax error at /usr/sbin/test.pl line 238, near ") {"
syntax error at /usr/sbin/test.pl line 247, near "} else"
syntax error at /usr/sbin/test.pl line 258, near ") {"
syntax error at /usr/sbin/test.pl line 276, near ") {"
syntax error at /usr/sbin/test.pl line 304, near "}"
syntax error at /usr/sbin/test.pl line 308, near "}"
syntax error at /usr/sbin/test.pl line 323, near "}"
it seems to be something to do with the brackets surrounding if and else
I'm experienced in C, C#, Java, PHP, Lua, and others so I'm a bit embaressed to get stuck on syntax errors..
I've pasted a sample of code that generates a syntax error:
if (substr(ToString($buffer),0,4) == 'HELO') {
$contype = 'smtp';
send($client,'250 Welcome',0);
} elsif (substr(ToString($buffer),0,4) == 'EHLO') {
$contype = 'esmtp';
send($client,'250-$hostname Welcome',0);
send($client,'250 SIZE $msgmaxsize',0);
}
do {
recv($client,$buffer,1024,0);
} while (ToString($buffer) != 'QUIT') {
if (substr(ToString($buffer),0,10) == 'MAIL FROM:')
{
$sender = ToString($buffer);
$sender =~ m/<(.*?)>/;
send($client,'250 OK',0);
} else {
send($client,'503 I was expecting MAIL FROM',0);
send($client,'221 Bye',0);
break;
}
}
unfortunately I can not show the entire program.
Perl version 5.10.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是没有意义的:
您将语句修饰符 (
do {..} while...;
与while () {}
循环组合起来。它是或。所以写一些类似的东西:
This makes no sense:
You're combining a statement modifier (
do {..} while...;
with awhile () {}
loop. It's either or.So write something like:
除了使用 == 和 != (数字比较运算符)而不是 eq 和 ne 进行字符串比较的错误之外,在 while 测试之后还缺少一个分号。也就是说,您
注意到 Perl 与 C 一样支持这两种形式
,
并且我假设您打算使用后一种形式。
如果上面解释了第 238 行的错误,那么当您更正它时,如果它以某种方式导致了悬空 else,则第 247 行的错误可能会消失;但如果不编译代码,我不太明白如何。
Besides the mistake of using == and != (which are number-comparison operators) instead of eq and ne for string comparisons, you are missing a semi-colon after the while's test. That is, you have
Note that Perl, like C, supports both forms
and
and I'm supposing you meant to use the latter form.
If the above accounts for the error at line 238, then it's possible that the error at line 247 might go away when you correct it, if it somehow causes a dangling else; but without compiling the code, I don't quite see how.