1Devel::Refcount(3)    User Contributed Perl Documentation   Devel::Refcount(3)
2
3
4

NAME

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

SYNOPSIS

9        use Devel::Refcount qw( refcount );
10
11        my $anon = [];
12
13        print "Anon ARRAY $anon has " . refcount($anon) . " reference\n";
14
15        my $otherref = $anon;
16
17        print "Anon ARRAY $anon now has " . refcount($anon) . " references\n";
18

DESCRIPTION

20       This module provides a single function which obtains the reference
21       count of the object being pointed to by the passed reference value.
22

FUNCTIONS

24   $count = refcount($ref)
25       Returns the reference count of the object being pointed to by $ref.
26

COMPARISON WITH SvREFCNT

28       This function differs from "Devel::Peek::SvREFCNT" in that SvREFCNT()
29       gives the reference count of the SV object itself that it is passed,
30       whereas refcount() gives the count of the object being pointed to. This
31       allows it to give the count of any referent (i.e. ARRAY, HASH, CODE,
32       GLOB and Regexp types) as well.
33
34       Consider the following example program:
35
36        use Devel::Peek qw( SvREFCNT );
37        use Devel::Refcount qw( refcount );
38
39        sub printcount
40        {
41           my $name = shift;
42
43           printf "%30s has SvREFCNT=%d, refcount=%d\n",
44              $name, SvREFCNT($_[0]), refcount($_[0]);
45        }
46
47        my $var = [];
48
49        printcount 'Initially, $var', $var;
50
51        my $othervar = $var;
52
53        printcount 'Before CODE ref, $var', $var;
54        printcount '$othervar', $othervar;
55
56        my $code = sub { undef $var };
57
58        printcount 'After CODE ref, $var', $var;
59        printcount '$othervar', $othervar;
60
61       This produces the output
62
63                       Initially, $var has SvREFCNT=1, refcount=1
64                 Before CODE ref, $var has SvREFCNT=1, refcount=2
65                             $othervar has SvREFCNT=1, refcount=2
66                  After CODE ref, $var has SvREFCNT=2, refcount=2
67                             $othervar has SvREFCNT=1, refcount=2
68
69       Here, we see that SvREFCNT() counts the number of references to the SV
70       object passed in as the scalar value - the $var or $othervar
71       respectively, whereas refcount() counts the number of reference values
72       that point to the referent object - the anonymous ARRAY in this case.
73
74       Before the CODE reference is constructed, both $var and $othervar have
75       SvREFCNT() of 1, as they exist only in the current lexical pad. The
76       anonymous ARRAY has a refcount() of 2, because both $var and $othervar
77       store a reference to it.
78
79       After the CODE reference is constructed, the $var variable now has an
80       SvREFCNT() of 2, because it also appears in the lexical pad for the new
81       anonymous CODE block.
82

SEE ALSO

84       ยท   Test::Refcount - assert reference counts on objects
85

AUTHOR

87       Paul Evans <leonerd@leonerd.org.uk>
88
89
90
91perl v5.12.0                      2010-04-30                Devel::Refcount(3)
Impressum