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):

2
1
2
SV = RV(0x8103b88) at 0x80fd5c0
  REFCNT = 1
  FLAGS = (PADBUSY,PADMY,ROK)
  RV = 0x80fd5a8
  SV = PVAV(0x80f77bc) at 0x80fd5a8
    REFCNT = 2
    FLAGS = (PADBUSY,PADMY)
    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

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


In reply to Reference Count of a Non-Scalar by bbfu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.