open(FILE,$template); foreach () { if ($_ =~ m/MY_MATCH/) { &do_something; } else { print $_; } } close(FILE);