in reply to The Zeroeth Principle

Why do you say "our" and "we"? I'm clearly not part of the intended audience. Here are some coding tips that may help you no longer be part of the intended audience either.

First suggested change, start declaring all of your variables with my. That allows you to start using strict.pm.

Second suggested change, never call &do_something without parentheses unless you really want the @_ aliasing. If you don't know what I mean, then trust me that you didn't want to do that.

Third, don't write C-style for loops without specific reason. One of the most common bugs in C is off-by-one errors in a loop. (Yes, even for very experienced people. Track yourself.) Using C-style loops is going to cause you to have the same errors in Perl.

Fourth, avoid action at a distance. Trying to remember whether you've played with $[ will cause even more off-by-one mistakes. Learn how Perl likes to work, and stick with that. You'll have fewer errors. (And will be less likely to break any modules that you want to load.)

Fifth, learn the difference between the range and flip-flop operators. Yes, list vs scalar context can be confusing. But in Perl it is also very important.

Sixth, always say what you want directly. If you want to loop over 1..10 you can do that directly with either looping style. Just do it.

And last, but not least, if you want to loop over an array, at least 95% of the time it is best to just do it directly and forget about what the index is:

for my $thing (@stuff) { do_something($thing); }
(Note that I don't use the & on the function call. That is intentional, but it is a minor style decision of mine allowing me to be more consistent across languages. It isn't an important preference.)

Replies are listed 'Best First'.
Re^2: The Zeroeth Principle
by Velaki (Chaplain) on Aug 25, 2004 at 22:24 UTC
    1. This was just a rough example. I always use strict, use warnings, and declare everything.
    2. I called the function without parenthesis, because it's a trivial example. I know what it does. This was just for display purposes.
    3. In perl, I have rarely ever needed anything akin to a c-styled loop. This was for simply to promote discussion. I'm happy to see it worked! :-) Such passion!
    4. Action at a distance is not good. I never do that in my own code.
    5. I mistyped. I rarely refer to the operator by name, so I said flip-flop instead of range. Believe me, I'm very well aware of list vs. scalar vs. void contexts. :-)
    6. Yes, I know. This was more to promote discussion about a strange little variable.
    7. Most of the time, I do not need an index, and so I interate over the list directly. That is the perl way to do it, after all. When I do need an index, I'm exceedingly careful about its use, and exploit the range operator to it fullest, especially since it no longer slugs memory in the gut with large ranges. Mind you, in the before time, or when performance plays an issue, simple things like moving a complicated conditional to the initializer, and runnign the loop "backwards" was a key to efficiency.

    And the "our" and "we" at the top referred to a simple commonality of brethren programmers...the universal "we" in a spirit of rememberance.

    I'm quite happy this little meditation has provoked much thought, although I didn't expect a lesson on stylistics. Thanks! I was more curious as to the treatment of the $[ operator by those who have used it.

    Cheers!
    -v
    "Perl. There is no substitute."
      I don't know what your usual style is. However experience indicates that the way people write "toy code" is the way that they write real code as well. Therefore I reacted to aspects of your style which I thought likely to show up in real code.

      As for the treatment of $[ by those who have used it, the wise ones don't use it any more. ;-) My understanding is that $[ was added to Perl so that a2p could use it to easily emulate awk without having to track down every potential array access.

        I hear ya. In fact, I've seen a lot of "toy" code passed off as real "production" code, so I understand your dismay. But fret not; my code is much nicer and cleaner than that. Just look at any snippit. :o)

        I wonder how much a2p is still being used? I saw one person use it the other day.

        Thanks,
        -v
        "Perl. There is no substitute."