if
… elsif
… else
…
Perl提供了一种更简短的“statement condition”语法,对于短的语句强烈推荐这种写法:
unless
… else
…
而相比之下,这种写法就被强烈推荐,因为实在是太易于阅读了:
print "Oh no it's too cold" unless $temperature > 15;
三目运算符
题外话:单复数形式最好都写出完整的拼写,不要自作聪明地写成下面这种样子,要不然别人永远也无法在代码中查找、替换到“tooth”或者“teeth”了:
print "You lost ", $lost, " t", ($lost == 1 ? "oo" : "ee"), "th!";
if
语句在scalar上下文中进行求值。举例说明,if(@array)
当且仅当@array
包含大于等于1个元素的时候返回true,而元素的内容则无关紧要(也许包括或者其他我们真正关心的代表false的值)。