Perl 新手,大括号/语法问题

发布于 2024-12-02 19:06:28 字数 1564 浏览 0 评论 0原文

我最近开始用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま昔日黯然 2024-12-09 19:06:28

这是没有意义的:

        ...

        do {
            recv($client,$buffer,1024,0);

        } while (ToString($buffer) != 'QUIT') {

            if (substr(ToString($buffer),0,10) == 'MAIL FROM:')

        ...

您将语句修饰符 (do {..} while...;while () {} 循环组合起来。它是或。

所以写一些类似的东西:

        ...

        while ( recv($client,$buffer,1024,0) ) {

            last if ToString($buffer) eq 'QUIT';

            if (substr(ToString($buffer),0,10) eq 'MAIL FROM:') {

                ...

            }

        }
etc.

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.
高冷爸爸 2024-12-09 19:06:28

除了使用 == 和 != (数字比较运算符)而不是 eq 和 ne 进行字符串比较的错误之外,在 while 测试之后还缺少一个分号。也就是说,您

do { ...; } while (...) { if (...) { ... } else {...}}

注意到 Perl 与 C 一样支持这两种形式

while (expr) { stuff }

do { stuff } while (expr)

并且我假设您打算使用后一种形式。

如果上面解释了第 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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文