in reply to Variables in regex pattern match give strange results.

Perl is looking for the variable $key_val and finding it undefined. Change your pattern string to $pattern = "s${key}_val";. Also, in the regexes, it would be good to say /\Q$pattern\E/ just in case there are any metacharacters in there.

After Compline,
Zaxo