use re 'eval'; my $rx = qr/$rx_with_foo(?{ $_ =~ s+foo+bar+g })/