什么返回布尔Perl函数返回实际

什么返回布尔Perl函数返回实际

本文介绍了什么返回布尔Perl函数返回实际的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

href=\"http://perldoc.perl.org/functions/defined.html\">定义功能(以及其他许多)返回布尔值Perl的

The Perl defined function (and many others) returns "a Boolean value".

由于Perl不会实际上有一个布尔类型(并使用像值的 1 的对的真正的和的 0 UNDEF 的对的的)不Perl语言明确指定返回一个布尔值?例如,将定义(民主)回报的 0 民主基金的,是不是随时更改?

Given Perl doesn't actually have a Boolean type (and uses values like 1 for true, and 0 or undef for false) does the Perl language specify exactly what is returned for a Boolean values? For example, would defined(undef) return 0 or undef, and is it subject to change?

推荐答案

在几乎所有情况下(即除非有一个理由不这样做),Perl中返回两个静态分配的标量之一:&安培; PL_sv_yes (真正的)和&放大器; PL_sv_no (假)。这是他们在细节:

In almost all cases (i.e. unless there's a reason to do otherwise), Perl returns one of two statically allocated scalars: &PL_sv_yes (for true) and &PL_sv_no (for false). This is them in detail:

>perl -MDevel::Peek -e"Dump 1==1"
SV = PVNV(0x749be4) at 0x3180b8
  REFCNT = 2147483644
  FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 1
  NV = 1
  PV = 0x742dfc "1"\0
  CUR = 1
  LEN = 12

>perl -MDevel::Peek -e"Dump 1==0"
SV = PVNV(0x7e9bcc) at 0x4980a8
  REFCNT = 2147483647
  FLAGS = (PADTMP,IOK,NOK,POK,READONLY,pIOK,pNOK,pPOK)
  IV = 0
  NV = 0
  PV = 0x7e3f0c ""\0
  CUR = 0
  LEN = 12

是一个三元VAR(IOK,挪威克朗和POK)。它包含一个带符号的整数(IV)等于1时,一个浮点数(NV)等于1,和一个字符串(PV)等于1。

yes is a triple var (IOK, NOK and POK). It contains a signed integer (IV) equal to 1, a floating point number (NV) equal to 1, and a string (PV) equal to 1.

没有也是三VAR(IOK,挪威克朗和POK)。它包含一个带符号的整数(IV)等于0,浮点数(NV)等于0,和一个空串(PV)。这意味着stringifies为空字符串,并将其numifies为0。这既不等同于空字符串

no is also a triple var (IOK, NOK and POK). It contains a signed integer (IV) equal to 0, a floating point number (NV) equal to 0, and an empty string (PV). This means it stringifies to the empty string, and it numifies to 0. It is neither equivalent to an empty string

>perl -wE"say 0+(1==0);"
0

>perl -wE"say 0+'';"
Argument "" isn't numeric in addition (+) at -e line 1.
0

也没有 0

>perl -wE"say ''.(1==0);"


>perl -wE"say ''.0;"
0

有没有保证,这将永远是这样。而且也没有理由就靠这个。如果你需要特定的值,可以使用类似

There's no guarantee that this will always remain the case. And there's no reason to rely on this. If you need specific values, you can use something like

my $formatted = $result ? '1' : '0';

这篇关于什么返回布尔Perl函数返回实际的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:54