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 abc", "def \t " ); print Dumper(\@x); my @y = trim_nonempties(@x); print Dumper(\@y); #### $VAR1 = [ ' hello ', ' hello again ', '', ' ', ' ', 'jock', ' abc', 'def ' ]; $VAR1 = [ 'hello', 'hello again', 'jock', 'abc', 'def' ];