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.

FWIW, on the fairly rare occasions that I do a long loop without a declared element
identifier, I will usually assigned a named variable to a function of $_ as my first 
line.

foreach (@words) {
   my $dictiionary_entry = lc $_;
   ...
}

Joseph



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to