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,
\$_ = 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,
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: $_ scoping issues
by tilly (Archbishop) on Jul 14, 2003 at 19:39 UTC | |
by pemungkah (Priest) on Jul 15, 2003 at 16:18 UTC | |
by tilly (Archbishop) on Jul 16, 2003 at 01:42 UTC | |
by swkronenfeld (Hermit) on Jul 15, 2003 at 17:27 UTC | |
Re: $_ scoping issues
by DrHyde (Prior) on Jul 16, 2003 at 11:55 UTC | |
by swkronenfeld (Hermit) on Jul 18, 2003 at 22:19 UTC |
Back to
Seekers of Perl Wisdom