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:
- Mark $_ as read-only in the above code. (How? Is there an attribute for this, or is that perl6-only?) Ideally, perl would then complain about writing to a read-only global variable - and it wouldn't complain about writing to a re-localised version of it.
- Set a watch on $_ in perl -d, and see when it changes. Again, I would hope localising $_ again wouldn't count towards changing this localised $_. I'm not sure how to set this type of watch, being not very proficient in perl's debugger, but I kind of doubt that it will obey localisation the way I want. On such a high-traffic variable such as $_, noting every change for other localised versions would cause me to spend too much time on it.
- I'm hoping someone can help with this one...
Any help would be appreciated - I'm pulling out my hair on this. Thanks.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.