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.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

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
    Awesome! I'll go through the "Making references" link carefully. Thank you so much for all the help. Eric