How to apply/use Perl::Tidy with an existing Perl project?

611 views Asked by At

This question is not so much technical than looking for advise to find the proper approach that will not hurt.

Assuming the following:

  1. We have a big application developed in Perl.
  2. We want to start using perltidy on the commandline to impose uniform style/rules for formatting
  3. We have many branches that we do not know exactly when will be merged to the master branch. They should be tidied too somehow before or after the merge
  4. We want to avoid any conflicts in commits caused by formating.

I thought about starting to format on subroutine level, but I do not find such feature in perltidy. There is another feature "Skipping Selected Sections of Code", but now I may want the opposite -- format only selected sections of code. The idea is that when a developer touches part of the code he/she will tidy only the modified part.

The best would be to find a way to format the whole project once without disrupting the development cycle and making sure we do not break any part of the code (during merging). We have unit tests, but some parts of the code may still be uncovered.

Let me also clarify that everybody in the team uses potentially different editor. For example I use Sublime Text 3 with SublimePerlTidy. Other people use Kate or Atom or VIM. A proper approach seem to be to format only pieces of code that we touch as pointed by @xxfelixxx (Thank you!)

1

There are 1 answers

1
xxfelixxx On BEST ANSWER
  1. Define a coding standard and create a .perltidyrc to share with all of the developers.
  2. Have tidying be one of the tasks that teams perform for their projects (along with testing and code review), so they tidy/test/review code they touch.
  3. Test the code very well. Tidying can introduce subtle bugs, so it is better in small manageable amounts (as opposed to just tidying the whole codebase, and wondering why things stopped working...)
  4. Tidy commits should be on their own, with no other changes, so that regressions can be tied to code changes or tidy changes respectively. git bisect is great for finding the offending commits.

As for my own perltidy usage, using emacs, I tend to tidy up small chunks of code at a time by highlighting a region ( creating a mark with C-space, navigating to highlight a region, and then running M-p which I have mapped to perltidy-region. To get this to work, install perltidy and add the following to your .emacs file:

(defun perltidy-region ()
    "Run perltidy on the current region."
    (interactive)
    (save-excursion
      (shell-command-on-region (point) (mark) "perltidy -q" nil t)))
(defun perltidy-defun ()
    "Run perltidy on the current defun."
    (interactive)
    (save-excursion (mark-defun)
    (perltidy-region)))

(global-set-key "\M-p" 'perltidy-region)