perl:为什么Devel :: Refcount :: refcount和Devel :: Peek ::
我正在阅读
How can I access the ref count of a Perl hash?,并且建议使用Devel :: Refcount :: refcount和Devel :: Peek :: SvREFCNT.
但是他们没有返回相同的引用计数.这是为什么? 这里有一个来自perldoc Devel的修改示例:Refcount: use Devel::Peek; use Devel::Refcount; my $anon = []; printf "Anon ARRAY $anon has %d/%d referencen",Devel::Refcount::refcount($anon),Devel::Peek::SvREFCNT($anon); my $otherref = $anon; printf "Anon ARRAY $anon now has %d/%d referencesn",Devel::Peek::SvREFCNT($anon); 其中打印出来: Anon ARRAY ARRAY(0x8b10818) has 1/1 reference Anon ARRAY ARRAY(0x8b10818) now has 2/1 references 注意最后的2/1差异… (如果事实证明我没有做愚蠢的事情,我会添加从How can I access the ref count of a Perl hash?到这里的链接) 解决方法
我不能说我已经把它全部了,但你的问题在
Devel::Refcount perldoc年被突出显现
use Devel::Peek qw( SvREFCNT ); use Devel::Refcount qw( refcount ); sub printcount { my $name = shift; printf "%30s has SvREFCNT=%d,refcount=%dn",$name,SvREFCNT($_[0]),refcount($_[0]); } my $var = []; printcount 'Initially,$var',$var; my $othervar = $var; printcount 'Before CODE ref,$var; printcount '$othervar',$othervar; my $code = sub { undef $var }; printcount 'After CODE ref,$othervar;
Initially,$var has SvREFCNT=1,refcount=1 Before CODE ref,refcount=2 $othervar has SvREFCNT=1,refcount=2 After CODE ref,$var has SvREFCNT=2,refcount=2
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |