What about undef? It can be even faster:

use 5.16.2; #use warnings; use Test::More; use Benchmark qw( cmpthese ); # Returns non-empty strings with leading and trailing spaces removed. sub mca # McA { grep { $_ ne "" } map { s/^\s+//; s/\s+$//; $_ } @_; } sub tux # Tux { map { defined $_ ? m/(\S(?:.*\S)?)/ : () } @_; } sub choroba # choroba { grep !m/^$/ => map s/^\s+|\s+$//gr => @_ } sub eplam # eyepopslikeamosquito { grep { !m/^\s*$/ && s/^\s*|\s*$//g } @_; } my @x = (" hello ", "\thello again\t", "", " ", undef, " \t", "jock", "\t abc", "def \t "); my @e = ("hello", "hello again", "jock", "abc", "def"); is_deeply ([ mca (@x) ], \@e, "mca"); is_deeply ([ tux (@x) ], \@e, "tux"); is_deeply ([ choroba (@x) ], \@e, "choroba"); is_deeply ([ eplam (@x) ], \@e, "eplam"); cmpthese (1000000, { "mca" => sub { my @y = mca (@x); }, "tux" => sub { my @y = tux (@x); }, "choroba" => sub { my @y = choroba (@x); }, "eplam" => sub { my @y = eplam (@x); }, }); done_testing; __END__ ok 1 - mca ok 2 - tux ok 3 - choroba ok 4 - eplam Rate eplam choroba mca tux eplam 100908/s -- -40% -57% -66% choroba 169205/s 68% -- -29% -43% mca 236967/s 135% 40% -- -20% tux 297619/s 195% 76% 26% -- 1..4

Enjoy, Have FUN! H.Merijn

In reply to Re^3: Code review: function to trim whitespace from all items in a list by Tux
in thread 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.