$_ = 'foo bar foobar barfoo 1 2 3'; s/(\w)(\w*)/\U$1\E$2/g; print; # Foo Bar Foobar Barfoo 1 2 3