my($b) = /(bar)/ ; # step 1 find the string my($substitute) = foo($b) ; # Step 2 make the substitution s/bar/$substitute/ ; # plug it in