I have recently started programming in Perl (I'll skip the long sto开发者_运维百科ry) 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
This makes no sense:
...
do {
recv($client,$buffer,1024,0);
} while (ToString($buffer) != 'QUIT') {
if (substr(ToString($buffer),0,10) == 'MAIL FROM:')
...
You're combining a statement modifier (do {..} while...;
with a while () {}
loop. It's either or.
So write something like:
...
while ( recv($client,$buffer,1024,0) ) {
last if ToString($buffer) eq 'QUIT';
if (substr(ToString($buffer),0,10) eq 'MAIL FROM:') {
...
}
}
etc.
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
do { ...; } while (...) { if (...) { ... } else {...}}
Note that Perl, like C, supports both forms
while (expr) { stuff }
and
do { stuff } while (expr)
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.
精彩评论