Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format perl code?

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?

like image 415
David B Avatar asked Sep 16 '10 06:09

David B


People also ask

How write to file with format in Perl?

Using a Format Write keyword is used to call on the format declaration. Write FormatName; Format name is the name of an open file handle and the write statement sends the output to the same file handle. In order to send the data to STDOUT, format name needs to be associated with the STDOUT file handle.

Does indentation matter in Perl?

Perl is a free-form language: you can format and indent it however you like. Whitespace serves mostly to separate tokens, unlike languages like Python where it is an important part of the syntax, or Fortran where it is immaterial.


2 Answers

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

like image 163
RickF Avatar answered Nov 14 '22 14:11

RickF


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:

  • Padre, the Perl IDE: Building an open source team, getting the project to users against the odds (from FOSDEM 2010)
  • Context Sensitive Help with Padre, the Perl IDE
  • 2nd birthday of Padre, the Perl IDE - use IRC

/I3az/

like image 10
draegtun Avatar answered Nov 14 '22 16:11

draegtun