Why is '0' false in Perl?

后端 未结 6 1775
故里飘歌
故里飘歌 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条回答
  •  旧时难觅i
    2021-01-05 02:25

    If you read that it has anything to do with <>, you are reading bad sources; I can't imagine where that claim would come from.

    '0' is false because almost everywhere perl doesn't distinguish between numbers stored in numeric form and in string form; doing so in boolean tests would run against this grain.

    In languages (Python, Javascript?) where '0' is true, I find it odd that '' is false; why make an exception for one string but not the other?

提交回复
热议问题