Your subroutine is a closure. It continues to point to the $value that existed when the sub was compiled. Instead, you should pass $value to your sub. Most of the time you should pass all needed variables to subs, because you want them to operate independently of their context.