How to tell apart numeric scalars and string scalars in Perl?

前端 未结 9 1468
一生所求
一生所求 2020-12-16 12:31

Perl usually converts numeric to string values and vice versa transparently. Yet there must be something which allows e.g. Data::Dumper to discriminate between

9条回答
  •  渐次进展
    2020-12-16 12:49

    It is more complicated. Perl changes the internal representation of a variable depending on the context the variable is used in:

    perl -MDevel::Peek -e '
        $x = 1;    print Dump $x;
        $x eq "a"; print Dump $x;
        $x .= q(); print Dump $x;
    '
    SV = IV(0x794c68) at 0x794c78
      REFCNT = 1
      FLAGS = (IOK,pIOK)
      IV = 1
    SV = PVIV(0x7800b8) at 0x794c78
      REFCNT = 1
      FLAGS = (IOK,POK,pIOK,pPOK)
      IV = 1
      PV = 0x785320 "1"\0
      CUR = 1
      LEN = 16
    SV = PVIV(0x7800b8) at 0x794c78
      REFCNT = 1
      FLAGS = (POK,pPOK)
      IV = 1
      PV = 0x785320 "1"\0
      CUR = 1
      LEN = 16
    

提交回复
热议问题