Socrates has asked for the wisdom of the Perl Monks concerning the following question:
Then, I simply assign the function to a scalar, call it and undef it:sub subfactory { my $static = int(rand(100)); return sub {print "my number is $static\n";} }
My question is as follows: is the memory reserved for each instance of $static freed when I undef the function? I would imagine that the anonymous sub compiled in memory contains the sole ref to the memory that was set aside when $static was defined in subfactory() and that, when I undef the function, the reference would be freed too, freeing the memory up for garbage collection, but I'm not sure if that's an accurate assessment, and certainly more info on the internals would be appreciated.my $func=subfactory(); $func->(); undef $func;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: When are references within a closure freed?
by Fletch (Bishop) on Sep 05, 2008 at 17:15 UTC |