What are coding standards in Perl?

Every programmer has a unique perspective and a little virtual world that they create which follows their own rules and guidelines.

It is, however, customary to adhere to some guidelines that act as Coding Standards. Hereon, we will take a look at the recommended usage of Perl published by the creator Larry Wall himself.

Aspects of the Perl Coding Standard

General usage guide

  • Stakeholder analysis

Before starting to design and write a code, Larry suggests we take a closer look at all the people this will be affecting and take all stakeholders into consideration. Not just the administrator and the users but rather the people who will someday need to maintain this as well.

  • Design → Code

Instead of coding a design, design a code and then write it. It always helps.

  • Inventing something new

Before trying to invent something new from scratch, Larry suggests we take a closer look at something that has been created already that comes close to what we are trying to achieve, and then improve on that.

User specific guide

  • Reusability

Write code that can be reused elsewhere as well. One-shot codes are never nice.

  • Clean run

Utilize the strict and warning flags effectively to ensure a smooth run.

  • Sub-routines

If your sub-routine is longer than 100 lines, you should modularize it further.

  • Returning vs. printing

Try returning processed data rather than printing it out on the standard output.

  • No global variables

Try not to use any global variables

  • Commenting and documentation

Make use of good comments and ensure effective user documentation is in place.

  • Open source

Consider giving away your code and see what the world does with it.

Aspects of User Specific Guide

Coding guide

To ensure that the readability of the code is increased and it is easy to understand, Larry recommends the following:

  • Use 4-column indentation.

  • If possible, place the opening curly brace on the same line as a keyword. Otherwise, it can be placed on the next line.

  • Add a space before a multi-line block’s opening curly brace.

  • A one-line block may have its curly braces on the same line too.

  • Omit space before a semicolon.

  • Avoid adding a semicolon in a short one-line block.

  • Include a space around an operator.

  • Include a space around a complex subscript (inside brackets).

  • Include blank lines between code sections that perform different functions.

  • Do not add a space between a function name and its opening parenthesis.

  • Place a space after each comma in the code.

  • For long lines of code that scroll right, include a break in the line after an operator, except with and and or.

  • The space after the last parenthesis should match the current line.

  • Line up corresponding items vertically.

The above-mentioned guide is where Larry explains the aesthetics of the code.

The below-mentioned guides make use of a logical structure that helps improve the coding standards

Purposeful beginning to scripts

Try mentioning the more meaningful parts first followed by the fallback rather than the other way around. For example:

($myvar1 / $myvar2) || die "Error: Variable 2 is 0";

Is better than:

die "Error: Variable 2 is 0" || ($myvar1 / $myvar2);

Omitting parenthesis

While some programmers are comfortable with the use of mathematical expressions atomically, it is not advisable to omit parenthesis unnecessarily as it makes it difficult for someone else to understand what is written. For example:

return ($a - ($b**$c) + $d);

Is better than:

return ($a-$b**$c+$d);

Labeling loops and last keyword

Larry advises labeling the loops that we may use. Moreover, the last keyword is similar to the break statement of C++ or Java and should be used similarly.

However, when using the last statement, ensure that it is indented a space less than everything else to increase readability.

LOOPLABEL:
    for (my $i = 0; $i < 10; $i++){
         #code
       last LOOPLABEL if ($i == 5)
         #code
}

Variable scoping Perl conventions

The following conventions are used when naming variables to associate scoping.

Convention

Scope

$ALL_CAPITAL_LETTERS

Generally Associated to Constants.

(This might even clash with Perl’s own variables hence use with caution)


$Camel_Case

Package-wide scope

$all_small_case

Subroutine’s scope/ my() or local() scope

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved