PerlのTruth/Falsehoodメモ

Truth in Perl is always evaluated in a scalar context.

  1. Any string is true except for "" and "0".
  2. Any number is true except for 0.
  3. Any reference is true.
  4. Any undefined value is false.

Programming Perl Chapter 1より一部抜粋

Truth and Falsehood

The number 0, the strings '0' and '' , the empty list () , and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as '' , but as a number, it is treated as 0.

perlsyn - perldoc.perl.orgより引用

よって、以下の結果

0          # false
1          # true
10 - 10    # false
0.00       # false
"0"        # false
""         # false
"0.00"     # true!!!
"0.00" + 0 # false
\$a        # true
undef()    # false

"0.00"がtrueで"0.00" + 0がfalse。これは気をつけないとはまってしまいそうな気がしたのでメモ。