Learning Perl: 10.1. The unless Control Structure
来源:互联网 发布:添加打印机无usb端口 编辑:程序博客网 时间:2024/06/05 20:16
10.1. The unless Control Structure
In an if control structure, the block of code is executed only when the conditional expression is true. If you want to execute a block of code only when the conditional is false, change if to unless:
unless ($fred =~ /^[A-Z_]/w*$/i) { print "The value of /$fred doesn't look like a Perl identifier name./n"; }
Using unless says to run the block of code unless this condition is true. It's like using an if test with the opposite condition. Another way to say it is that it's like having the else clause on its own. Whenever you see an unless you don't understand, you can rewrite it (in your head or in reality) as an if test:
if ($fred =~ /^[A-Z_]/w*$/i) { # Do nothing } else { print "The value of /$fred doesn't look like a Perl identifier name./n"; }
It's no more or less efficient, and it should compile to the same internal byte codes. Another way to rewrite it would be to negate the conditional expression by using the negation operator (!):
if ( ! ($fred =~ /^[A-Z_]/w*$/i) ) { print "The value of /$fred doesn't look like a Perl identifier name./n"; }
You should pick the way of writing code that makes the most sense to you since that will probably make the most sense to your maintenance programmer. If it makes the most sense to write if with a negation, do that. More often, you'll likely find it natural to use unless.
10.1.1. The else Clause with unless
You can have an else clause with an unless. Though this syntax is supported, it can be confusing:
unless ($mon =~ /^Feb/) { print "This month has at least thirty days./n"; } else { print "Do you see what's going on here?/n"; }
Some people may wish to use this, especially when the first clause is short (perhaps only one line) and the second is several lines of code. But we'd make this one a negated if or maybe swap the clauses to make a normal if:
if ($mon =~ /^Feb/) { print "Do you see what's going on here?/n"; } else { print "This month has at least thirty days./n"; }
Remember you're always writing code for two readers: the computer that will run the code and the human being who has to keep the code working. If the human can't understand what you've written, pretty soon the computer won't be doing the right thing either.
- Learning Perl: 10.1. The unless Control Structure
- Perl Learning: 2.6. The if Control Structure
- Perl Learning: 2.9. The while Control Structure
- Learning Perl: 3.6. The foreach Control Structure
- Learning Perl: 10.2. The until Control Structure
- Learning Perl: 10.4. The Naked Block Control Structure
- perl unless循环
- Learning Perl: 2.8. The chomp Operator
- Perl Learning: 2.10. The undef Value
- Learning Perl: 2.11. The defined Function
- 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.5. The elsif Clause
- Learning Perl: 9.3. The join Function
- Learning Perl: 9.4. m// in List Context
- 瑞斯康达NView 5 研究实录 (6) - 功能实现二
- java Script查询列表排序
- Learning Perl: 9.5. More Powerful Regular Expressions
- Learning Perl: 10.1. The unless Control Structure
- 重置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()方法深度剖析(转)