| 1 |
NAME |
| 2 |
`Devel::Refcount' - obtain the REFCNT value of a referent |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Devel::Refcount qw( refcount ); |
| 6 |
|
| 7 |
my $anon = []; |
| 8 |
|
| 9 |
print "Anon ARRAY $anon has " . refcount( $anon ) . " reference\n"; |
| 10 |
|
| 11 |
my $otherref = $anon; |
| 12 |
|
| 13 |
print "Anon ARRAY $anon now has " . refcount( $anon ) . " references\n"; |
| 14 |
|
| 15 |
assert_oneref $otherref; # This will throw an exception at runtime |
| 16 |
|
| 17 |
DESCRIPTION |
| 18 |
This module provides a single function which obtains the reference count |
| 19 |
of the object being pointed to by the passed reference value. It also |
| 20 |
provides a debugging assertion that asserts a given reference has a |
| 21 |
count of only 1. |
| 22 |
|
| 23 |
FUNCTIONS |
| 24 |
$count = refcount( $ref ) |
| 25 |
Returns the reference count of the object being pointed to by $ref. |
| 26 |
|
| 27 |
assert_oneref( $ref ) |
| 28 |
Asserts that the given object reference has a reference count of only 1. |
| 29 |
If this is true the function does nothing. If it has more than 1 |
| 30 |
reference then an exception is thrown. Additionally, if Devel::FindRef |
| 31 |
is available, it will be used to print a more detailed trace of where |
| 32 |
the references are found. |
| 33 |
|
| 34 |
Typically this would be useful in debugging to track down cases where |
| 35 |
objects are still being referenced beyond the point at which they are |
| 36 |
supposed to be dropped. For example, if an element is delete from a hash |
| 37 |
that ought to be the last remaining reference, the return value of the |
| 38 |
`delete' operator can be asserted on |
| 39 |
|
| 40 |
assert_oneref delete $self->{some_item}; |
| 41 |
|
| 42 |
If at the time of deleting there are any other references to this object |
| 43 |
then the assertion will fail; and if `Devel::FindRef' is available the |
| 44 |
other locations will be printed. |
| 45 |
|
| 46 |
COMPARISON WITH SvREFCNT |
| 47 |
This function differs from `Devel::Peek::SvREFCNT' in that SvREFCNT() |
| 48 |
gives the reference count of the SV object itself that it is passed, |
| 49 |
whereas refcount() gives the count of the object being pointed to. This |
| 50 |
allows it to give the count of any referent (i.e. ARRAY, HASH, CODE, |
| 51 |
GLOB and Regexp types) as well. |
| 52 |
|
| 53 |
Consider the following example program: |
| 54 |
|
| 55 |
use Devel::Peek qw( SvREFCNT ); |
| 56 |
use Devel::Refcount qw( refcount ); |
| 57 |
|
| 58 |
sub printcount |
| 59 |
{ |
| 60 |
my $name = shift; |
| 61 |
|
| 62 |
printf "%30s has SvREFCNT=%d, refcount=%d\n", |
| 63 |
$name, SvREFCNT( $_[0] ), refcount( $_[0] ); |
| 64 |
} |
| 65 |
|
| 66 |
my $var = []; |
| 67 |
|
| 68 |
printcount 'Initially, $var', $var; |
| 69 |
|
| 70 |
my $othervar = $var; |
| 71 |
|
| 72 |
printcount 'Before CODE ref, $var', $var; |
| 73 |
printcount '$othervar', $othervar; |
| 74 |
|
| 75 |
my $code = sub { undef $var }; |
| 76 |
|
| 77 |
printcount 'After CODE ref, $var', $var; |
| 78 |
printcount '$othervar', $othervar; |
| 79 |
|
| 80 |
This produces the output |
| 81 |
|
| 82 |
Initially, $var has SvREFCNT=1, refcount=1 |
| 83 |
Before CODE ref, $var has SvREFCNT=1, refcount=2 |
| 84 |
$othervar has SvREFCNT=1, refcount=2 |
| 85 |
After CODE ref, $var has SvREFCNT=2, refcount=2 |
| 86 |
$othervar has SvREFCNT=1, refcount=2 |
| 87 |
|
| 88 |
Here, we see that SvREFCNT() counts the number of references to the SV |
| 89 |
object passed in as the scalar value - the $var or $othervar |
| 90 |
respectively, whereas refcount() counts the number of reference values |
| 91 |
that point to the referent object - the anonymous ARRAY in this case. |
| 92 |
|
| 93 |
Before the CODE reference is constructed, both $var and $othervar have |
| 94 |
SvREFCNT() of 1, as they exist only in the current lexical pad. The |
| 95 |
anonymous ARRAY has a refcount() of 2, because both $var and $othervar |
| 96 |
store a reference to it. |
| 97 |
|
| 98 |
After the CODE reference is constructed, the $var variable now has an |
| 99 |
SvREFCNT() of 2, because it also appears in the lexical pad for the new |
| 100 |
anonymous CODE block. |
| 101 |
|
| 102 |
PURE-PERL FALLBACK |
| 103 |
An XS implementation of this function is provided, and is used by |
| 104 |
default. If the XS library cannot be loaded, a fallback implementation |
| 105 |
in pure perl using the `B' module is used instead. This will behave |
| 106 |
identically, but is much slower. |
| 107 |
|
| 108 |
Rate pp xs |
| 109 |
pp 225985/s -- -66% |
| 110 |
xs 669570/s 196% -- |
| 111 |
|
| 112 |
SEE ALSO |
| 113 |
* Test::Refcount - assert reference counts on objects |
| 114 |
|
| 115 |
AUTHOR |
| 116 |
Paul Evans <[email protected]> |
| 117 |
|