$str = " sfsfsa "; trimwhitespace($str); sub trimwhitespace($) { my $string = shift; $string =~ s/^\s+//; $string =~ s/\s+$//; return $string; }