What really constitutes good programming practices in Perl? Invariably, when the subject comes up people start by pointing out that your programs should start with something like:
#!/usr/bin/perl -wT user strict;
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.
while (<>) { $array[$index++] = $_ if /$searchstring/; }
This is better written as:
while (<>) { push (@array, $_) 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.

Perl's syntax flexibility is a great feature that also causes difficulty here.

if (/$searchstring/) { &doSomething } &doSomething if /$searchstring/;
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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.