Perl coding guidelines


Do not use smartmatch - operator ~~ and given/when statements. They are marked experimental in recent Perl versions.

Do not use source filters. See a discussion on why it is bad.

Warnings and strict pragma

Starting every Perl program with these lines:

use strict;
       use warnings;

This will ensure a better, cleaner code.

  • 'use warnings' will cause Perl to spit out warnings on uninitialized variables - potential bugs.
  • 'use strict' turns on the 'strict' pragma which will make you declare your variables with the my keyword. 'use strict' also tosses up errors if your code contains any barewords that can't be interpreted in their current context. Those two lines are a must, even for very small scripts.


  • Lines should not be longer than 120 characters. (perltidy flag: -l=120)
  • Two or more consecutive empty lines should not be used.
  • One space on both sides of = or =>, except when contents are aligned for better readability.
  • One space on both sides of . (concatenation operator).
  • All blocks (including sub-blocks) have opening and closing curly braces on their own line, indented to the same level as the block. (perltidy flag: -bl)
if ($flag eq "h")
           $headers = 0;
       elsif ($flag eq "b")
           $body = 0;
           $something = 0;
  • For multiline blocks with parenthesis, parenthesis goes on the same line:
my %COMMANDS = (
           help => {function => \&cmd_help, usage => 'help <command> - print usage information'},
  • No space between function and its opening parenthesis.
myfunc($a, $b, $c);
  • No space in curly braces. (perltidy flag: '-bt=2')
  • No space in parenthesis. (perltidy flag: '-pt=2')
open(my $fh, '<:raw', $config_file)
  • In subroutines, return should be separated by an empty line:
sub nicer
           my $nice = shift;

           $nice += 10;

           return $nice;
  • For logical AND and OR always use '&&' and '||' instead of 'and' and 'or':
    if ($strict == 0 || ($str ne "" && $length <= 10))
               # validation successful

if ($strict == 0 or ($str ne "" and $length <= 10))

  • It's perfectly fine to use the block before expression in single-line conditionals and loops:
    $empty = 1 if ($str eq "");
    print("Hello $_!\n") for qw(world Dolly nurse);
  • Do not put constants on the left side of conditional operator:
    if ($value == MAX_FILE_SIZE)
               # do something

Variables, flow control etc

  • A semicolon must be placed after every statement
  • Always end subroutines with return if they are expected to return a value
  • Always unpack @_ into local variables in subroutines. Use either a direct assignment or shift. Always separate unpacking with an empty line. For example, instead of:
sub reply
           $irc→yield(privmsg => $_[0], $_[1]);


sub reply
           my $recipient = shift; # message recipient
           my $replymsg = shift; # raw IRC message (can contain colour codes)
           $irc→yield(privmsg => $recipient, $replymsg);


sub reply
           my ($recipient, $replymsg) = @_;
           $irc→yield(privmsg => $recipient, $replymsg);
  • If using next, last, and redo, a label must be used on the affected loop
  • Object naming
    • Identifiers (variables, subroutines) must be in lowercase, words separated with an underscore
    • Package names must be in CamelCase
    • Constants must be in uppercase, words separated with an underscore


  • Do not use the Switch module. It uses source filtering and is not recommended to be used. Either use if statements or the Switch::Plain module. if statements can be less readable, the module is an extra requirement that requires a recent Perl and is XS based, thus might need a C compiler to install.