$var =~s/\s+$//; # Output (as expected): $var is 'foo'