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

swkronenfeld has asked for the wisdom of the Perl Monks concerning the following question:

I'm currently writing a program which passes some user information around in a hash. In one of the subroutines of one of my files, the address of $_ is the same as the address of some of my data

\$_ = SCALAR(0x401ba594)
\$u->{loc} = SCALAR(0x401ba594)

I traced the problem down to this line of code in another file (which calls the above sub)

foreach ($u->{loc}) { ... }

So my question, how does the scoping apply to $_? I know that it's in package 'main', so it's global throughout my program (even across separate files). Can I make it local temporarily, or is there some other trick I can use? I'd like to avoid something like (foreach $a ($u->{loc})) because I actually have that foreach line in many spots, with lots of pattern matching, so it keeps my code cleaner to use $_. Is copying $u->{loc} into a temp variable prior to the foreach perhaps the best solution?

Thanks for any help,