in reply to How do I speed this up?

Look at perlman:perlfaq6 for the section "How do I efficiently match many regular expressions at once?"

Another option is to build a regular expression like this:

s/(foo|bar|baz)/$constants{$1}/g;
where "foo", "bar", and "baz" are your constants, the keys in your %constants hash. You'll want to use \Q and \E around each of the constants if they contain any regexp metacharacters.