Why is '0' false in Perl?

后端 未结 6 1792
故里飘歌
故里飘歌 2021-01-05 02:14

First of all, I come from C, Java, Python background. Recently, I started learning Perl for my new job and I got curious about \'0\' (0-string) being false.

6条回答
  •  暖寄归人
    2021-01-05 02:16

    Internal storage format, ideally, should never matter in Perl. Zero is zero is zero, no matter whether it's stored in an IV, UV, NV, PV, etc.

    In fact, because it can, Perl will change the format in which values are stored in ways that might surprise you. If string 0 was true, the following would print "true":

    $x = 0;
    print("$x\n");
    if ($x) {
       print("true\n");
    } else {
       print("false\n");
    }
    

提交回复
热议问题