我正在尝试修复我的代码以使Perl能够通过削弱引用和打破周期来恢复不需要的数据.
我最近在How to access Perl ref counts问了一个问题,答案对我来说效果很好.
对于我的一些对象,引用计数是> 1,我不知道为什么.
有没有办法让我添加一个回调或什么来帮助我知道引用计数何时递增?我想知道谁在引用一个对象.
解决方法
实现一个Devel :: XXX包来检查你的对象的refcounts?
package Devel::Something; # just emulating Devel::Trace here # see http://cpansearch.perl.org/src/MJD/Devel-Trace-0.10/Trace.pm sub DB::DB { if ($Devel::Something::CHECK) { my ($package,$file,$linenumber) = caller; ... inspect current refcounts ... if any have changed,print out the details ... including current package/file/linenumber $Devel::Something::CHECK = 0; # disable until it's enabled again } } 1;
# my program ... do some stuff ... $Devel::Something::CHECK = 1; ... do some more stuff ... $Devel::Something::CHECK = 1;
$perl -d:Something my_program.pl ...
您可以在整个代码中的适当位置撒上$Devel :: Something :: CHECK = 1语句,或者更改DB :: DB中的条件以定期运行(例如,if($Devel :: Something :: CHECK%100) == 0){每100次陈述评估后检查一次).