且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Perl 中是否有内置的 true/false 布尔值?

更新时间:2023-10-15 12:40:58

如果你用 strict 运行它:

If you run it with strict:

perl -Mstrict -e 'if(true) { print 1 }'

你会明白原因:

Bareword "true" not allowed while "strict subs" in use at -e line 1.

它被解释为字符串 "true""false" 始终为真.Perl 中未定义常量,但您可以自己定义:

It is interpreted as string "true" or "false" which is always true. The constants are not defined in Perl, but you can do it yourself:

use constant { true => 1, false => 0 };
if(false) { print 1 }