sub trim { my ($s) = @_; $s =~ s/^\s+|\s$//g; return $s; } my $string = trim( EXPR );