10.5. The elsif ClauseEvery so often,you may need to check a number of conditional expressions,one after another,to see which one of them is true. This can be done with the if control structure's elsif clause,as in this example: if ( ! defined $dino) { print "The value is undef./n"; } elsif ($dino =~ /^-?/d+/.?$/) { print "The value is an integer./n"; } elsif ($dino =~ /^-?/d*/./d+$/) { print "The value is a _simple_ floating-point number./n"; } elsif ($dino eq '') { print "The value is the empty string./n"; } else { print "The value is the string '$dino'./n"; } Perl will test the conditional expressions one after another. When one succeeds,the corresponding block of code is executed,the whole control structure is done,[*] and the execution goes on to the rest of the program. If none has succeeded,the else block at the end is executed. (The else clause is optional,though in this case it's often a good idea to include it.)
There's no limit to the number of elsif clauses,but Perl has to evaluate the first 99 tests before it can get to the hundredth. If you'll have more than half a dozen elsifs,you should consider whether there's a more efficient way to write it. The Perl FAQ (see the perlfaq manpage) has a number of suggestions for emulating the "case" or "switch" statements of other languages. You may have noticed by this point that the keyword is spelled elsif,with one e. If you write it as "elseif," with a second e,Perl will tell you that it is the incorrect spelling. Why? Because Larry says so.[*]
|
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。