develooper Front page | perl.golf | Postings from September 2002

Re: [cam.pm] Programming guidlines

Thread Next
From:
Stephen Turner
Date:
September 15, 2002 02:37
Subject:
Re: [cam.pm] Programming guidlines
Message ID:
Pine.LNX.3.96.1020915100558.899A-100000@gentoo
Lucy McWilliam wrote:
> 
> I came across this on openchallenge.org 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.
  $a&&print
is more idiomatic than 
  print if$a
or
  if($b){print}

4) Never use semi-colons at the end of blocks or your program.
  for$i(1..10){$_*=$i;print}
not
  for$i(1..10){$_*=$i;print;}

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;
    print
  }

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



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

-- 
Stephen Turner, Cambridge, UK    http://homepage.ntlworld.com/adelie/stephen/
"This is Henman's 8th Wimbledon, and he's only lost 7 matches." BBC, 2/Jul/01


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About