Perl Learning: 2.6. The if Control Structure
来源:互联网 发布:京东和淘宝买吉他 编辑:程序博客网 时间:2024/06/03 19:46
2.6. The if Control Structure
Once you can compare two values, you'll probably want your program to make decisions based upon that comparison. Like all similar languages, Perl has an if control structure:
if ($name gt 'fred') { print "'$name' comes after 'fred' in sorted order./n"; }
If you need an alternative choice, the else keyword provides that as well:
if ($name gt 'fred') { print "'$name' comes after 'fred' in sorted order./n"; } else { print "'$name' does not come after 'fred'./n"; print "Maybe it's the same string, in fact./n"; }
Those block curly braces are required around the conditional code (unlike C, whether you know C or not). It's a good idea to indent the contents of the blocks of code as we show here; that makes it easier to see what's going on. If you're using a programmers' text editor (as discussed in Chapter 1), it'll do most of the work for you.
2.6.1. Boolean Values
You may use any scalar value as the conditional of the if control structure. That's handy if you want to store a true or false value into a variable, like this:
$is_bigger = $name gt 'fred'; if ($is_bigger) { ... }
But how does Perl decide whether a given value is true or false? Perl doesn't have a separate Boolean data type as some languages have. Instead, it uses a few simple rules:[*]
[*] These aren't the rules Perl uses but are rules you can use to get the same result.
If the value is a number, 0 means false; all other numbers mean true.
If the value is a string, the empty string ('') means false; all other strings mean true.
If the value is another kind of scalar than a number or a string, convert it to a number or a string and try again.[]
[] This means that undef (which we'll see soon) means false, and all references (which are covered in the Alpaca book) are true.
There's one trick hidden in those rules. Because the string '0' is the same scalar value as the number 0, Perl has to treat them the same. That means that the string '0' is the only nonempty string that is false.
If you need to get the opposite of any Boolean value, use the unary not operator, !. If what follows it is a true value, it returns false; if what follows is false, it returns true:
if (! $is_bigger) { # Do something when $is_bigger is not true }
- 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.1. The unless Control Structure
- Learning Perl: 10.2. The until Control Structure
- Learning Perl: 10.4. The Naked Block Control Structure
- 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
- XML加密解密
- sql server快捷键
- hdu 3463Goldbach Division(打表法)
- fusionChart的使用
- 用 win32 API 模拟按键
- Perl Learning: 2.6. The if Control Structure
- Learning Perl: 2.7. Getting User Input
- Learning Perl: 2.8. The chomp Operator
- 笔记
- C++ CFile::Rename 出现“拒绝访问”异常 及解决方案(不能说原创,自己悟出来的)
- JAVA的内省机制(introspector)与反射机制(reflection)
- 无电池的情况下,如何升级DELL INSPIRON 6400的bios
- Perl Learning: 2.9. The while Control Structure
- Perl Learning: 2.10. The undef Value