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:
Yeh I definitely agree, which was my comment about one liners. If your program is 10 lines long and is only likely ever to grow to 15 then by all means, or if it will never even end up in a file, then sure. But you breach a certain point and all of a sudden "it" is on shaky ground...
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...
I didn't pick up on what you meant by your "explicit" 'it'... This is an excellent explanation and demonstration of why I don't like it, even though I couldn't have provided *it*!
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.
Sounds like good reasoning to me. Though Americans' grasp of the English language is going downhill so fast that who knows what we will end up with next. It goed to the it and it did some iting to the thingamait, and it was smurfy! I won't get into a rant about why the media should be outlawed from putting ex-professional athletes on the air, especially live!
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.
Let's not and say we did....
http://danconia.org
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>