Perl regex substitute from hash

Viewed 8634

Is there an efficient way to substitute a bunch a strings using values from a Perl hash?

For example,

$regex{foo} = "bar";
$regex{hello} = "world";
$regex{python} = "perl";

open(F, "myfile.txt");
while (<F>) {
      foreach $key (keys %regex) {
            s/$key/$regex{$key}/g;
      }
}
close(F);

Is there a way to accomplish the above in Perl?

7 Answers
Related