# start your program with these. trust me use strict; use warnings; # hash table of search strings and insertion code my %insertions = ( 'search string' => 'code insertion', foo => 'bar', alpha => 'beta', ); # read in array of filenames from a directory opendir (my $DH, $directory); my @files = $readdir $DH; closedir $DH; # iterate over array of filenames for my $filename (@files){ # process your file here # or use a subroutine subName($filename); } # basic subroutine sub subName{ # get first arg passed my $filename = shift; # if you need a return value return $foo; } # opening files. $TFH = target filehandle open (my $TFH, ">", $targetfile); open (my $SFH, "<", $sourcefile); # read source file line by line while (my $line = <$SFH>){ print $TFH, $line; # use regular expression to find matching string if ($line =~ /$matchString/){ print $TFH, $snippet; } } close $TFH; close $SFH;