in reply to Regex to replace $vars with subroutine
I agree with friedo. A template is the right way to go. The solution from Corion will work fine, but you may also want to look at How do I avoid double substitution when replacing many patterns?