Perl初始化值:“”vs q {}和0 vs(1 == 2)

前端之家收集整理的这篇文章主要介绍了Perl初始化值:“”vs q {}和0 vs(1 == 2)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
my $EMPTY = q{};
use constant TRUE  => ( 1==1 );
use constant FALSE => ( 1==2 );


my $a = "";   vs my $a = $EMPTY;             
my $b = 0;    vs my $b = FALSE

应该使用哪种方法有什么区别?
这取决于某些情况吗?
如果是这样的话,那些关于你何时想要使用我的$b = 0的情况是什么;我的$b = FALSE,反之亦然?

解决方法

>“”和q {}都生成零长度字符串.

>perl -MDevel::Peek -e"$_ = q{}; Dump($_);"
SV = PV(0x306748) at 0x4c9058
  REFCNT = 1
  FLAGS = (POK,pPOK)            <--- Contains a string.
  PV = 0x4ac9e8 ""\0
  CUR = 0                       <--- Length of the string is zero.
  LEN = 16

> 0产生数字零.

>perl -MDevel::Peek -e"$_ = 0; Dump($_)"
SV = IV(0x229088) at 0x229098
  REFCNT = 1
  FLAGS = (IOK,pIOK)            <--- Contains a signed integer.
  IV = 0                        <--- Contained integer is zero.

>(1 == 2)生成标量包含空字符串且数值为零.

>perl -MDevel::Peek -e"$_ = 1==2; Dump($_);"
SV = PVNV(0x1fc598) at 0x259038
  REFCNT = 1
  FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK)   <--- A signed int,a float and a str.
  IV = 0                        <--- Contained integer is zero.
  NV = 0                        <--- Contained floating point number is zero.
  PV = 0x23ca18 ""\0
  CUR = 0                       <--- Length of the string is zero.
  LEN = 16

猜你在找的Perl相关文章