in reply to Re: Match a pattern only if it is not within another pattern
in thread Match a pattern only if it is not within another pattern

That gave me
bl123 and barthis123thatqux and barso123quxhim and123som 123


Update:
However this seems to do what is intended, at least with the test string:
$str =~ s/((?<!bar)\S*)foo((?!\S*qux))/${1}123${2}/gx;
gives
bl123 and barthisfoothatqux and barsofooquxhim and123som 123