Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re: Who's referencing my variables?

by LanX (Bishop)
on Apr 01, 2014 at 10:02 UTC ( #1080520=note: print w/replies, xml ) Need Help??

in reply to Who's referencing my variables?

My variables don't live within packages!

You can use Devel::Peek to check reference counts of lexicals and PadWalker to hunt them down in closures.

HTH! :)

Cheers Rolf

( addicted to the Perl Programming Language)

Replies are listed 'Best First'.
Re^2: Who's referencing my variables?
by Anonymous Monk on Apr 01, 2014 at 12:40 UTC
    I'm afraid my original post wasn't entirely clear. Consider the following code:
    package Foo; my @objects; sub new { my $self = bless {}, shift; push @objects, $self; return $self; } sub DESTROY { print "bye bye" } package main; { my $foo = Foo->new; } ETERNITY: while(1) { # DESTROY is NOT called yet. }
    When you run this, "bye bye" is not printed while we're waiting inside ETERNITY because @Foo::objects still holds a reference to $foo, even though its instantiation happened inside a scope we've since left.

    What I want to be able to do is to discover whether and where similar @Foo::objects-like data structures exist in a large code base. So I need to walk the symbol tables and probe what's there. How do I find such symbols? Can I?

      Hi RutgerVos: I assume you posted the code shown above.

      When you write and show us code that demonstrates some painful consequence and then ask "How can I avoid this pain?", the obvious, simple and complete answer is "Don't do that!"

      I suppose what you really mean is that other people, users of your code, are indulging nasty, unclean practices that are the source of the pain, such as exemplified by tobyink here. In this case, the only true solution is code review: go around and whack everyone's hand with a ruler until they stop doing the nasty. If a ruler doesn't work, use a hatchet. Others have pointed out tools to aid the review process, but you must ultimately persuade others not to do Bad Things with your code. Other than Good Practice, I'm not aware of any procedure in Perl that will 'armor' a reference, object or otherwise, against being circularized or otherwise given an infinite lifetime.

      Here is how you should have written that
      our @objecgts; ## @Foo::objects ... push @objects, weaken $object;

      If you're going to do things the fudgy way, do it the easy fudgy way

        I guess your implied advice is: go through all the modules (a few dozen) and make all variables 'our', then probe the symbol table. I'm not yet convinced that this is the best approach.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1080520]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2018-04-23 14:21 GMT
Find Nodes?
    Voting Booth?