Tanktalus has asked for the wisdom of the Perl Monks concerning the following question:
Let's start with a piece of code:
my @list = map { do_something_complex($_); } @other_list;
In some of my code, I must be doing something to $_ accidentally somewhere. If I use the code above as-is, @other_list gets corrupted, and @list doesn't have what I want. If I localise $_, though, then everything works great. The problem is - I want to figure out where $_ is getting clobbered. A better solution, in my mind, is to localise $_ to the clobbering routine, to make it safer. But, of course, I'm using $_ all over the place - map's, foreach's, grep's, etc. And just using fgrep to find $_ is unwieldy - it's impossible to tell when I'm using $_ in an automatically localised area (such as map, grep, foreach) from a simple fgrep because I often have multi-line map's, grep's, etc. And then there is the use of @_ - fgrep picks up on $_[0], too, of course.
There has to be a better way. I just can't seem to find it. Ideas I have (which I don't know how to try) are currently limited to:
Any help would be appreciated - I'm pulling out my hair on this. Thanks.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: detecting changes in a localised variable
by TedYoung (Deacon) on Feb 18, 2005 at 22:25 UTC | |
by Tanktalus (Canon) on Feb 18, 2005 at 22:46 UTC | |
by TedYoung (Deacon) on Feb 18, 2005 at 23:15 UTC | |
|
Re: detecting changes in a localised variable
by borisz (Canon) on Feb 18, 2005 at 23:25 UTC | |
by thor (Priest) on Feb 19, 2005 at 00:15 UTC | |
by Tanktalus (Canon) on Feb 19, 2005 at 00:24 UTC | |
|
Re: detecting changes in a localised variable
by gaal (Parson) on Feb 19, 2005 at 08:10 UTC | |
by BrowserUk (Patriarch) on Feb 19, 2005 at 16:08 UTC | |
by Tanktalus (Canon) on Feb 19, 2005 at 16:10 UTC | |
by gaal (Parson) on Feb 19, 2005 at 16:44 UTC |