Skip to content
Dan Book edited this page Jun 21, 2019 · 3 revisions

Perl::Tidy to the rescue

Keeping code well formatted is not vital, however, it reduces the cost of maintenance and it raises acceptance among co-workers. Whenever someone looks into code snippets which vary all over the place the results are unpredictable. There are reasonable tools on the market which test your code and format it.

A common one is the Perl module Perl::Tidy. It lets you specify the way your code is being formatted by using a configuration file named .perltidyrc. The Mojolicious GitHub repository has a nice .perltidyrc included, which should be used to format any contributions to Mojolicious projects, and you can also use it for your own code.

  • Install Perl::Tidy (easiest way: cpanm Perl::Tidy)

  • Download the .perltidyrc file from the Mojolicious repo and install it inside your home directory.

    wget -O ~/.perltidyrc https://raw.githubusercontent.com/mojolicious/mojo/master/.perltidyrc
  • perltidy will start with .perltidyrc in the current directory if it exists, and otherwise ~/.perltidyrc, and append any further options given on the command line. You can use a different .perltidyrc file using the option -pro=<filepath>.

  • Tidy your sources to new files with the extension .tdy:

    perltidy <sourcefile(s)>
  • Tidy your sources in place and back up changed files with the extension .bak:

    perltidy -b <sourcefile(s)>
  • Tidy your sources in place and remove the backup files if successful:

    perltidy -b -bext=/ <sourcefile(s)>