If you don't need to preserve the output order in Result_file.txt you can reduce the runtime to a single pass over Input_file.dat:
# if @tag contains simple words
my $re = join '|', @tag;
# if they can be more complicated:
# my $re = join '|', map { "(?:$_)" } @tag;
open my $out, '+>', "Result_file.txt"
or die "Can't open file Result_file.txt for writing: $!";
open my $in, '<', 'Input_file.dat'
or die "Can't read Input_file.dat: $!";
while(<$in>){
print $out $_ if m/$re/o;
}
close $in;
close $out;
If you use perl 5.10.0, the match against many (constant) alternatives is blazingly fast due to the trie optimizations, demerphq++ |