条件语句
Perl当然也支持if/then/else语句,下面是一个例子:
if ($a)
{
print "The string is not empty\n";
}
else
{
print "The string is empty\n";
}
记住,空字符串被认为是false,如果$a是字符串"0",结果将是"empty"。
条件语句中也可以使用elsif:
if (!$a) # The ! is the not operator
{
print "The string is empty\n";
}
elsif (length($a) == 1) # If above fails, try this
{
print "The string has one character\n";
}
elsif (length($a) == 2) # If that fails, try this
{
print "The string has two characters\n";
}
else # Now, everything has failed
{
print "The string has lots of characters\n";
}
注意:elsif中确实缺一个"e"。