I am strongly opposed to adding stuff to code "for clarity's sake", even
if it provably doesn't hurt.
[...]
As an example, I prefer the following:
my $var = scalar(@array);
instead of:
my $var = @array;
because it explicates that I'm interested in the size of the array --
and makes it obvious that I didn't mistype the sigil in front of 'var'.
That's an error that would almost certainly be detected elsewhere under
strict. No need for special guards against that.
I also, on the few occasions when I use c-style loops, prefer:
for (my $i = 0; $i < scalar(@array); $i++) {
}
although the reason for this doesn't spring to mind -- probably because
it's how I taught myself (I only discovered this forum a few months
back, although I have been programming in Perl for about five years now).
It's just another case of accessing the array size. If you do one, you
practically have to do the other.
I don't really see that either of these two cases makes it harder to
understand the code -- in fact, when I'm reading over it really quickly,
it makes it easier...
A matter of habit, I suppose. I do have to look twice and condition my
perception to ignore the "scalar".
It also depends on the frame of reference. Since I know you as a
trustworthy Perl programmer, I know how to read it. Seen in isolation,
it would make me wonder if you had understood the concept of scalar
context. In beginner's code, posted to clpm, say, I would certainly
point out that "scalar" is redundant for this reason. A construct that
is bad in beginner's code should be avoided in advanced code unless
there is a strong reason to use it. Is the "context clarification"
it provides a strong-enough reason? Your call.
Context dependency is one of the very distinctive features of Perl.
No other language, except natural languages, makes use of it to quite
that extent, and it's the one feature that contributes most to making
Perl a bit like natural languages. So here's the killer argument
against scalar() in scalar context:
It is un-natural and unperlish!
So there.
Anno