Perl 内部结构详解 -- PerlGuts Illustrated (6 CV)

CV

code value,函数的结构,如下图:


下边示例可以看到几个主要参数:

函数所在的Stash & GV。

函数的参数表和临时变量表PADLIST。

D:\Tmp>perl -MDevel::Peek -e "sub a{my ($a,$b,$t); } Dump \&a"
SV = RV(0x3a90f0) at 0x3a90e4
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x285a864
  SV = PVCV(0x285923c) at 0x285a864
    REFCNT = 2
    FLAGS = ()
    COMP_STASH = 0x3a8fd4	"main"
    START = 0x2885fa8 ===> 0
    ROOT = 0x2885f64
    GVGV::GV = 0x285abd4	"main" :: "a"
    FILE = "-e"
    DEPTH = 0
    FLAGS = 0x0
    OUTSIDE_SEQ = 96
    PADLIST = 0x285a874
    PADNAME = 0x285a8a4(0x2865424) PAD = 0x285a974(0x288292c)
       1. 0x285a8c4<1> (96,97) "$a"
       2. 0x285a894<1> (96,97) "$b"
       3. 0x285a904<1> (96,97) "$t"
    OUTSIDE = 0x3a9274 (MAIN)


PAD

PAD是一个列表,为每个函数存储局部变量。第0个slot 存储参数名字,运行时产生新的AV存储实际参数,DEPTH也加深,递归函数观察效果明显。

use Devel::Peek;
use Data::Dumper;

sub test{
	my ($a,$b) = @_;
	Dump \&test;
	test(++$a,--$b) if $b>0;
}

Dump \&test;
test(1,2);


输出:
SV = RV(0x298f50) at 0x298f44
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x182a834
  SV = PVCV(0x1829024) at 0x182a834
    REFCNT = 2
    FLAGS = ()
    COMP_STASH = 0x298e34	"main"
    START = 0x18ba94c ===> 0
    ROOT = 0x18ba4cc
    GVGV::GV = 0x184b34c	"main" :: "test"
    FILE = "tmp.pl"
    DEPTH = 0
    FLAGS = 0x0
    OUTSIDE_SEQ = 535
    PADLIST = 0x182a864
    PADNAME = 0x182a934(0x18c7c6c) PAD = 0x182a8c4(0x18c7be4)
       1. 0x182a854<1> (535,536) "$a"
       2. 0x182a884<1> (535,536) "$b"
    OUTSIDE = 0x299094 (MAIN)
SV = RV(0x298f50) at 0x298f44
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x182a834
  SV = PVCV(0x1829024) at 0x182a834
    REFCNT = 4
    FLAGS = ()
    COMP_STASH = 0x298e34	"main"
    START = 0x18ba94c ===> 0
    ROOT = 0x18ba4cc
    GVGV::GV = 0x184b34c	"main" :: "test"
    FILE = "tmp.pl"
    DEPTH = 1
    FLAGS = 0x0
    OUTSIDE_SEQ = 535
    PADLIST = 0x182a864
    PADNAME = 0x182a934(0x18c7c6c) PAD = 0x182a8c4(0x18c7be4)
       1. 0x182a854<1> (535,536) "$a"
       2. 0x182a884<1> (535,536) "$b"
    OUTSIDE = 0x299094 (MAIN)
SV = RV(0x1899240) at 0x1899234
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x182a834
  SV = PVCV(0x1829024) at 0x182a834
    REFCNT = 4
    FLAGS = ()
    COMP_STASH = 0x298e34	"main"
    START = 0x18ba94c ===> 0
    ROOT = 0x18ba4cc
    GVGV::GV = 0x184b34c	"main" :: "test"
    FILE = "tmp.pl"
    DEPTH = 2
    FLAGS = 0x0
    OUTSIDE_SEQ = 535
    PADLIST = 0x182a864
    PADNAME = 0x182a934(0x18c7c6c) PAD = 0x182a8c4(0x18c7be4)
       1. 0x182a854<1> (535,536) "$a"
       2. 0x182a884<1> (535,536) "$b"
    OUTSIDE = 0x299094 (MAIN)
SV = RV(0x1863a28) at 0x1863a1c
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x182a834
  SV = PVCV(0x1829024) at 0x182a834
    REFCNT = 4
    FLAGS = ()
    COMP_STASH = 0x298e34	"main"
    START = 0x18ba94c ===> 0
    ROOT = 0x18ba4cc
    GVGV::GV = 0x184b34c	"main" :: "test"
    FILE = "tmp.pl"
    DEPTH = 3
    FLAGS = 0x0
    OUTSIDE_SEQ = 535
    PADLIST = 0x182a864
    PADNAME = 0x182a934(0x18c7c6c) PAD = 0x182a8c4(0x18c7be4)
       1. 0x182a854<1> (535,536) "$a"
       2. 0x182a884<1> (535,536) "$b"
    OUTSIDE = 0x299094 (MAIN)

词法变量(my & our)会设置SVs_PADMY/SVs_PADOUR标记,目标设置SVs_PADTMP标记。


你可能感兴趣的:(Perl 内部结构详解 -- PerlGuts Illustrated (6 CV))