R. Joseph Newton wrote: > > Rob Dixon wrote: > > > > foreach (@lines) { > > # too many lines here > > > > } > > > > > > While the purpose of the above may bve totally > > > incomprehesible, there is no question about what $_ is. <;:-o) > > > > One proviso here. I always feel very uncomfortable about > > explicitly assigning to $_. As I implied in my post to > > Wiggins, $_ is very much the equivalent to 'it', and I would > > no more use 'it' over more than a simple English sentence > > than I would use $_ across nested loops, procedure calls or > > whatever. In English your lines above say > > You're right. I actually got carried away. I was having so > much fun coming up with the most pointless code possible. that > I forgot the main point. The use of the idefault $_ did serve > its purpose, though, perverse as it was. I should probably > have done the print outside the loop to demonstrate both the > purpose and danger of using "it": > > my @lines = (<DATA>); > > foreach (@lines) { > chomp; > s/dog/cat/g; > my @words = split; > $words[0] = lc $words[0]; > $_ = join ' ', reverse @words; > } > > print"$_\n" for @lines; > #same data, results as previous sample > > ...has the same effect as printing from inside the loop. > Which means that my array contents > > have either been effectively modified, or totally hashed, > depending on whether that was the desired effect.
..except that, to be picky, you need print ucfirst "$_\n" for @lines outside the loop. And that this is a warning for 'foreach' in general, not specifically for $_. If your loop had been foreach my $line (@lines) { : } then the named scalar would similarly have been aliased with the array elements. Cheers, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>