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

Perl 内部结构详解 -- PerlGuts Illustrated (6 CV),第1张

概述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 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) "$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) "$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) "$b"    OUTSIDE = 0x299094 (MAIN)
词法变量(my & our)会设置SVs_PADMY/SVs_PADOUR标记,目标设置SVs_PADTMP标记。 总结

以上是内存溢出为你收集整理的Perl 内部结构详解 -- PerlGuts Illustrated (6 CV)全部内容,希望文章能够帮你解决Perl 内部结构详解 -- PerlGuts Illustrated (6 CV)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1293047.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存