-
StackOverflow 文件
-
Perl Language 教程
-
真假
-
真值和假值列表
use feature qw( say );
# Numbers are true if they're not equal to 0.
say 0 ? 'true' : 'false'; # false
say 1 ? 'true' : 'false'; # true
say 2 ? 'true' : 'false'; # true
say -1 ? 'true' : 'false'; # true
say 1-1 ? 'true' : 'false'; # false
say 0e7 ? 'true' : 'false'; # false
say -0.00 ? 'true' : 'false'; # false
# Strings are true if they're not empty.
say 'a' ? 'true' : 'false'; # true
say 'false' ? 'true' : 'false'; # true
say '' ? 'true' : 'false'; # false
# Even if a string would be treated as 0 in numeric context, it's true if nonempty.
# The only exception is the string "0", which is false.
# To force numeric context add 0 to the string
say '0' ? 'true' : 'false'; # false
say '0.0' ? 'true' : 'false'; # true
say '0e0' ? 'true' : 'false'; # true
say '0 but true' ? 'true' : 'false'; # true
say '0 whargarbl' ? 'true' : 'false'; # true
say 0+'0 argarbl' ? 'true' : 'false'; # false
# Things that become numbers in scalar context are treated as numbers.
my @c = ();
my @d = (0);
say @c ? 'true' : 'false'; # false
say @d ? 'true' : 'false'; # true
# Anything undefined is false.
say undef ? 'true' : 'false'; # false
# References are always true, even if they point at something false
my @c = ();
my $d = 0;
say \@c ? 'true' : 'false'; # true
say \$d ? 'true' : 'false'; # true
say \0 ? 'true' : 'false'; # true
say \'' ? 'true' : 'false'; # true