Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8183807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:16:52+00:00 2026-06-07T01:16:52+00:00

I was reading How can I access the ref count of a Perl hash?

  • 0

I was reading How can I access the ref count of a Perl hash?, and there both Devel::Refcount::refcount and Devel::Peek::SvREFCNT are suggested.

But they don’t return the same reference counts. Why is that?

Here a modified example from perldoc Devel::Refcount:

use Devel::Peek;
use Devel::Refcount;

my $anon = [];

printf "Anon ARRAY $anon has %d/%d reference\n",
    Devel::Refcount::refcount($anon),
    Devel::Peek::SvREFCNT($anon);

my $otherref = $anon;

printf "Anon ARRAY $anon now has %d/%d references\n",
    Devel::Refcount::refcount($anon),
    Devel::Peek::SvREFCNT($anon);

which prints out:

Anon ARRAY ARRAY(0x8b10818) has 1/1 reference
Anon ARRAY ARRAY(0x8b10818) now has 2/1 references

Notice the last 2/1 discrepancy…

(If it turns out I’m not doing something stupid, I’ll add a link from How can I access the ref count of a Perl hash? to here)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:16:55+00:00Added an answer on June 7, 2026 at 1:16 am

    I can’t say that I grok all of it yet, but your question is answered prominently in the Devel::Refcount perldoc

    COMPARISON WITH SvREFCNT

    This function differs from Devel::Peek::SvREFCNT in that SvREFCNT() gives the reference count of the SV object itself that it is passed, whereas refcount() 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.

    Consider the following example program:

     use Devel::Peek qw( SvREFCNT );
     use Devel::Refcount qw( refcount );
    
     sub printcount
     {
        my $name = shift;
    
        printf "%30s has SvREFCNT=%d, refcount=%d\n",
           $name, SvREFCNT($_[0]), refcount($_[0]);
     }
    
     my $var = [];
    
     printcount 'Initially, $var', $var;
    
     my $othervar = $var;
    
     printcount 'Before CODE ref, $var', $var;
     printcount '$othervar', $othervar;
    
     my $code = sub { undef $var };
    
     printcount 'After CODE ref, $var', $var;
     printcount '$othervar', $othervar;
    

    This produces the output

                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
    

    Here, we see that SvREFCNT() counts the number of references to the SV object passed in as the scalar value – the $var or $othervar respectively, whereas refcount() counts the number of reference values that point to the referent object – the anonymous ARRAY in this case.

    Before the CODE reference is constructed, both $var and $othervar have SvREFCNT() of 1, as they exist only in the current lexical pad. The anonymous ARRAY has a refcount() of 2, because both $var and $othervar store a reference to it.

    After the CODE reference is constructed, the $var variable now has an SvREFCNT() of 2, because it also appears in the lexical pad for the new anonymous CODE block.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE So after reading both of your answers I realize there is no reliable
Is there a way to access browsing context in JavaScript? Update: I'm reading HTML5
I've been reading previous posts but I can't figure out what will work or
I tried reading the MSDN article on markup extensions, but I can’t find out
I was reading How can I get query string values in JavaScript? on Stackoverflow
How I can start reading from a specific byte? I have following code: try
When reading an NSString from a file I can use initWithContentsOfFile:usedEncoding:error: and it will
How can i go about using bar code reading capabilities with my S60 application?
From reading the RFC it appears that CID can/must only contain characters from the
How can i Load Temptable on Server B by reading data from Server A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.