Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Reference Count of a Non-Scalar

by bbfu (Curate)
on Mar 10, 2002 at 22:03 UTC ( #150796=perlquestion: print w/replies, xml ) Need Help??

bbfu has asked for the wisdom of the Perl Monks concerning the following question:

Devel::Peek has a function, SvREFCNT, that lets you find out the reference count of a scalar value or variable. Unfortunately, it doesn't seem to have any way of determining the reference count of a non-scalar variable.

The only way I have been able to think of to find the ref count for, say, an array variable would be to parse the output of Devel::Peek's Dump function. This seems to me, however, to be a Really Bad Idea.

Can anyone point me to a way to get the ref count without resorting to this kind of chicanery?

Example Code:

#!/usr/bin/perl -l use Devel::Peek qw( Dump SvREFCNT ); my $a = 1; my $ra = \$a; print SvREFCNT($a); # prints 2 print SvREFCNT($ra); # prints 1 print SvREFCNT($$ra); # prints 2 my @b = (1, 2); my $rb = \@b; Dump($rb);

Output (the information I seek is in blue):

SV = RV(0x8103b88) at 0x80fd5c0
  REFCNT = 1
  RV = 0x80fd5a8
  SV = PVAV(0x80f77bc) at 0x80fd5a8
    REFCNT = 2
    IV = 0
    NV = 0
    ARRAY = 0x81226e8
    FILL = 0
    MAX = 3
    ARYLEN = 0x0
    FLAGS = (REAL)
    Elt No. 0
    SV = IV(0x80ff80c) at 0x80f62ac
      REFCNT = 1
      FLAGS = (IOK,pIOK)
      IV = 1

Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Replies are listed 'Best First'.
Re: Reference Count of a Non-Scalar
by derby (Abbot) on Mar 11, 2002 at 10:23 UTC

    A little Inline::C could do the trick. We could check if the passed in SV is a reference, if so, dereference it and get the SvREFCNT of that. The only issue is to remember that by passing it as \@array we up the reference count.

    #!/usr/local/bin/perl -w use Inline C; use strict; my( @x, $z, $r ); @x = qw( a b c d e f g ); $z = \@x; $r = ref_count( \@x ); print "Ref count is $r - expect 3 - initial, ref, and pass ref\n"; $r = ref_count( $z ); print "Ref count is $r - expect 2 - initial, and ref\n"; __END__ __C__ #include <stdio.h> U32 ref_count ( SV * val ) { SV *y; y = SvROK(val) ? SvRV( val ) : val; return SvREFCNT( y ); }


      After poking around a bit (and finding that Inline::C is broken on my nearest installation) I found this, which seems to do something similar to what I want. Is it right? I'm playing with toys I don't fully understand.
      use B 'svref_2object'; my $objref = {}; my $another = $objref; print svref_2object($objref)->REFCNT;
      This will print 2, as expected.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://150796]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2020-12-01 14:49 GMT
Find Nodes?
    Voting Booth?
    How often do you use taint mode?

    Results (10 votes). Check out past polls.