in reply to Reference Count of a Non-Scalar

bbfu,

A little Inline::C could do the trick. We could check if the passed in SV is a reference, if so, dereference it and get the SvREFCNT of that. The only issue is to remember that by passing it as \@array we up the reference count.

#!/usr/local/bin/perl -w use Inline C; use strict; my( @x, $z, $r ); @x = qw( a b c d e f g ); $z = \@x; $r = ref_count( \@x ); print "Ref count is $r - expect 3 - initial, ref, and pass ref\n"; $r = ref_count( $z ); print "Ref count is $r - expect 2 - initial, and ref\n"; __END__ __C__ #include <stdio.h> U32 ref_count ( SV * val ) { SV *y; y = SvROK(val) ? SvRV( val ) : val; return SvREFCNT( y ); }

-derby

Replies are listed 'Best First'.
Re^2: Reference Count of a Non-Scalar
by Anonymous Monk on May 25, 2005 at 16:27 UTC
    After poking around a bit (and finding that Inline::C is broken on my nearest installation) I found this, which seems to do something similar to what I want. Is it right? I'm playing with toys I don't fully understand.
    use B 'svref_2object'; my $objref = {}; my $another = $objref; print svref_2object($objref)->REFCNT;
    This will print 2, as expected.