$ perl -Mstrict -Mwarnings -E ' my $str = " \t item0 with leading whitespace |item1 | item2| item3 | extra item with embedded whitespace \t \n \n\t |item4| | itemN with trailing whitespace \t "; say "---- WITHOUT SUBROUTINE ---"; say ">$_<" for map { (/\A\s*(.*?)\s*\z/) } split /\|/, $str; say "---- USING SUBROUTINE ---"; say "<$_>" for map { clean($_) } split /\|/, $str; sub clean { ($_[0] =~ /\A\s*(.*?)\s*\z/)[0] } ' ---- WITHOUT SUBROUTINE --- >item0 with leading whitespace< >item1< >item2< >item3< >extra item with embedded whitespace< >item4< >< >itemN with trailing whitespace< ---- USING SUBROUTINE --- <>