Perl Coding Standards
- 1 Purpose
- 2 Rules for Perl
- 2.1 General
- 2.2 Design
- 2.3 Declarations: Scoping and Names
- 2.4 Implementation Details
- 2.5 Comments
See the C++ Coding Standards for the general purpose of Standards.
After doing Code Reviews with C++ for a while, there occurred the wish among Perl developers to do code reviews as well and to set up a rule set for that purpose.
The following is a first draft of those.
Rules for Perl
Strict Settings (Strict)
use strict and
use warnings to all scripts and modules.
Make Functions Short (Short)
Make functions short. A comment line or large control structures are hints to extract functions, making the containing function more concise and readable.
Avoid Duplicate Code (Dupli)
Avoid and remove duplicate code. Put common code into an extra function.
Duplicate code may be okay, if otherwise dependencies among so far unrelated modules would be created.
Use Framework Classes (Reuse)
Use the modules provided in a standard Perl 5.8 installation instead of reinventing the wheel.
However, don't create new dependencies to uncommon Perl modules, if there is no major need.
Use Perl Functions (PerlFs)
Prefer perl functionality over calls to
system() where possible.
Optimize Sensibly (Opti)
Optimize by algorithm complexity.
Don't do smaller optimizations, if they make the code less readable and there is no proven reason.
Declarations: Scoping and Names
my to scope all variables local to a block.
Avoid Global Data (NoGlobal)
Use as few global data as possible.
Assign Parameters at Top (Params)
Assign all parameters of a function to local, sensibly named variables on top of the function body.
Descriptive Names (Descr)
Use descriptive names.
Naming Conventions (NameConv)
Adhere to naming comventions:
- filehandles in capital letters:
- start class names with a capital:
- prefere underscores instead of mixed cases for variable names
ref postfix for references
Explicit Returns (Return)
Use an explicit
return statement for every return.
Use parentheses for all function calls.
Possible exceptions: standard perl idioms
Close Handles Soon (Close)
Don't forget to close all filehandles and connections. Close them as soon as possible.
Near Backinserters (BackIn)
Use and assign backinserters (like
(1), (2)) as close to the originating regular expression as possible, never more than five lines away.
Check System Calls (System)
Check the return values of all system calls.
Use the Correct Comparison Operators (Compare)
eq etc. are used for strings and
== etc. for numbers.
Pathseparator is Slash (Path)
Use the forward slash as path separator in all cases.
The singular exception is, when a system program needs a path in local convention as argument.
Use enough comments in the code to make it understandable.
Document Function Behaviour (ClearBehave)
As far as the behaviour of a function is not clear by its name and parameters, document that behaviour.
Document Return (DocReturn)
If a function has a non-obvious return semantic, document type and semantic of the possible return values.