http://qs321.pair.com?node_id=652972


in reply to The number of references to a variable

The SvREFCNT macro (perlguts) returns this number. I am sure this is already somewhere on cpan, but a simple XS routine could look like this:
/* generated by h2xs -c Scalar::RefCount */ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" MODULE = Scalar::RefCount PACKAGE = Scalar::RefCount int refcount(s) SV* s PROTOTYPE: $ CODE: RETVAL = SvREFCNT(s); OUTPUT: RETVAL
use Test::More tests => 6; BEGIN { use_ok('Scalar::RefCount') }; ######################### my $a; is(Scalar::RefCount::refcount($a),1, 'return 1'); my $b = \$a; is(Scalar::RefCount::refcount($a),2, 'return 2'); my $c = \$a; is(Scalar::RefCount::refcount($a),3, 'return 3'); { my $d = \$a; is(Scalar::RefCount::refcount($a),4, 'return 4'); } is(Scalar::RefCount::refcount($a),3, 'return 3');
Update: Ah, thanks cdarke.