Home > Software design >  Consistent indentation of modules with Perl::Tidy
Consistent indentation of modules with Perl::Tidy

Time:07-23

I am using Perl::Tidy and Perl::Critic while testing my software on several platforms (different Linux distributions and macOS).

I now get different formatting from Perl::Tidy on Linux and macOS which makes Perl::Critic fail because the code is not tidy.

The difference is in the indentation of arguments for used modules.

For example, on Linux (Fedora with the default Perl::Tidy v20220217):

use Carp;
use English qw(-no_match_vars);
use POSIX qw(uname);
use Readonly;

and on macOS with Perl::Tidy v20220613

use Carp;
use English qw(-no_match_vars);
use POSIX   qw(uname);           ## qw is indented to be aligned
use Readonly;

I did not find an option to define the behaviour and cannot control the version of Perl::Tidy on all the systems where the tests are run.

Is there a way to make Perl::Tidy indent these lines consistently regardless of the version? Is there a command line option that I missed?

Currently, I am just ignoring the block:

#<<<  Perl::Tidy indents "qw" differently on older versions
use Carp;
use English qw(-no_match_vars);
use POSIX   qw(uname);
use Readonly;
#>>>

But maybe there is a better way.

CodePudding user response:

The 20220613 version added a new option to control this alignment. From the changelog:

Added vertical alignment for qw quotes and empty parens in 'use' statements (see issue #git 93). This new alignment is 'on' by default and will change formatting as shown below. If this is not wanted it can be turned off with the parameter -vxl='q' (--valign-exclusion-list='q').

# old default, or -vxl='q'
use Getopt::Long qw(GetOptions);
use Fcntl qw(O_RDONLY O_WRONLY O_EXCL O_CREAT);
use Symbol qw(gensym);
use Exporter ();
# new default
use Getopt::Long qw(GetOptions);
use Fcntl        qw(O_RDONLY O_WRONLY O_EXCL O_CREAT);
use Symbol       qw(gensym);
use Exporter     ();

So if you invoke perltidy with the -vxl='q' option on the Mac you're using, you'll get the older behavior. If this is done as part of an automated process, maybe adjust whatever is running perltidy to check the version first to see if the option should be included or not automatically.

  • Related