Scalar::Util::Refcount - Show an object's reference count


 my $foo = Bar->new;
 say refcount($foo); # prints "1"
 my $baz = $foo;
 say refcount($foo); # prints "2"


Provides two functions to show reference counts of Perl objects.

This can be handy when testing/debugging and you want to know what the heck is going on.

This is a trivial module. The documentation is longer than the code. Heck, the BSD license at the top of the file is longer than the code. It took longer to make it release-ready than it took to write it. It's handy, but seems almost not worth releasing.

I bet many of us have code like that in our personal libraries, and I can't help thinking that if more of us took the hour to clean it up, put it in a sensible namespace, write some documentation, and release it, the world would be a better place.


refcount ( obj )

Given a Perl scalar, return the object's reference count. If the passed scalar is a reference, it is dereferenced and the reference count of the referenced object is returned. Otherwise the reference count of the scalar itself is returned. This is normally what you want.

This function is exported by default.

refcount_sv ( scalar )

Returns the reference count of the given scalar, even if it's a reference. This is normally not what you want -- normally you'll have a reference to an object and want to know the reference count of the underlying object, not of the reference itself -- but sometimes it is.

This function is not exported by default.


There are no non-core dependencies. You will need a C compiler to install. Tested on Perl 5.10.0 and 5.8.8.

SWIG is not necessary to build, install, or use this module, but will be necessary if you want to modify it. See the README file.


Jeremy Nixon <>


Released under a BSD license (3-clause).