develooper Front page | | Postings from September 2002

Re: [] Programming guidlines

Thread Next
Stephen Turner
September 15, 2002 02:37
Re: [] Programming guidlines
Message ID:
Lucy McWilliam wrote:
> I came across this on and it set me wondering.
> "The challenge consists of writing coding guidelines for experienced
> programmers wishing to adopt Perl. These guidelines are not intended for
> "Perl hackers", but for the  ones who are most used to programming in C or
> other not-so-idiomatic languages. Those guidelines should be short
> enough, so that half an hour should be sufficient for a medium-level
> programmer to understand and be abble to use  them. They should give
> examples of clean way of implementing common operations and things to
> avoid."

Perl idioms for C programmers? OK.

1) Regular expressions are usually the most efficient way of doing things,
even if they can be a little impenetrable.

2) $_ is very useful. Normally you try and get the variable you're working
on into $_, so that you can edit it, print it, etc. without having to keep
stating the variable name explicitly.

3) Use of short-circuiting operators.
is more idiomatic than 
  print if$a

4) Never use semi-colons at the end of blocks or your program.

5) Leave out any superfluous space. Save whitespace for Python. So in the
previous example, you should never write anything like
  for $i (1..10) {
    $_ *= $i;

6) Never use tr///. y/// is always one stroke better.

(Cc:'ed to Just in case anyone doesn't know me well enough
to have got the joke yet. :-)

Stephen Turner, Cambridge, UK
"This is Henman's 8th Wimbledon, and he's only lost 7 matches." BBC, 2/Jul/01

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About