.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Devel::Refcount 3pm" .TH Devel::Refcount 3pm 2024-01-10 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME "Devel::Refcount" \- obtain the REFCNT value of a referent .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Devel::Refcount qw( refcount ); \& \& my $anon = []; \& \& print "Anon ARRAY $anon has " . refcount( $anon ) . " reference\en"; \& \& my $otherref = $anon; \& \& print "Anon ARRAY $anon now has " . refcount( $anon ) . " references\en"; \& \& assert_oneref $otherref; # This will throw an exception at runtime .Ve .SH DESCRIPTION .IX Header "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. .SH FUNCTIONS .IX Header "FUNCTIONS" .ie n .SS "$count = refcount( $ref )" .el .SS "\f(CW$count\fP = refcount( \f(CW$ref\fP )" .IX Subsection "$count = refcount( $ref )" Returns the reference count of the object being pointed to by \f(CW$ref\fR. .ie n .SS "assert_oneref( $ref )" .el .SS "assert_oneref( \f(CW$ref\fP )" .IX Subsection "assert_oneref( $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. .PP 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 \f(CW\*(C`delete\*(C'\fR operator can be asserted on .PP .Vb 1 \& assert_oneref delete $self\->{some_item}; .Ve .PP If at the time of deleting there are any other references to this object then the assertion will fail; and if \f(CW\*(C`Devel::FindRef\*(C'\fR is available the other locations will be printed. .SH "COMPARISON WITH SvREFCNT" .IX Header "COMPARISON WITH SvREFCNT" This function differs from \f(CW\*(C`Devel::Peek::SvREFCNT\*(C'\fR in that \fBSvREFCNT()\fR gives the reference count of the SV object itself that it is passed, whereas \&\fBrefcount()\fR 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. .PP Consider the following example program: .PP .Vb 2 \& use Devel::Peek qw( SvREFCNT ); \& use Devel::Refcount qw( refcount ); \& \& sub printcount \& { \& my $name = shift; \& \& printf "%30s has SvREFCNT=%d, refcount=%d\en", \& $name, SvREFCNT( $_[0] ), refcount( $_[0] ); \& } \& \& my $var = []; \& \& printcount \*(AqInitially, $var\*(Aq, $var; \& \& my $othervar = $var; \& \& printcount \*(AqBefore CODE ref, $var\*(Aq, $var; \& printcount \*(Aq$othervar\*(Aq, $othervar; \& \& my $code = sub { undef $var }; \& \& printcount \*(AqAfter CODE ref, $var\*(Aq, $var; \& printcount \*(Aq$othervar\*(Aq, $othervar; .Ve .PP This produces the output .PP .Vb 5 \& 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 .Ve .PP Here, we see that \fBSvREFCNT()\fR counts the number of references to the SV object passed in as the scalar value \- the \f(CW$var\fR or \f(CW$othervar\fR respectively, whereas \&\fBrefcount()\fR counts the number of reference values that point to the referent object \- the anonymous ARRAY in this case. .PP Before the CODE reference is constructed, both \f(CW$var\fR and \f(CW$othervar\fR have \&\fBSvREFCNT()\fR of 1, as they exist only in the current lexical pad. The anonymous ARRAY has a \fBrefcount()\fR of 2, because both \f(CW$var\fR and \f(CW$othervar\fR store a reference to it. .PP After the CODE reference is constructed, the \f(CW$var\fR variable now has an \&\fBSvREFCNT()\fR of 2, because it also appears in the lexical pad for the new anonymous CODE block. .SH "PURE-PERL FALLBACK" .IX Header "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 \f(CW\*(C`B\*(C'\fR module is used instead. This will behave identically, but is much slower. .PP .Vb 3 \& Rate pp xs \& pp 225985/s \-\- \-66% \& xs 669570/s 196% \-\- .Ve .SH "SEE ALSO" .IX Header "SEE ALSO" .IP \(bu 4 Test::Refcount \- assert reference counts on objects .SH AUTHOR .IX Header "AUTHOR" Paul Evans