I think that one of the things that's mildly confusing in this area is that perl does an implicit "local" for you whenever it sets $_ for you (in foreach, map and grep loops), but if you do the setting of $_ yourself, then you've got a new problem you don't see otherwise.

(I still don't understand why our "standard practice" doesn't include doing a "local $_" at the beginning of every sub... but then on the other hand, I guess it isn't all that common to get burned by $_ problems.)

Anyway, here's a script that demos what I'm talking about:

use warnings; use strict; use Test::More qw(no_plan); # we're going to try doing various things to $_, # and we want to see if it changes the initial value my $initial_value = 'Some Value'; my @initial_array = qw( wun tew thuree foah fahv sex sevhun ); {#1 map perlfunc: "locally setting $_ to each element"? my $testcase = "map"; my @array = @initial_array; $_ = $initial_value; my @whateva = map{ s/^f/F/ } @array; is( $_, $initial_value, $testcase); } {#2 foreach my $testcase = "foreach"; my @array = @initial_array; $_ = $initial_value; my @whateva = (); foreach (@array) { s/^f/F/; push @whateva, $_ }; is( $_, $initial_value, $testcase); } {#3 while - this one fails my $testcase = "while"; my @array = @initial_array; $_ = $initial_value; my @whateva = (); while (@array) { $_ = pop @array; s/^f/F/; push @whateva, $_}; is( $_, $initial_value, $testcase); } {#4 while with local my $testcase = "while with explicit local"; my @array = @initial_array; $_ = $initial_value; my @whateva = (); while (@array) { local $_ = pop @array; s/^f/F/; push @whateva, $_}; is( $_, $initial_value, $testcase); } {#5 grep my $testcase = "grep"; my @array = @initial_array; $_ = $initial_value; my @whateva = grep{ m/^f/ } @array; is( $_, $initial_value, $testcase); }

In reply to implicit local $_ happens sometimes by doom
in thread "$_" vs. $_ by argv

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.