in reply to global match except last one

look at "look around assertions"
$str =~ s/(?<=[a-z])(?=[A-Z])/_/g; say uc $str;