Perl の truth

Programming Perl Chapter 1 より。

Truth in Perl is always evaluated in a scalar context.

  • Any string is true except for "" and "0".
  • Any number is true except for 0.
  • Any reference is true.
  • Any undefined value is false.

どう判定されるか

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.

http://perldoc.perl.org/perlsyn.html#Truth-and-Falsehood

よって、以下の結果

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。これは嵌りポイント。