Perl is very flexible in may ways including whitespaces. Perl does not force you to use tab or spaces. The script works fine no matter if it has DOS or UNIX line breaks.

But there are some good practices. I preffer these rules:

  • every line ends with "\n" (including the last line)
  • UNIX way for new lines (only "\n", not "\r\n")
  • no tabs, but 4 spaces instead
  • no trailing spaces
  • no empty lines in the end of the file

I want to make sure that all that rules are followed in my source code. So I've written Test::Whitespaces. There are several good things in this module.

You can create test file xt/whitespaces.t:

use Test::Whitespaces {
    dirs => [ 'lib', 'bin', 't' ],

It will check all the files in these directories for errors in whitespaces. And it will output the errors in a very friendly manner.

Instead of writing test file you can use script test_whitespaces that will show the problems. Here is the sample output. It is perfect, isn't it? =)

test_whitespaces output

In case you have found some errors there is a simple way to fix them. There is a script whiter that can fix all the errors in the project files.

I'm using this module to check and fix whitespaces not only in Perl source code, but also in test files and in templates and css/js.

I've written this module with the main idea to simplify the developers work. It has good documentation and it is as simple as possible.

I really hope that this module can be helpful for the Perl developers. And I would be happy in you express you opinion about this module. =)

PS And I want to thank play-perl.org for endorsing my work.


Isn't Perl::Tidy a better choice for this? You can also make Perl::Critic check the tidiness...

It doesn't check for code formatting, but Test::EOL and Dist::Zilla::Plugin::EOLTests checks for trailing whitespaces and for tabs.

I think this is nice and useful and I hope to use it soon.

But: Running the test script from the synopsis using prove doesn't output anything helpful:

$ prove t/Whitespace.t
t/Whitespace.t .. Failed 364/406 subtests 

Test Summary Report
t/Whitespace.t (Wstat: 0 Tests: 406 Failed: 364)
Failed tests: 1-9, 11-14, 16-26, 28, 31-33, 35-38, 40-44
47-59, 61-66, 68-69, 71-77, 79-103, 105
107-108, 110-113, 115-134, 136-231, 233-234
236-254, 256-261, 263-277, 279-281, 283-292
294-299, 301-318, 320-364, 366-371, 373-378
380, 382-386, 388, 391-393, 396-398, 400
Files=1, Tests=406, 30 wallclock secs ( 4.51 usr 0.05 sys + 29.15 cusr 0.38 csys = 34.09 CPU)
Result: FAIL

Which files need fixes where?

There is also Test::NoTabs and Test::Tabs (mine) on CPAN.

Leave a comment

About Ivan Bessarabov

user-pic I blog about Perl.