my $last_match=0; while ($wholefile =~ /(pattern for table)/){ $newfile .= substr($wholefile, $last_match, ($last_match - $-[0])); $newfile.=eval{sub($1)}; # I think this is what you want to do #of course it will always return 0, #but that's what you have in the regex above. # perhaps you meant $newfile.= "sub($1)"; $last = $+[0]; }