Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I track references to Perl objects?

I'm trying to fix my code to enable Perl to recover unneeded data by weakening references and breaking cycles.

I recently asked a question on How to access Perl ref counts and the answer has been working well for me.

For some of my objects, the reference count is > 1 and I don't know why.

Is there a way for me to add a callback or something to help me know when a reference count is incremented? I want to know who is referencing an object.

like image 377
mmccoo Avatar asked Mar 02 '10 17:03

mmccoo


People also ask

Which operator takes a reference and turns into an object in Perl?

bless function is used to return a reference which ultimately becomes an object.

What is anonymous array in Perl?

Array references—which anonymous arrays are one type of—allows Perl to treat the array as a single item. This allows you to build complex, nested data structures as well. This applies to hash, code, and all the other reference types too, but here I'll show only the hash reference.

What does @_ mean in Perl?

Using the Parameter Array (@_) Perl lets you pass any number of parameters to a function. The function decides which parameters to use and in what order.


1 Answers

Implement a Devel::XXX package that inspects the refcounts of your objects?

package Devel::Something;
# just emulating Devel::Trace here
# see http://cpansearch.perl.org/src/MJD/Devel-Trace-0.10/Trace.pm
sub DB::DB {
    if ($Devel::Something::CHECK) {
        my ($package, $file, $linenumber) = caller;
        ... inspect current refcounts
        ... if any have changed, print out the details
        ...    including current package/file/linenumber
        $Devel::Something::CHECK = 0;  # disable until it's enabled again
    }
}
1;

# my program
... do some stuff ...
$Devel::Something::CHECK = 1;
... do some more stuff ...
$Devel::Something::CHECK = 1;

$ perl -d:Something my_program.pl ...

You could sprinkle $Devel::Something::CHECK = 1 statements at appropriate places throughout your code, or change the condition in DB::DB to run at regular intervals (e.g., if (++$Devel::Something::CHECK % 100 == 0) { to inspect after every 100 statement evaluations).

like image 66
mob Avatar answered Sep 21 '22 15:09

mob