in reply to my within a loop

Yes, my $var will go out of scope after each iteration. If you want it to stay, you must declare it outside of your for loop.

You also might find this illuminating: Lexical scoping like a fox. Someone else shared it with regard to another thread today.