Perl 中是否有内置的 true/false 布尔值? [英] Is there a built-in true/false boolean value in Perl?

查看:63
本文介绍了Perl 中是否有内置的 true/false 布尔值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
如何在 Perl 中使用布尔变量?

[root@ ~]$ perl -e 'if(true){print 1}'
1
[root@ ~]$ perl -e 'if(false){print 1}'
1

我很惊讶 truefalse 都通过了 if...

I'm astonished both true and false passes the if...

推荐答案

如果你用 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 }

这篇关于Perl 中是否有内置的 true/false 布尔值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆