sub foo { return reverse uc shift } $_ = "this has bar and foo\n"; s/(bar|foo)/foo($1)/eg; print; #=> this has RAB and OOF