http://www.perlmonks.org?node_id=1014653


in reply to Re^2: foreach-loop-local var in sub
in thread foreach-loop-local var in sub

I think you are searching for anonymous subroutines and closures, but given that you indeed seem like a programming/Perl newbie, you shouldn't be venturing there and instead heed to the advice given in 1014512 above.