我应该使用Perl的条件吗? :运算符作为switch / case语句,或者代替elsif? [英] Should I use Perl's conditional ? : operator as a switch / case statement or instead of if elsif?

查看:80
本文介绍了我应该使用Perl的条件吗? :运算符作为switch / case语句,或者代替elsif?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Perl具有条件运算符,该运算符与 C的条件运算符

Perl has a conditional operator that is the same a C's conditional operator.

要刷新,在C和Perl中的条件运算符为:

To refresh, the conditional operator in C and in Perl is:

(test) ? (if test was true) : (if test was false)

如果与左值一起使用,则可以分配并测试以下一项操作:

and if used with an lvalue you can assign and test with one action:

my $x=  $n==0 ? "n is 0" : "n is not 0";

我正在阅读Igor Ostrovsky在用基于C的语言表达多条子if的巧妙方法,并且意识到这确实是Perl中的巧妙方式。

I was reading Igor Ostrovsky's blog on A neat way to express multi-clause if statements in C-based languages and realized this is indeed a "neat way" in Perl as well.

例如:(编辑:使用了Jonathan Leffler更具可读性的表格...)

For example: (edit: used Jonathan Leffler's more readable form...)

# ternary conditional form of if / elsif construct:
my $s=
      $n == 0     ? "$n ain't squawt"
    : $n == 1     ? "$n is not a lot"
    : $n < 100    ? "$n is more than 1..."
    : $n < 1000   ? "$n is in triple digits"
    :               "Wow! $n is thousands!" ;  #default

读很多东西要比许多人在Perl中写的容易:
(编辑:使用cjm更优雅的 my $ t = do {如果}; 形式在rafi的答案中)

Which reads a LOT easier than what many would write in Perl: (edit: used cjm's more elegant my $t=do{ if }; form in rafi's answer)

# Perl form, not using Switch or given / when
my $t = do {
    if    ($n == 0)   { "$n ain't squawt"        }
    elsif ($n == 1)   { "$n is not a lot"        }
    elsif ($n < 100)  { "$n is more than 1..."   }
    elsif ($n < 1000) { "$n is in triple digits" }
    else              {  "Wow! $n is thousands!" }
};

这里是否有陷阱或缺点?为什么不以这种方式编写扩展的条件形式,而不是使用 if(something){this} elsif(something){that}

Are there any gotchas or downside here? Why would I not write an extended conditional form in this manner rather than use if(something) { this } elsif(something) { that }?

条件运算符具有正确的关联性和较低的优先级。因此:

The conditional operator has right associativity and low precedence. So:

a ? b : c ? d : e ? f : g

解释为:

a ? b : (c ? d : (e ? f : g))

我想您可能需要括号如果您的测试使用了优先级低于?:的少数运算符之一。我认为您也可以将大括号的形式放在块中。

I suppose you might need parenthesis if your tests used one of the few operator of lower precedence than ?:. You could also put blocks in the form with braces I think.

我确实知道关于不赞成使用的 use Switch 或Perl 5.10给出的

I do know about the deprecated use Switch or about Perl 5.10's given/when constructs, and I am not looking for a suggestion to use those.

这些是我的问题:


  • 您是否看到了Perl中使用的这种语法?**我没有,并且它不在 perlop 或 perlsyn 作为切换的替代方法。

  • Have you seen this syntax used in Perl?** I have not, and it is not in perlop or perlsyn as an alternate to switch.

是否存在潜在的语法问题或陷阱?以这种方式使用条件/三元运算符?

Are there potential syntax problems or 'gotchas' with using a conditional / ternary operator in this way?

意见:您对它更具可读性/理解性吗?

Opinion: Is it more readable / understandable to you? Is it consistent with Idiomatic Perl?

--------编辑- strong>

我接受了Jonathan Leffler的回答,因为他指出了我 Perl最佳做法。相关部分是关于 Tabular三元组的6.17。这使我可以进一步研究其用途。 (如果您使用的是Google Perl表格三进制,则可以看到其他注释。)

I accepted Jonathan Leffler's answer because he pointed me to Perl Best Practices. The relevant section is 6.17 on Tabular Ternaries. This allowed me to investigate the use further. (If you Google Perl Tabular Ternaries, you can see other comments.)

Conway的两个示例是:

Conway's two examples are:

my $salute;
if ($name eq $EMPTY_STR) {
    $salute = 'Dear Customer';
}
elsif ($name =~ m/\A ((?:Sir|Dame) \s+ \S+)/xms) {
    $salute = "Dear $1";
}

elsif ($name =~ m/([^\n]*), \s+ Ph[.]?D \z/xms) {
    $sa1ute = "Dear Dr $1";
}
else {
    $salute = "Dear $name";
}

VS:

           # Name format...                            # Salutation...
my $salute = $name eq $EMPTY_STR                       ? 'Dear Customer'
           : $name =~ m/ \A((?:Sir|Dame) \s+ \S+) /xms ? "Dear $1"
           : $name =~ m/ (.*), \s+ Ph[.]?D \z     /xms ? "Dear Dr $1"
           :                                             "Dear $name"
           ;

我的结论是:


  • Conway的?:例子比 if / elsif 对我来说更易读和更简单表格,但我可以看到表格很难理解。

  • Conway's ?: example is more readable and simpler to me than the if/elsif form, but I could see how the form could get hard to understand.

如果您拥有Perl 5.13.1,请使用 my $ t = do {给{当}时}; 作为 rafi已经做到了。我认为给定/何时现在是最好的习惯用法,除非表格三元格式更适合您的特定情况。

If you have Perl 5.13.1, use my $t=do { given { when } }; as an assignment as rafi has done. I think given/when is the best idiom now, unless the tabular ternary format is better for your particular case.

如果您拥有Perl 5.10+,请在一般情况下使用 given / when 代替 Switch ,或者如果您需要某种案例类型的开关。

If you have Perl 5.10+ use given/when in general instead of Switch or if you need some sort of case type switch.

较旧的Perl,这是简单替代方案或case语句替代方案的优良形式。我认为它比使用 Switch 更好。

Older Perl's, this is a fine form for simple alternatives or as an alternate to a case statement. It is better than using Switch I think.

从右到左的关联性表示从下到上对表单进行求值。请记住,在使用...

The right to left associativity means the form is evaluated bottom to top. Remember that when using...

推荐答案

有条件的布局操作员很难阅读。
这更像是我记得的 Perl最佳做法建议:

The layout shown for the conditional operator is hard to read. This is more like what I recall Perl Best Practices recommending:

my $s = $n == 0   ? "$n ain't squawt"
      : $n == 1   ? "$n is not a lot"
      : $n < 100  ? "$n is more than 1..."
      : $n < 1000 ? "$n is in triple digits"
      :             "Wow! $n is thousands!";  # default...

有时,最好使用更紧凑的符号 if 也是如此:

And there are times when it is better to use a more compact notation with the if notation, too:

  if    ($n == 0)   { $t = "$n ain't squawt";        }
  elsif ($n == 1)   { $t = "$n is not a lot";        }
  elsif ($n < 100)  { $t = "$n is more than 1...";   }
  elsif ($n < 1000) { $t = "$n is in triple digits"; }
  else              { $t = "Wow! $n is thousands!" ; }  

这两种重新格式化都强调了代码各部分的相似性,从而使它们更易于阅读和理解。理解。

Both these reformattings emphasize the similarity of the various sections of the code, making it easier to read and understand.

这篇关于我应该使用Perl的条件吗? :运算符作为switch / case语句,或者代替elsif?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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