foreach $file (@infile){ open(IN, '<', "$file") or warn $! and next; open(OUT, '>', $outpath.basename($file)) or warn $! and next; while () { substr($_,279) =~ s/$regex/$substitute{$1}/g; print OUT $_; } }