http://www.perlmonks.org?node_id=250912

Q&A > regular expressions > How do I remove whitespace at the beginning or end of my string? - Answer contributed by The Mad Hatter

Regex::Common is also a good choice.
use Regexp::Common qw/whitespace/; $foo =~ s/$RE{ws}{crop}//g;