R. Joseph Newton wrote:
>
> Rob Dixon wrote:
> >
> > Not sure which way you're leaning here. I never saw you as anti the 'it'
> > variable $_?
> >
> > Rob
>
> I think its a matter of context, Rob.

I agree 100%. But see later.

> What I cringe at is seeing a default
> variable being used in dense code.  The more other code happening, the less
> clear will be the meaning of $_.  When I first started using Perl, I just
> didn't like it a bit--it seemed just too damned tech-ish.  I've come to
> appreciate, though, that it can be very effective and actually aid clarity in
> a more staccato context.  I would say that it should be used only in contexts
> where it is very clear what it means:
>
> foreach (@lines) {
>    chomp;
>    s/dog/cat/g;
>    my @words = split;
>    $words[0] = lc $words[0];
>    $_ = join ' ', reverse @words;
>    print ucfirst $_, "\n";
> }
>
> 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

  For each element of @lines, chomp it, substitute 'cat' for 'dog' in it, split it
  into @words...

and there the simile starts to fall down because of the explicit assignment. That's
why I prefer the idiom:

  for (join ' ', reverse @words) {
    print ucfirst $_, "\n";
  }

(No I'd never write this code, but the circumstance is artificial.)

This starts a new 'sentence' saying

  With the value obtained by joining the elements of the reversed @words array with
  a space character, print it followed by a newline and with its first letter 
uppercased.

I believe that the reason all this makes sense to me in this way is because of
Larry's linguistic background. Some will love it and some hate it: something like the
difference between a bricklayer and a sculptor.

As a final thought, I would point out that $_ is a package ('our') variable, but is
localised by 'map', 'grep', 'foreach (LIST)' and 'while (<>)'. The only worry, then,
is subroutine calls which can easily do

  local $_;

if that's what you want.


Rob



-- 
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