This may be old news for seasoned module authors, but I just got bitten by a new bug I wasn't really aware of before.

While debugging a script using Mail::Audit::Attach, I was wondering why the script would die at a certain point (it called a method on an undefined value). Finally, I found out the reason. The code was something like this:

foreach (@attachments) { $_->method1; $_->method2; }

Unfortunately, method1 contained a loop using $_ and finally setting it to undef, thus causing the script to die on the next method call.

The funny thing is, I have used constructs like

{ local $/ = undef; $data = <FILE> }

a lot of times, but somehow the concept never transferred itself to $_.

The solution, of course, is to either not use $_, or insert

local $_

in each method/function that uses it.

update (broquaint): title clarification (s/clutter/clobber/)


In reply to Is your module clobbering predefined variables? by crenz

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.