How to format perl code?
Asked Answered
B

3

10

I really like my Perl code formatted - lines indented, etc. The problem is I hate doing it myself, and I really enjoy auto-formatters that do this automatically for you.

I work with Eclipse and the EPIC plugin does just that. The problem is that it does not handle comments very well. If comments are too long, it does not break them into multiple lines but simply shifts them to the beginning of the line, so the indention is not right at all.

Also, it keeps code lines about 80 characters long, which sometimes makes things ugly. From my school days I remember that too long line are bad, but now I wonder if perhaps it's better to just leave long lines as-is.

What do you say? any suggestions on how to format my Perl code under Eclipse (or in general)?

UPDATE

Eclipse's EPIC plugin also uses perl tidy. Any idea on how to make perl tidy break long code lines, but if it can't (e.g. long string) still indent them as normal and allow them to be longer (instead of indenting them all the way to the left as is the default?

Bly answered 16/9, 2010 at 6:39 Comment(3)
re: 80 chars line - I aim to keep my code below 80 chars but I'd go mad if my IDE enforced it! (TextMate & VIM have optional word wrap... but I have these switched off).Jd
Re: Perl::Tidy - Only lines that can be broken apart are broken if you set that option. E.g., long string constants and comments are not broken apart but may be outdented. Perl::Tidy reformats your code, it does not add (non-whitespace) code. Breaking apart such things would require extra code.Selfmoving
RE: 80 lines, the only reason there's anything special about 80 lines is because IBM paper punch cards had 80 lines. Why anyone would want to be held captive that that stupid and arbitrary limitation is beyond me. I also don't flog my sports car with a buggy whip to make it go faster!!!Capapie
J
10

For a non IDE solution then have a look at Perl::Tidy.

This module is good at beautifying your Perl code into whatever style you have a preference with. However not sure that it has a long line/comment fixer though? (Disclaimer: I don't use Perl::Tidy because I'm happy with the way TextMate & Vi(m) handle my Perl code).

For an alternative IDE solution then have a look at Padre, the Perl Application Development and Refactoring Environment.

Padre is self hosting (ie. written in Perl5), cross platform (uses wxWidgets for GUI) and works with Perl5 & Perl6 (rakudo).

Here are some videos:

/I3az/

Jd answered 16/9, 2010 at 8:2 Comment(2)
I found no Format Source function in Padre.Tamar
@JirongHu I'm afraid I can't help you on this because I don't have Padre installed on current machine.Jd
F
7

It looks like Eclipse/EPIC actually uses Perl::Tidy as its source formatter. Perl::Tidy is extremely flexible, so you probably just need to check the docs and set the options the way you want them.

The EPIC guide to setting those options: http://www.epic-ide.org/guide/ch02s04.php

Fitch answered 16/9, 2010 at 14:1 Comment(1)
Acvtually, it doesn't support my own formatting style: one feature is missing. But I use it anyway.Keramic
H
2

As stated in other answers, the Eclipse/EPIC source formatter uses Perl::Tidy. Reference both the EPIC guide & the Perl::Tidy guide.

http://www.epic-ide.org/guide/ch02s04.php http://perltidy.sourceforge.net/perltidy.html#formatting_options

Or the short answer, in the EPIC Source Formatting preferences, use -l 132 to change the line length to 132.

Homophony answered 16/7, 2014 at 22:18 Comment(2)
THIS is the answer!Bridie
Thanks for this tip of using -l 132. That is the real answer!Crosslink

© 2022 - 2024 — McMap. All rights reserved.