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