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.


In reply to detecting changes in a localised variable by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.