$str =~ s/\s+\z//; is a far better (and more portable) solution.
In reply to Re^3: Strange behavior with STDIN by ikegami in thread Strange behavior with STDIN by slugger415