Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Who's referencing my variables?

by LanX (Canon)
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)


Comment on Re: Who's referencing my variables?
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?
      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.

      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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-09-19 22:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (147 votes), past polls