use Fcntl ':flock'; my @regexen; { open my $rx, '<', '/path/to/junk.txt' or die $!; @regexen = map { chomp; qr/\Q$_\E/ } <$rx>; close $rx or die $!; } for (@list_of_files) { local $/; open my $fh, '+<', $_ or warn $! and next; flock $fh, LOCK_EX; my $contents = <$fh>; # study $contents; # may want to try this $contents =~ s/$_//g for @regexen; seek $fh, 0, 0; print $fh $contents or warn $! and next; close $fh or warn $! and next; }