Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Can you determine the number of References to a value?

by wog (Curate)
on Sep 13, 2001 at 03:24 UTC ( [id://112058]=note: print w/replies, xml ) Need Help??


in reply to Can you determine the number of References to a value?

The Devel::Peek module offers the functions SvREFCNT, SvREFCNT_inc, and SvREFCNT_dec, if you import them explictly. These can give you direct access to the refcount of objects.

(Note that holding a reference to something, and decrementing its refcount manually is likely to cause perl to become confused -- you can probably decrement it's refcount by cleaning up all your refrences to it instead. It looked like you may have been thinking about doing something like this from your wording.)

Replies are listed 'Best First'.
Re: Re: Can you determine the number of References to a value?
by shotgunefx (Parson) on Sep 13, 2001 at 03:56 UTC
    Thanks.
    Basically at this point, I'm doing it just to see how. What I want to do (and have done) is create a few list operators.

    elements

    returns 1 or more elements. Like so


    findone

    Like grep only returns first matching element and index. Subsequent calls return next matching element and index or undef if array is processed.

    while ( my @els = elements @array,5){ somefunc(@els). } while ( findone { $_ > 5 } @array } print "Found $_\n"; } # or while (my ($val,$index) = findone { $_ > 5 } @array } print "Found $val in position $index\n"; }

    The problem is that I want to be able to nest calls to it which means I have to keep track of the current list. By taking a reference to them, I am stopping it from getting garbaged collected. The references are stored in a hash. What I'd like to do is loop through each and see if the ref count is 1. If it is, then it has to be me and I can "I think" safely delete my reference to it.

    UPDATE
    Does anyone else see value in these constructs? I think there handy. One of the things that I love about Perl is how much you can say in so few words. I was thinking of making the eleements operator possibly take a CODEREF so thay you could grab all the elements up to and including the element that returns true.

    -Lee

    "To be civilized is to deny one's nature."

      Yes, deleting your reference when the count goes to 1 makes sense in some situations. A more efficient way to accomplish this (and more general since it allows two or more different chunks of software to do the same trick to the same data) is called a "weak reference" which is available in Perl 5.6 and later.

      A weak reference is just like an ordinary Perl reference except that it isn't included in the reference count of the thing referred to. This means that once all references to a particular piece of data are weak, the piece of data is freed and all the weak references are set to undef.

      So you just have to check your weak references to make sure they haven't become undef before you use them. See WeakRef.

              - tye (but my friends call me "Tye")
        Thanks tye++
        I hadn't seen this before. Looks like it'll do exactly what I want.

        If you don't mind me asking, what do you think of the constructs? I think they represent some things that occur fairly often in a nice way. There were present in a propriatery LISPish language I worked with. I liked them and thought they where handy.

        -Lee

        "To be civilized is to deny one's nature."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://112058]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-24 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found