GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages


Manual Reference Pages  -  DEVEL::REFCOUNT (3)

.ds Aq ’

NAME

"Devel::Refcount" - obtain the REFCNT value of a referent

CONTENTS

SYNOPSIS



 use Devel::Refcount qw( refcount );

 my $anon = [];

 print "Anon ARRAY $anon has " . refcount( $anon ) . " reference\n";

 my $otherref = $anon;

 print "Anon ARRAY $anon now has " . refcount( $anon ) . " references\n";

 assert_oneref $otherref; # This will throw an exception at runtime



DESCRIPTION

This module provides a single function which obtains the reference count of the object being pointed to by the passed reference value. It also provides a debugging assertion that asserts a given reference has a count of only 1.

FUNCTIONS

CW$count = refcount( CW$ref )

Returns the reference count of the object being pointed to by $ref.

assert_oneref( CW$ref )

Asserts that the given object reference has a reference count of only 1. If this is true the function does nothing. If it has more than 1 reference then an exception is thrown. Additionally, if Devel::FindRef is available, it will be used to print a more detailed trace of where the references are found.

Typically this would be useful in debugging to track down cases where objects are still being referenced beyond the point at which they are supposed to be dropped. For example, if an element is delete from a hash that ought to be the last remaining reference, the return value of the delete operator can be asserted on



 assert_oneref delete $self->{some_item};



If at the time of deleting there are any other references to this object then the assertion will fail; and if Devel::FindRef is available the other locations will be printed.

COMPARISON WITH SvREFCNT

This function differs from Devel::Peek::SvREFCNT in that SvREFCNT() gives the reference count of the SV object itself that it is passed, whereas refcount() gives the count of the object being pointed to. This allows it to give the count of any referent (i.e. ARRAY, HASH, CODE, GLOB and Regexp types) as well.

Consider the following example program:



 use Devel::Peek qw( SvREFCNT );
 use Devel::Refcount qw( refcount );

 sub printcount
 {
    my $name = shift;

    printf "%30s has SvREFCNT=%d, refcount=%d\n",
       $name, SvREFCNT( $_[0] ), refcount( $_[0] );
 }

 my $var = [];

 printcount Initially, $var, $var;

 my $othervar = $var;

 printcount Before CODE ref, $var, $var;
 printcount $othervar, $othervar;

 my $code = sub { undef $var };

 printcount After CODE ref, $var, $var;
 printcount $othervar, $othervar;



This produces the output



                Initially, $var has SvREFCNT=1, refcount=1
          Before CODE ref, $var has SvREFCNT=1, refcount=2
                      $othervar has SvREFCNT=1, refcount=2
           After CODE ref, $var has SvREFCNT=2, refcount=2
                      $othervar has SvREFCNT=1, refcount=2



Here, we see that SvREFCNT() counts the number of references to the SV object passed in as the scalar value - the $var or $othervar respectively, whereas refcount() counts the number of reference values that point to the referent object - the anonymous ARRAY in this case.

Before the CODE reference is constructed, both $var and $othervar have SvREFCNT() of 1, as they exist only in the current lexical pad. The anonymous ARRAY has a refcount() of 2, because both $var and $othervar store a reference to it.

After the CODE reference is constructed, the $var variable now has an SvREFCNT() of 2, because it also appears in the lexical pad for the new anonymous CODE block.

PURE-PERL FALLBACK

An XS implementation of this function is provided, and is used by default. If the XS library cannot be loaded, a fallback implementation in pure perl using the B module is used instead. This will behave identically, but is much slower.



        Rate   pp   xs
 pp 225985/s   -- -66%
 xs 669570/s 196%   --



SEE ALSO

o Test::Refcount - assert reference counts on objects

AUTHOR

Paul Evans <leonerd@leonerd.org.uk>
Search for    or go to Top of page |  Section 3 |  Main Index


perl v5.20.3 DEVEL::REFCOUNT (3) 2016-03-17

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.