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?

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);
To clarify the intent of this little function, the output produced by running the above program is shown below:
$VAR1 = [ ' hello ', ' hello again ', '', ' ', ' ', 'jock', ' abc', 'def ' ]; $VAR1 = [ 'hello', 'hello again', 'jock', 'abc', 'def' ];
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.


In reply to Code review: function to trim whitespace from all items in a list by eyepopslikeamosquito

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.