eyepopslikeamosquito has asked for the wisdom of the Perl Monks concerning the following question:
At work we have a simple rule for all our Perl code: it must pass our automated in-house static analysis check -- which includes a Perl::Critic check. No ifs, no buts, it must pass before you are allowed to check it in.
To allow for special cases, and Perl::Critic bugs, we do allow you to switch off specific Perl::Critic warnings, but only in the smallest scope possible.
Though this simple blanket rule has worked very well IMHO over the past few years, today a workmate and good friend of mine, Misha, was agitated because his lovingly-crafted function was rejected by our automated Perl::Critic check.
After I showed him how to silence the Perl::Critic check (as shown below) and get his code checked in, I wondered what the Perl Monks think of his coding style. Do you -- like Perl::Critic -- object to Misha's modification of the list items in the grep block below? How would you write the trim_nonempties function below?
To clarify the intent of this little function, the output produced by running the above program is shown below:use strict; use warnings; use Data::Dumper; # Returns non-empty strings with leading and trailing spaces removed. sub trim_nonempties { ## no critic (ProhibitMutatingListFunctions) grep { !/^\s*$/ && s/^\s*|\s*$//g } @_; ## use critic } my @x = ( ' hello ', "\thello again\t", '', " ", " \t", 'jock', "\t ab +c", "def \t " ); print Dumper(\@x); my @y = trim_nonempties(@x); print Dumper(\@y);
As you can see, it has trimmed leading and trailing whitepace from non-empty items in the list, while removing any white-space-only items.$VAR1 = [ ' hello ', ' hello again ', '', ' ', ' ', 'jock', ' abc', 'def ' ]; $VAR1 = [ 'hello', 'hello again', 'jock', 'abc', 'def' ];
|
|---|