Perl   发布时间:2022-04-07  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了我应该使用Perl的条件吗? :作为switch/case语句的操作符,而不是elsif?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Perl拥有 conditional运算符,与 @L_696_1@相同。

要刷新,C和Perl中的条件运算符是:@H_607_3@

(test) ? (if test was truE) : (if test was falsE)

如果使用左值,则可以通过一个操作进行分配和测试:@H_607_3@ @H_69_4@my $x= $n==0 ? "n is 0" : "n is not 0";

我正在A neat way to express multi-clause if statements in C-based languages阅读Igor Ostrovsky的博客,并意识到这在Perl中确实是一个“整洁的方式”。@H_607_3@

例如:(编辑:使用Jonathan Leffler更易读的形式…)@H_607_3@

# 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

其中读取的LOT比Perl中写的更容易
(编辑:使用cjm更优雅我的$ t = do {if};形式在rafi的答案)@H_607_3@

# 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}?@H_607_3@

条件运算符有right associativity and low precedence.所以:@H_607_3@

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

被解释为:@H_607_3@

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

如果您的测试使用低于优先级的少数操作符之一,则可能需要括号:。你也可以用我认为的大括号把块放在表单中。@H_607_3@

我知道已经弃用的Switch或Perl 5.10的给定/何时结构,我不是在寻找使用它们的建议。@H_607_3@

这些是我的问题:@H_607_3@

>您是否看过Perl中使用的这种语法?**我没有看到,并不是在perlop或perlsyn中作为交替的替代方法。
>用这种方式使用条件/三元运算符有潜在的语法问题还是“陷阱”?
>意见:你更可读/可以理解吗?是否符合惯用语Perl?@H_607_3@

——–编辑 – @H_607_3@

我接受了乔纳森·莱夫勒的回答,因为他指出我在Perl Best Practices。相关部分是6.17表格三元。这让我进一步调查使用。 (如果您使用Google Perl表格三元组,则可以看到其他评论。)@H_607_3@

康威的两个例子是:@H_607_3@ @H_69_4@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:@H_607_3@

# 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"
           ;

我的结论是:@H_607_3@

> Conway的?:例子比if / elsif格式更可读,更简单,但是我可以看到这个表单怎么会很难理解。
>如果您有Perl 5.13.1,请使用我的$ t = do {given {when}};作为rafi has done.的作业,我认为现在是最好的成语,除非表格三元格式对于你的具体情况更好。
>如果您有Perl 5.10使用给定/通常而不是Switch,或者您需要某种类型的开关。
>较老的Perl,这是一个很好的形式,简单的替代方案或作为案例陈述的替代。比使用Switch我想的更好。
>左侧关联的权利意味着表单从底部到顶部进行评估。记住当使用…@H_607_3@

解决方法

为条件运算符显示的布局很难阅读。
这更像是我所说的 Perl Best Practices推荐: @H_69_4@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...

而且,有时更好地使用更简洁的符号,如果符号也是如此:@H_607_3@

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!" ; }

这些重新格式化都强调了代码的各个部分的相似性,使其更容易阅读和理解。@H_607_3@

大佬总结

以上是大佬教程为你收集整理的我应该使用Perl的条件吗? :作为switch/case语句的操作符,而不是elsif?全部内容,希望文章能够帮你解决我应该使用Perl的条件吗? :作为switch/case语句的操作符,而不是elsif?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。