Search code examples
perlperl-tidy

Can perltidy remove all leading whitespaces before performing indent rules?


Suppose I have some Perl code that looks like this

      use Data::Dump q[pp];
  sub something{
      return 2 + 2;
  }

I want it to be formatted by perltidy as

use Data::Dump q[pp];
sub something{
    return 2 + 2;
}   

That is, all leading whitespace eliminated and all lines aligned left, but then with indent rules applied. I've tried -dws but that didn't work and no-one of the other options seems to do anything. Is this possible?


Solution

  • As shown it is not possible.

    Some additional experimentation shows that as long as there is at least one line (which can contain pretty much anything) that is indented fewer than four spaces at the beginning of the file perltidy will indent as hoped for in the original question.

    The solution for this specific sort of example is to include pre-processing steps in a Makefile which simply eliminates all blank lines and left space before running perltidy. This solves the problem.

    sed -i -e 's/^[ \t]*//' file.pl
    sed -i -e '/./!d' file.pl
    perltidy file.pl