Many of you know the adage; but learning it anew can still be fun.
I had to trim leading and trailing whitespace in Perl. So:
# strip any leading or trailing whitespace $string =~ s/^\s+//; $string =~ s/\s+$//;
Then I made the mistake I often make, and started thinking. Two lines for that? Please. That can be done in one line, especially in Perl. So I found this:
$string =~ s/^\s*(\S*(?:\s+\S+)*)\s*$/$1/;
Yuck! Oh well, at least it probably runs faster.
Or better yet, Marcus, just stop thinking altogether. This one-liner idea would have made the routine both slower and less readable for the next person (notice I forgot the #comment the second time).