Unfortunately, everything after that seems to break down to anecdotes (definition by example) and suggestions to comment heavily. For instance, I changed some of my code after seeing someone point out a better way to add elements to an array.#!/usr/bin/perl -wT user strict;
This is better written as:while (<>) { $array[$index++] = $_ if /$searchstring/; }
The reason it's often (like everything, there are exceptions) considered better is because we avoid a needless variable, $index, and it's faster because we're not tracking that what happens to that extra variable. Unfortunately, this one example doesn't improve my overall programming. It just helps in this instance.while (<>) { push (@array, $_) if /$searchstring/; }
Perl's syntax flexibility is a great feature that also causes difficulty here.
The above code fragments do the same thing, but you get to place the "important part" of the code first. That's great. What's the "important part"? Well, that depends upon the programmer and how well he/she understands good coding practices.if (/$searchstring/) { &doSomething } &doSomething if /$searchstring/;
Other times, people will say (and rightly so) that we should consult CPAN modules for good examples. Unfortunately, entry level programmers are often at a complete loss to gain a knowledge of decent programming practices from these because they are so rigorous, that many cannot follow them (um... like me).
Personally, I'm a decent programmer. When programming in other languages, I have been routinely complimented on how idiot-proof and user-friendly they are. This is due, in part, to my use of Warnier-Orr diagrams to outline my program flow, but this is often problematic with event-driven or object-oriented programming. When it comes to Perl, it is such an incredibly rich language that no matter how much I code, I find better, more efficient, ways to do things. I'm at a loss here.
Can someone offer Perl-specific advice? I think a FAQ of good coding practices would be wonderful. Even if all the advice is "definition by example", at least having it in one place would be wonderful.
In reply to Good coding practices by Ovid
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |