Perltidy and MojoMojo
Overview
The MojoMojo code base currently has some basic syntax formatting guidelines1. For example, the indent level is four spaces, and hardcoded tabs are not used (instead they should be converted to 4 spaces). We are considering a more general tidiness to the code. Specifically, the use of perltidy in its default mode has been encouraged2. perltidy's default behavior brings up some interesting points for discussion. A few of these points are introduced in this article.
Line Length
One of the things perltidy enforces is a limited line length. The default line length is 80 characters. MojoMojo developers are considering other choices such as 115 characters3
When lines exceed the maximum length, perltidy attempts to choose good break points4. The break point decision can have interesting effects.
Line Break Effects
The process of breaking long lines into smaller ones can exhibit interesting behavior. For example a statement with inline comments that exceeds 80 characters may put the statement's terminating semi-colon on the line after the statement:
Statement Ending Semi-Colon Example
my $original_formatter ; # used to save/restore whatever formatter is set up in mojomojo.db my $c; # the Catalyst object of this live server
While this is perfectly valid syntax, it may tweak some people's flavor buds:
No improvements
Code before perltidy
$test = "this string will still have more than 80 characters if it's moved to the next line"; if (...) { $test = '[div] with non-standard HTML attribute> in a code span - the HTML scrubber should leave this alone'; }
After perltidy with the default line length of 80 chars:
$test = "this string will still have more than 80 characters if it's moved to the next line"; if (...) { $test = '[div] with non-standard HTML attribute> in a code span - the HTML scrubber should leave this alone'; }
Note that in both cases, the strings are still longer than 80 characters, but perlstyle indenting has been broken. In the second case, it's particularly odd, as the string starts at column 1, while the block itself is indented one level.
Possible Solutions
- Don't write inline comments that exceed the maximum line length.
- Use a pure comment line above the statement.
- Don't use perltidy.
- Live with it.
Blank Line Before Comments
perltidy puts a blank line before comments.
Effects on Sandwiching Code with Comments
Given the newline before comments, perltidy will alter the following:
#------------------------------------------------------------------------------- $test = 'unpassable test'; #-------------------------------------------------------------------------------
to be:
#------------------------------------------------------------------------------- $test = 'unpassable test'; #-------------------------------------------------------------------------------
Possible Solutions
- Turn off default behavior of newlines before comments. How?
- Modify or give up the sandwich technique.
Comment Line Indentation Level
Comment lines will indent to their block level as much as they can based on their length. For example a comment line of 80 characters will not indent at all, but a comment line of length 77 will be indented 3 spaces by perltidy if found in { }.
An initial opinion of syntax guidelines is available for discussion. ↩
irc log - "15:45 <marcus> yeah, feel free to run perltidy on anything mojomojo" ↩
One can set the line lenght with: perltidy --perl-best-practices -l=115 ↩
Certain long lines will not break. For example, strings or heredoc lines. ↩
Showing changes from previous revision. Removed | Added
