In depth study of true and false in Perl


Perl believes that the truth value is self-evident, which means that the truth value of anything can be calculated. Perl defines the truth value in a practical way, that is, the truth value of an entity depends on the type of entity. Perl is always optimistic that there are more real things than fake things in the world.

Perl is different from any other computer language. Perl is created by linguists, and the meaning of language is inseparable from the context, so the truth value in Perl can be calculated in scalar (scalar $and array @ are similar to singular and plural in English, and the difference between book and books. In the real world, the truth value should be singular, so it is scalar), No type of coercion will be done (for example, int (’42 ‘) in Python will convert the character type containing numbers to int type, and (int)’d’ in Java will convert the character type to integer type).

For various types of values in scalars, the rules are as follows:

Character type: all characters are true except ” and ‘0’
Numeric: all numbers except 0 are true
Referential: all references are true (all references will point to an object with an address, which will never be 0 because it must be defined)
Undefined: all undefined values are false

The following examples can well understand the concepts of true and false in Perl:

Copy codeThe code is as follows:

0            # Will be converted to string ‘0’, so it is false  
one            # Will be converted to string ‘1’, so it is true  
100 – 100    # 100-100 equals 0, which will be converted to the string “0”, so it is false  
zero         # Equal to 0, will be converted to the string “0”, so it is false  
“0”          # String ‘0’, so it is false  
“”           # This is an empty string, so it is false  
“0.00”       # Neither “” nor “0”, so it is true  
“0.00” + 0   # Cast by + and the calculation result is 0, so it is false  
\$a          # A reference to scalar $a, so it is true, even if $a is false.  
undef()      # Is a function that returns an undefined value, so it is false