foreach my $file (glob('*.txt')) { open(my $fh, $file) or die("Unable to open '$file': $!"); while (my $line = <$fh>) { if ($line =~ m/(Apple|Orange|Litchi)/) { $Sheet->Cells($row,$col-1)->{'Value'} = $file; $Sheet->Cells($row,$col+6)->{'Value'} = $line ; $row=$row+1; } } # close file close($fh); }