为什么 Perl 使用空字符串来表示布尔 false 值?
在标量(布尔)上下文中计算表达式时,如果表达式计算结果为 true,则 Perl 使用显式值 1
作为结果;如果表达式计算结果为 false,则使用空字符串。 我很好奇为什么 Perl 使用空字符串来表示布尔假值,而不是看起来更直观的 0
。
请注意,我不关心 Perl 在标量(布尔)上下文中将空字符串视为 false。
编辑
使用 true 字符串(例如 "false"
)作为 false 值的字符串表示将如何改变现有代码的含义?我们是否可以说,在进行此类更改后更改语义的代码不如其应有的那样健壮/正确?我猜字符串上下文在 Perl 中是如此普遍,以至于导致理智语义的唯一选择是布尔值在往返于字符串之后是否保留其值......
When evaluating an expression in a scalar (boolean) context, Perl uses the explicit value 1
as a result if the expression evaluates to true and the empty string if the expression evaluates to false.
I'm curious why Perl uses the empty string to represent boolean false value and not 0
which seems more intuitive.
Note that I'm not concerned with Perl treating the empty string as a false in scalar (boolean) context.
EDIT
How would using string which is true ("false"
for instance) as a string representation of false values change the meaning of existing code? Could we say that code that changes semantics after such a change is less robust/correct than it could have been? I guess string context is so pervasive in Perl that the only option leading to sane semantics is if boolean value preserve its value after round tripping to and from a string...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
各种逻辑运算符不返回空字符串,它们在所有三种简单标量类型中返回 false 或 true 值。它看起来只是返回一个空字符串,因为
print
在其参数上强制使用字符串上下文:输出:
对于那些不熟悉 Perl 5 内部结构的人来说,
PVNV
是一个标量包含所有三种简单标量类型(整数IV
、双精度浮点NV
和字符串PV
)的结构。标志IOK
、NOK
和POK
表示整数、双精度和字符串值全部同步(对于同步的某些定义) )因此可以使用其中任何一个(即,如果将其用作整数、双精度或字符串,则无需进行转换)。我假设选择空字符串作为假字符串,因为它
更小并且比“0”
更符合假字符串的概念。忽略我关于它更小的说法,""
和"1"
大小相同:十六个字符。垃圾场里就是这么说的。 Perl 5 为字符串添加了额外的空间,以允许它们快速增长。哦,我恨你。在研究这个问题时,我发现我在
perlopquick
中撒了谎,现在将必须找到一种方法来解决它。如果您像所有其他绵羊一样并且接受 Perl 5 表面上的怪异作为事实,那么我要做的工作就会更少。编辑部分问题的答案:
PL_sv_yes 和 PL_sv_no(比较运算符返回的规范 true 和 false 值)的唯一特殊之处在于它们是只读的,并且是由
perl
创建的,而不是由正在运行的程序创建的。如果更改它们,不会更改真实性测试,因此设置为"false"
的 PL_sv_no 将被视为 true。您甚至可以使用perl
未记录的功能自行执行此操作(此代码在 Perl 5.18 和最新 Perl 之间的某个时刻停止工作):输出
这是因为真实性测试首先查看字符串。
就会直接被打碎。即使您限制自己将其设置为 int 0 或字符串“0”(两者都是 false),它也会破坏一些有效代码。
是的。
The various logical operators don't return an empty string, they return a false or true value in all three simple scalar types. It just looks like it returns an empty string because
print
forces a string context on its arguments:Output:
For those not familiar with the Perl 5 internals, a
PVNV
is a scalar structure that holds all three simple scalar types (integerIV
, double precision floatNV
, and stringPV
). The flagsIOK
,NOK
, andPOK
mean that the integer, double, and string values are all in sync (for some definition of in sync) so any one of them may be used (i.e. no conversions need to take place if you use it as an integer, double, or string).I assume the empty string was chosen for the false string because it
is smaller andis more in keeping with the idea of a false string than"0"
. Ignore my statement about it being smaller, both""
and"1"
are the same size: sixteen characters. It says so right in the dump. Perl 5 adds extra space to strings to allow them to grow quickly.Oh, and I hate you. In researching this I have found that I have lied in
perlopquick
and will now have to find a way to fix it. If only you had been like all of the other sheep and just accepted Perl 5's surface weirdness as fact, I would have less work to do.Answers to the questions in the EDIT section:
The only special things about about PL_sv_yes and PL_sv_no (the canonically true and false values returned by comparison operators) are that they are read only and are created by
perl
not the program that is running. If you change them, it does not change the truthiness test, so a PL_sv_no that is set to"false"
will be treated as true. You can even do this yourself (this code stops working at some point between Perl 5.18 and the latest Perl) using undocumented features ofperl
:outputs
This is because the truthiness test looks at strings first.
It will be straight up broken. Even if you restrict yourself to setting it to an int 0 or a string "0" (both of which are false), it will break some valid code.
Yes.
您可以重载 true、false 和 undef 的字符串化,例如
You can overload the stringification of true, false and undef, like this:
Perl 中的错误不只是
""
< /a>.至于为什么......要么是因为 Perl 很棒,要么很糟糕——取决于你的个人喜好:)It's not just
""
that's false in Perl. As for why... it's either because Perl is awesome or terrible -- depending on your personal preferences :)在 Perl 中,数字 0 和空字符串最终都计算为 false。我认为这是语言设计的问题。当编写自己的代码时,您当然可以假设任何一种错误的编码约定。
有关更多详细信息,请查看“如何在 Perl 中使用布尔变量? ”。
Both number 0 and empty string ultimately evaluate as false in Perl. I think this is a matter of language design. When writing your own code, you can of course assume any which one false encoding convention.
For further details, check out "How do I use boolean variables in Perl?".
以下是我解决该问题的方法:
*1
将($a eq $b)
生成的布尔值转换为标量。Here is how I got around the problem:
The
*1
converts the boolean resulting from($a eq $b)
into a scalar.