Learning Perl: 10.5. The elsif Clause
来源:互联网 发布:添加打印机无usb端口 编辑:程序博客网 时间:2024/06/05 15:30
10.5. The elsif Clause
Every 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 "fall-through" to the next block as in the "switch" structure of languages such as C.
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.[*]
[*] In fact, he resists any suggestion that it even be permitted as a valid alternative spelling: "If you want to spell it with a second e, it's simple. Step 1: Make up your own language. Step 2: Make it popular." When you make your own programming language, you can spell the keywords any way you'd like. We hope you will decide that yours shouldn't be the first to have an "elseunless."
- Learning Perl: 10.5. The elsif Clause
- perl if elsif else
- Perl Learning: 2.6. The if Control Structure
- Learning Perl: 2.8. The chomp Operator
- Perl Learning: 2.9. The while Control Structure
- Perl Learning: 2.10. The undef Value
- Learning Perl: 2.11. The defined Function
- Learning Perl: 3.6. The foreach Control Structure
- Learning Perl: 4.8. The use strict Pragma
- Learning Perl: 4.9. The return Operator
- Learning Perl: 5.3. The Invocation Arguments
- Learning Perl: 8.4. The Binding Operator, =~
- Learning Perl: 8.6. The Match Variables
- Learning Perl: 9.2. The split Operator
- Learning Perl: 9.3. The join Function
- Learning Perl: 10.1. The unless Control Structure
- Learning Perl: 10.2. The until Control Structure
- Learning Perl
- 重置cdr的billsec的方法
- 飞秋_飞秋2010_飞秋2010下载_飞秋下载2010正式版
- Learning Perl: 10.2. The until Control Structure
- Learning Perl: 10.3. Expression Modifiers
- Learning Perl: 10.4. The Naked Block Control Structure
- Learning Perl: 10.5. The elsif Clause
- Learning Perl: 10.6. Autoincrement and Autodecrement
- ASP.NET中Response.Redirect()方法深度剖析(转)
- 一些学习prestashop的好网站
- Scite配置
- jfreechart实例
- 精华贴子整理
- 如何在ListBox中添加控件后让ListBox滚动到最底部
- 正则表达式