in reply to Substitutions if hash entry exists

You have to make the RHS executable, by adding a /e modifier, and do the check there. I think this will do for your case:
s/(\$\w+)/exists $variables{$1} ? $variables{$1} : $1/ge;
but personally, I'd remove the '$' from the hash keys, and go for
s/(\$(\w+))/exists $variables{$2} ? $variables{$2} : $1/ge;