in reply to Re: Should we Localize $_ ?
in thread Should we Localize $_ ?

I don't see any difference between "modifying" and "setting" $_. If the sub called a a built-in that returns a result in $_ (like magic while(<>)), it will clobber the value in the caller's foreach.

If the sub aliased it, using local or something like foreach which I assumes rebinds without changing the value, that would indeed be different.

I'm supposing that the reason I don't see this as a problem in real scripts is that I don't use the implicit loop with nontrivial bodies—I name the iteration variable. I suppose people who heavily use map might get bit with it more.

—John

Replies are listed 'Best First'.
Re: Reii: Should we Localize $_ ?
by bikeNomad (Priest) on Jun 13, 2001 at 23:47 UTC
    I don't get what you're saying. If you have a construct like while (<>) or foreach(@arr) you get a newly scoped $_:
    sub doit { my @array = qw(1 2 3 4); # this doesn't damage outer @array: $_ = 'xyz' foreach (@array); } my @array = qw(a b c d); print join("|", @array), "\n"; foreach (@array) { my $f = doit('abcd'); } print join("|", @array), "\n";
      Try the following with and without the 'local $_;' line and you'll see what he's talking about.

      use strict; sub getfile { local $_; my $filename = shift; local *F; open F, "< $filename" or die "Couldn't open `$filename': $!"; my $contents; while (<F>) { s/#.*//; # Remove comments next unless /\S/; # Skip blank lines $contents .= $_; # Save current (nonblank) line } return $contents; } my @array = ("base_tester", "Foo.pm"); foreach (@array) { my $filename = $_; my $f = getfile($filename); } print "@array\n";
      That's what I tried to see the effects, and I was rather surprised.
      No, while(<>) does not create a newly scoped $_.

      Behold:

      require 5.6.1; use strict; use warnings; my @array= (1,2,3,4,5); foo() foreach (@array); print "@array\n"; sub foo { while (<DATA>) { return } # silly, but makes a small example with little input. } __DATA__ clobbered clobbered clobbered clobbered clobbered