Jul 24, 2001, 10:36 PM
Post #2 of 4
It is 3, at least with my HP-UX build of Perl 5.6.0. I'm not sure if there's an easier way to get the value of the reference counter, but mine isn't too hard anyway. (Took me only some minutes.)
If you have a look at the perlguts manpage, there's a section Reference Counts and Mortality. There's also an internal routine to retrieve the reference counter value of an SV called SvREFCNT. With this knowledge, one can easily write a small XSUB with the following code:
You can then use this XSUB from some Perl code like this:
MODULE = RefCount PACKAGE = RefCount
GetRefCount( sv )
RETVAL = SvREFCNT( sv );
And in this case, it will print RefCount = 3.
$a = 123;
$b = \$a;
$c = $b;
printf "RefCount = %d\n", RefCount::GetRefCount( $a );
There are also some interesting resources besides the perlguts manpage on the web. There is for example an article called Perl Guts Illustrated that graphically illustrates Perl's internal data types. Also, there's interesting reply by Nathan Torkington concerning circular references and the related problems.
Hope this helps.