in reply to How has your coding style changed over the years?
Here's the document (converted from pod):
Omit parentheses in builtin functions where possible.
print int rand $x;
Sometimes, I add parentheses to keep parallel structures similar.
return if substr($x, 0, 2) eq substr($y, 0, 2); # Second pair not ne +eded.
For subroutines, almost always use parentheses. You can omit them when it creates a nice DSL-like effect:
my $first = first { $x > 0 } @array; # List::Util
I also don't write them after test assertions.
is_deeply $response->{content}, bag(1 .. 10); # Test::Deep
I don't write empty parentheses after method calls.
$object->frobnicate;
For m, s, and qr, I use / or m{} (resp. s{}{}, qr{}). I use qx{} for code and usually q() and qq() for lists. I use different types of brackets for qw depending on the semantics of the enclosed expression:
use Exporter qw{ import }; # sub name my $from_roman = {qw{ I 1 II 2 III 3 IV 4 V 5 }}; my $primes = [qw[ 2 3 5 7 11 13 17 19 ]];
I use 4 spaces for indentation and spaces to align. No tabs at all.
I use a space after control flow statements and before the opening curly bracket:
while (my $item = $iter->next) {
I use spaces inside parentheses when the parenthesised expression does something on its own, e.g. changes a variable value. I only do it after functions, though, not control flow statements.
chomp( my @lines = <> );
I don't put spaces after [ or { when indexing an array or hash with a simple index. I usually use spaces for complex indices (both around the operators and around the whole expression). I write spaces after commas.
my $first = $array[0]; my $middle = $array[ $#array / 2 ];
I don't add space after a bracket starting a list, unless it's a qw() list or dereference.
my $arr = [1, 2, 3]; my @directions = qw( left right ); my @items = @{ $ref{items} };
I cuddle elses and use the K&R style:
if ($x > 0) { say 'Positive'; } elsif ($x < 0) { say 'Negative'; } else { say 'Zero'; }
For lists, though, I use the List style for the closing bracket:
my @planets = qw( Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune );
After a control flow command, I usually insert an empty line (unless the following line is a closing bracket).
while ($x = $iter->next) { next if $x > $threshold; $x -= $correction; }
If there is a longer sequence of the same commands, the inner empty lines might be omitted.
return if $x > $threshold; return if $err || ! $success; return [] if $not_found; my $result = count($x);
I use empty lines to separate code chunks semantically. In larger projects, I use two empty lines to separate subroutine definitons.
I use ||, &&, and ! in expressions and and and or for control flow (which means I don't use not much).
I usually don't type the $_ if not needed (i.e. map and grep blocks).
my @normalised = map lc, @words;
Expression forms of map and grep are OK. I switch to the block form when the expression can't be written without +().
my %pairs = map { $_ => value($_) } @labels;
I don't use map and grep in void context. That's why we have for (and I never spell it foreach).
I don't put a semicolon after the command that leaves the block.
sub new { my ($class, $name) = @_; return bless { name => $name }, $class }
It protects me from adding code below and wondering why it's never executed.
Similarly, I don't put a semicolon after the 1 at the end of a module. Moreover, I usually spell it
__PACKAGE__
I also omit the semicolons in short one-line subs:
sub second { $_[0][1] }
I never put whitespace (including newline) before a semicolon.
|
|---|