in reply to Re^12: an algorithm to randomly pick items that are present at different frequencies
in thread an algorithm to randomly pick items that are present at different frequencies
Does $pick contain a reference to the anonymous subroutine, and it also remembers what was in my %kmer_prob hash?
Yes, & yes.
I suggest that you read section 4 of Perlref - Making references, carefully.
But, in a nutshell, when you create a subroutine (named or anonymous) it 'remembers' any variables external to that subroutine that it references.
So, this subroutine:
my $var = 2; sub x{ print $var; } x(); # prints 2 $var = 3; x(); # prints 3
For safety, you should isolate closed-over variables so they cannot be changed:
{ my $var = 2; sub x{ print $var }; } ## $var goes out of scope; but x() remembers it. x(); # prints 2; # $var = 3; ## Would be an error my $var = 3; x(); ## Still prints 2; the $var above is a different $var.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^14: an algorithm to randomly pick items that are present at different frequencies
by efoss (Acolyte) on Jun 05, 2015 at 02:46 UTC |