in reply to Passing value outside of loop

I believe that your variable $file is only available inside the while loop because of scope (someone feel free to correct me if I'm mistaken). You should put the my $file before the start of the while loop.