[Purdue-pm] Perl Style guide
Mark Senn
mark at purdue.edu
Mon Mar 19 07:16:41 PDT 2018
I recommend Tom Christiansen's Perl Style guide from 1997.
I agree with way over half of it and found it very thought provoking.
FIRST SOME GENERAL COMMENTS ABOUT PROGRAMMING IN GENERAL
Use the Emacs text editor, a Dvorak layout keyboard (or anything but
QWERTY), an ergonomic keyboard, and a trackball---not a mouse.
Don't try to squeeze your code into 80 character wide lines.
If the structure of it is better expressed in more characters
then use more characters. I'm working on a program now that
has lines up to 207 characters (initializing data in that part.)
SOME COMMENTS ABOUT TOM'S PERL STYLE GUIDE
These comments sometimes disagree with Tom's recommendations
The lines that begin with "Perl Style" are the names of Tom's slides.
Perl Style: Avoid Byte Processing
Use Perl 6 to avoid code like in the example.
Perl Style: Break Complex Tasks Up
I don't know what "PCB" stands for off-hand.
Perl Style: Learn to Switch with for
Perl Style: Switch by Using do{} Creatively
Perl Style: Switch with for via && and ||
Perl Style: Switch Using for and do{} Even More Creatively
Perl 5 has a switch feature:
#!/usr/local/bin/perl
use strict;
use warnings;
use feature qw(say switch);
my $day = 'Thursday';
given ($day) {
when ('Monday') { say 'feel refreshed and energetic'; }
when ('Wednesday') { say ' hump day'; }
when ('Friday') { say 'feel worn out'; }
default { say 'nothing special going on'; }
}
Perl 6 has a given/when construct which is more flexible.
Perl Style: From Perlstyle (part 1)
o Use 4-column indent. <4 is too small, >4 is too big.
(Joke: 8 column reminds me of people watching tennis matches---moving
eyes back and forth, back and forth)
o Don't do
if (condition) {
statement;
statement;
}
Get a display with lots of lines so the one extra line
isn't too much of a penalty and use code where open and
close braces are on lines by themselves.
if (condition)
{
statement;
statement;
}
o Don't do
statement ;
Do
statement;
o Use spaces and precedence to make code easier to read
Don't do
if (($name eq 'Mark') && ($cavities == 31))
Do
# note two spaces around "&&", one around "eq" and "==".
if ($name eq 'Mark' && $cavities == 31)
o Break any rule to make patterns easy to see. Like this
($condition) and do { statement; statement };
or use the more compact
($condition) and do statement, statement;
o Line up related things vertically
$name = 'Mark'; $cavities = 31;
$name = 'Toothless'; $cavities = 32;
o Start comments at same level of indentaton as code.
Don't do
# comment
code
Do
# comment
code
Perl Style: From Perlstyle (part 2)
o Don't use parens around function names unless there is
a good reason for it.
$width = length $column[1];
Use Perl 6! It is a much better, more expressive language, with object
oriented programming built-in instead of bolted on as an afterthought.
-mark
More information about the Purdue-pm
mailing list