C 中 return 和 continue 关键字的 Perl 等价物是什么? [英] What are the Perl equivalents of return and continue keywords in C?

查看:59
本文介绍了C 中 return 和 continue 关键字的 Perl 等价物是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用此代码时出现此错误

子搜索{我的 ($a, @a_list) = @_;foreach (@a_list) {if($_ == $a) 返回真;# 继续;}返回假;}

<块引用>

code.pl 第 26 行语法错误,靠近) return"

  • return TRUE 的正确方法是什么?
  • 另外,继续的正确方法是什么?

我知道我应该更多地考虑 Perl 术语,而不是尝试以这种方式将 C 代码转换为 Perl.

解决方案

return、continue、break 的等价物

  • return 相当于 return.
  • next 相当于 continue.
  • last 相当于 break.

'if' 语句后跟块

你的问题是 if 语句后面总是跟着一个大括号内的块.

if ($_ == $a) { return TRUE;}elif (...) { ... }别的          { ... }

尽管有时看起来有点冗长,但我同意 ysth 的观点,即 Perl 做对了.对于一个有趣的替代方案,请参阅 Go 编程语言,该语言将括号视为不必要的并强制使用大括号.

'if' 可以用作限定符

或者您可以使用 if 作为语句修饰符:

如果 $_ == $a 则返回 TRUE;

请注意,在这种情况下,您不必在条件表达式周围使用括号,尽管添加它们没有坏处.

使用除非"

您也可以使用 unless 代替 if 来反转条件:

return TRUE 除非 $_ != $a;

(而且,正如 Phillip Potter 指出的那样,将 unless 与否定的条件使理解更困难;该示例直接与问题相同,但最好编写为具有相等性的 if.)

使用下一个"和上一个"

您可以类似地使用 nextlast:

子搜索{我的 ($a, @a_list) = @_;foreach (@a_list) {如果 $_ == $a 则返回 TRUE;最后如果 $_ >$a;下一个如果 $ptom != LAST_QUARTER;...}返回假;}

注意 foreach 循环中的修正.(问题已修改以包含修正.)确保你总是有在脚本顶部使用严格;'和'use warnings;'.专家总是使用它们来确保他们没有犯错误.初学者应该出于完全相同的原因使用它们.

对与错

此外,正如其他答案中首先指出的那样,Perl 没有预定义的常量 TRUE 和 FALSE,就像 C 或 C++ 那样(C++ 有一个内置的 truefalse;如果您 #include ,C99 具有 truefalse 有条件地可用.您可以为 Perl 提供以下定义:

使用常量 TRUE =>1;使用常量 FALSE =>0;

不过要小心.有些事情即使不等于 TRUE 也会是真的";即使不等于 FALSE,其他事情也会是假".

<小时>

讨论'$a'和'$b'的使用

评论包含关于不使用 $a$b 作为变量的讨论.相关评论依次为:

  • 请避免使用 $a,除非它在排序块中.– Zaid

  • @Zaid:很好,$a 和 $b 在排序块的上下文中是特殊的.我不确定是否有法令规定它们不应该以其他方式使用 - 当周围还潜伏着一个排序块时使用它们将是残酷的,但是在没有排序块的情况下,我看不出有任何理由对待 $a 并不同于 $z.– 乔纳森·莱夫勒

  • $a 和 $b 是全局变量,因此其行为与词法不同.– phaylon

  • @phaylon:好吧,严格来说它们是包全局变量"(参见 Perl sort).是的,在排序时,它们与词法(my)变量不同.当您不进行排序时,如果您明确声明它们,则它们可以被视为词法.– 乔纳森·莱夫勒

  • @Jonathan Leffler,他们也免于 use strict qw(vars);所以你可能不会注意到你正在从另一个范围践踏它们.– Ven'Tatsu

指出显而易见的:我只使用了 $a 因为这个问题确实如此 - 而不是用大量细节淹没原始海报,我主要保留了要点.例如,lastnext 的讨论没有提到循环标签.

也就是说,建议避免使用 $a$b 作为变量"是合理的;由于所指出的原因,它们是特殊名称,使用它们可能会出现错误,这些错误可能会或可能不会被检测到.

I am getting this error when I use this code

sub search {
    my ($a, @a_list) = @_;
    foreach (@a_list) {
        if($_ == $a) return TRUE;
        # continue;
    }
    return FALSE;
}

syntax error at code.pl line 26, near ") return"

  • What is the right way to return TRUE?
  • Also, what is the right way to continue?

I know I should be thinking more in Perl terms than trying to convert C code to Perl this way.

解决方案

Equivalents of return, continue, break

  • return is the equivalent of return.
  • next is the equivalent of continue.
  • last is the equivalent of break.

'if' statements are followed by blocks

Your problem is that an if statement is always followed by a block inside braces.

if ($_ == $a) { return TRUE; }
elsif (...)   { ... }
else          { ... }

For all it sometimes seems a bit verbose, I agree with ysth that this is something that Perl got right. For an interesting alternative take, see the Go programming language, which treats the parentheses as unnecessary and mandates the braces.

'if' can be used as a qualifier

Or you can use the if as a statement modifier:

return TRUE if $_ == $a;

Note that in this case, you don't have to use parentheses around the conditional expression, though there'd be no harm in adding them.

Using 'unless'

You can also use unless instead of if to invert the condition:

return TRUE unless $_ != $a;

(And, as Phillip Potter pointed out, mixing unless with a negated condition makes comprehension harder; the example is directly doing the same as the question, but is better written as an if with equality.)

Using 'next' and 'last'

You can use next and last similarly:

sub search {
    my ($a, @a_list) = @_;
    foreach (@a_list) {
        return TRUE if $_ == $a;
        last if $_ > $a;
        next if $ptom != LAST_QUARTER;
        ...
    }
    return FALSE;
}

Note the fixup in the foreach loop. (Question amended to include the fixup.) Make sure you always have 'use strict;' and 'use warnings;' at the top of your script. Experts always use them to make sure they haven't made mistakes. Beginners should use them for exactly the same reason.

TRUE and FALSE

Also, as pointed out first in other answers, Perl does not have pre-defined constants TRUE and FALSE, any more than C or C++ do (C++ has a built-in true and false; C99 has true and false conditionally available if you #include <stdbool.h>). You can provide the definitions for Perl as:

use constant TRUE => 1;
use constant FALSE => 0;

Be wary, though. Some things will be 'true' even when not equal to TRUE; other things will be 'false' even when not equal to FALSE.


Discussion about use of '$a' and '$b'

The comments contain a discussion about not using $a and $b as variables. In sequence, the relevant comments were:

  • Please avoid using $a unless it's in a sort block. – Zaid

  • @Zaid: Good point that $a and $b are special in the context of a sort block. I'm not sure whether there are edicts that they should never be used otherwise - it would be atrocious to use them when there is also a sort block lurking around, but in the absence of sort blocks, I don't see any reason to treat $a and different than $z. – Jonathan Leffler

  • $a and $b are globals, and as such behave different than lexicals. – phaylon

  • @phaylon: well, strictly they are 'package globals' (see Perl sort). Yes, when you are sorting, they are different from lexicals (my) variables. When you aren't doing sorting, then they can be treated as lexicals if you declare them explicitly. – Jonathan Leffler

  • @Jonathan Leffler, they are also exempt from use strict qw(vars); so you might not notice that you are trampling on them from another scope. – Ven'Tatsu

Pointing out the obvious: I only used $a because the question did - and rather than inundate the original poster with lots of details, I kept mostly to the main points. For example, the discussion of last and next does not mention loop labels.

That said, the advice "avoid using $a and $b as variables" is sound; they are special names for the reasons pointed out, and using them leaves open the possibility of mistakes that may or may not be be detectable.

这篇关于C 中 return 和 continue 关键字的 Perl 等价物是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆