jettero has asked for the wisdom of the Perl Monks concerning the following question:
I'm looking for ways to avoid the fact that an anonymous code ref usually references itself (and therefore never goes out of scope). Leonard of #perl-freenode complained that a sub like this will never say goodbye:
my $x = sub { "test" }; bless $x, "HRM"; sub HRM::DESTROY { print "bye\n" }
I used Devel::FindRef to discover the obvious: the sub keeps a reference to $x for closure reasons, which makes sense. If you construct the ref like this:
my $y = eval 'sub {"test"}'; bless $y, "HRM";
... It will say goodbye just fine. Are there other, hopefully non-eval, ways to keep the sub from referencing itself? (It's not a bug is it? I mean, you clearly want the subs to keep references to all the lexicals they see... just not ... that one, sometimes.)
-Paul
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: coderefs keep references to themselves
by moritz (Cardinal) on Jul 15, 2008 at 11:43 UTC | |
by jettero (Monsignor) on Jul 15, 2008 at 12:04 UTC | |
|
Re: coderefs keep references to themselves
by dave_the_m (Monsignor) on Jul 15, 2008 at 11:44 UTC | |
by jettero (Monsignor) on Jul 15, 2008 at 12:02 UTC | |
by Fletch (Bishop) on Jul 15, 2008 at 13:00 UTC |