为什么Perl中使用空字符串重新present布尔值false值? [英] Why does Perl use the empty string to represent the boolean false value?

查看:208
本文介绍了为什么Perl中使用空字符串重新present布尔值false值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在一个标量(布尔)环境评估一个前pression,Perl使用显式值 1 如果EX pression计算为结果真实,空字符串如果EX pression计算为false。
我很好奇,为什么Perl使用空字符串重新present布尔值false值,而不是 0 这看起来更直观。

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.

请注意,我不关心用Perl对待空字符串作为一个错误的标量(布尔)上下文。

Note that I'm not concerned with Perl treating the empty string as a false in scalar (boolean) context.

修改

将如何使用字符串,它是真实的(为实例)作为一个字符串再假值presentation更改现有code的含义?我们可以说,code,这样的改变后变为语义是不太可靠的/正确的比它本来可以?我猜串上下文是在Perl如此普遍,导致语义理智唯一的选择是,如果布尔值preserve其轮跳闸,并从一个字符串...

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...

推荐答案

的各种逻辑运算符不返回一个空字符串,返回他们在所有三个简单的标量类型的虚假或真实价值。它只是看起来像它返回一个空字符串,因为打印迫使其参数字符串上下文中:

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:

#!/usr/bin/perl

use strict;
use warnings;

use Devel::Peek;

my $t = 5 > 4;
my $f = 5 < 4;

Dump $t;
Dump $f;

输出:

SV = PVNV(0x100802c20) at 0x100827348
  REFCNT = 1
  FLAGS = (PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1
  PV = 0x100201e60 "1"\0
  CUR = 1
  LEN = 16
SV = PVNV(0x100802c40) at 0x100827360
  REFCNT = 1
  FLAGS = (PADMY,IOK,NOK,POK,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x100208ca0 ""\0
  CUR = 0
  LEN = 16

对于那些不熟悉的Perl 5内部,一个 PVNV 是包含所有三个简单的标量类型(整数 IV ,双precision浮动 NV 和字符串光伏)。标志 IOK NOK POK 意味着整数,双精度和字符串值都在同步信号(用于同步定义一些),所以他们中的任何一个都可以被使用(即不转换需要的,如果你把它作为一个整数,双或字符串发生)。

For those not familiar with the Perl 5 internals, a PVNV is a scalar structure that holds all three simple scalar types (integer IV, double precision float NV, and string PV). The flags IOK, NOK, and POK 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).

我假定被选作虚假字符串空字符串,因为它<击>较小,更多的是与虚假的字符串比想法保持0。忽略它是小我的发言,既1大小都一样:十六个字符。它说,在转储这样的权利。 Perl 5中增加了额外的空间为字符串,让他们快速成长。

I assume the empty string was chosen for the false string because it is smaller and is 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.

哦,我恨你。在研究这个,我发现,我在 perlopquick 撒了谎,现在将必须找到一种方法来解决它。只要你曾经像所有其他的羊,只接受Perl 5的表面怪事事实上,我会少工作要做。

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.

这篇关于为什么Perl中使用空字符串重新present布尔值false值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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