6

How can I configure perltidy to format long if statements like this:

if (
    ('this is an example' =~ /an.*example/ and 1 * 2 * 3 == 6) or
    ('hello world' =~ /world/ and 6 = 3 * 2 * 1)
) {
    print "hello\n";
}

or like this

if (
    ('this is an example' =~ /an.*example/ and 1 * 2 * 3 == 6)
    or ('hello world' =~ /world/ and 6 == 3 * 2 * 1)
) {
    print "hello\n";
}

Edit1: perltidyrc

--maximum-line-length=100
--indent-columns=4
--default-tabsize=4
--continuation-indentation=4
--closing-token-indentation=0

--no-indent-closing-brace

--paren-tightness=2
--square-bracket-tightness=2
--block-brace-tightness=0

--trim-qw

--nospace-terminal-semicolon
--nospace-for-semicolon

--indent-spaced-block-comments
--ignore-side-comment-lengths

--cuddled-else

--no-opening-brace-on-new-line
--no-opening-sub-brace-on-new-line
--no-opening-anonymous-sub-brace-on-new-line
--no-brace-left-and-indent

--blanks-before-comments
--blank-lines-before-subs=1
--blanks-before-blocks
--maximum-consecutive-blank-lines=1

Edit2: The idea is to have a new line after the first ( and also the last ) to be on a new line with {. If this is not possible, any other suggestions for better formatting will be appreciated.

bliof
  • 2,957
  • 2
  • 23
  • 39

3 Answers3

4

The default style of perltidy is to follow the Perl Style Guide where possible. Which results in this output:

if (   ( 'this is an example' =~ /an.*example/ and 1 * 2 * 3 == 6 )
    or ( 'hello world' =~ /world/ and 6 == 3 * 2 * 1 ) )
{
    print "hello\n";
}

You can control the curly braces whether they're on a new line or not. You can control the tightness of the parentheses. You cannot, however, control the new lines for parentheses in code blocks.

The default style is as close as you're going to get to your desired output.

titanofold
  • 2,852
  • 1
  • 15
  • 21
  • Is there a way to add spaces before/after the outer brackets if the statement is multiline? (p.s. I've added my perltidyrc in which I remove them) – bliof Nov 15 '12 at 11:54
  • @bliof, no, the options for styling blocks are quite limited. – titanofold Nov 15 '12 at 17:51
2

If it's obnoxiously long, one idea might be to limit the maximum length of each line with -l=n, where n is the maximum length of each line:

$ cat perltidy_test.pl
if (
    ('this is an example' =~ /an.*example/ and 1 * 2 * 3 == 6) or('hello world' =~ /world/ and 6 = 3 * 2 * 1)
) {
    print "hello\n";
}

$ perltidy -l=60 perltidy_test.pl
$ cat pertidy_test.pl.tdy
if (
    (
        'this is an example' =~ /an.*example/
        and 1 * 2 * 3 == 6
    )
    or ( 'hello world' =~ /world/ and 6 == 3 * 2 * 1 )
  )
{
    print "hello\n";
}
Zaid
  • 36,680
  • 16
  • 86
  • 155
0

try

$ perltidy -bli -wba='or' perltidy_test.pl

that works for this example...

  • I not so sure. The `bli` indents the curly brackets and `wba` adds new line after `or`. I will edit the question to specify the problem.. – bliof Nov 15 '12 at 15:26
  • **$ perltidy -anl -bbb tidy_test.pl** :) `-anl` add new lines `-bbb` add blank line between major blocks –  Nov 15 '12 at 17:11