( 1==1 ); use constant FALSE => ( 1==2 ); my $a = ""; vs my-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:03:15 25 4
gpt4 key购买 nike

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

应该使用哪个而不是什么有什么区别吗?
它取决于某些情况吗?
如果是这样,关于您何时希望使用 my $b = 0; 的情况是什么?在 my $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 初始化值 : "" vs q{} and 0 vs (1==2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23373129/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com