The solutions that have been provided all use "closure" which can be difficult to understand and is sometimes surprising. Reading perlref and Closure on Closures might help you to understand what closure is, how it works and other uses for it.
Comment on Re: How to get variable evaluation in anonymous subroutines declaration