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

eal*_*eon 3 perl

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
Run Code Online (Sandbox Code Playgroud)

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

ike*_*ami 8

  • ""并且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
    
    Run Code Online (Sandbox Code Playgroud)
  • 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.
    
    Run Code Online (Sandbox Code Playgroud)
  • (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
    
    Run Code Online (Sandbox Code Playgroud)