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:
(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.)for my $thing (@stuff) { do_something($thing); }
In reply to Re: The Zeroeth Principle
by tilly
in thread The Zeroeth Principle
by Velaki
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |