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

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

You want to do a

s/^\s+//

followed by a

s/\s+$//