Perl Coding Style

Perl should only be studied as a second language. A good first language would be English.

Larry Wall

General Guidelines

In general, follow the rules in the perlstyle man page, except we use cuddled elses (else on the same line after }). And, of course, follow rules below rather than perlstyle if they conflict.


I consider these optional rules that I may break when I have good reason, but which I try to adhere to whenever possible.

Formatting and Indentation

Don't use tabs in perl scripts, since they can expand differently in different environments. In particular, please try not to use the mix of tabs and spaces that is the default in Emacs.

Please follow these guidelines for spacing and formatting:

Editor Configuration

The recommended Emacs configuration, suitable for including in your .emacs file, is:

    (setq cperl-indent-level 4)
    (setq cperl-lineup-step 1)
    (setq cperl-continued-statement-offset 4)
    (setq indent-tabs-mode nil)

The section headings may be generated with the following Emacs function.

    ;; Insert a header for a Perl program (using # comment notation).
    (defun rra-insert-script-header (header)
      "Insert my Perl section header, prompting for the header content."
      (interactive "sHeader: ")
      (insert (make-string 78 ?#) "\n# " header "\n"
              (make-string 78 ?#) "\n"))

You may want to bind that to a key.

The recommend vim configuration, suitable for including in your .vimrc file, is:

    set tabstop=4
    set expandtab
Last spun 2013-07-01 from thread modified 2013-01-04