http://www.perlmonks.org?node_id=1000218


in reply to File handles in regular expressions

First, three comments:

1. check the status of the open instructions;

2. chomp the lines you are reading to remove newline characters

3. use the eq operator instead of a regex, unless you have good reason to use regexes.

If the files are not too large (or, rather, if at least one of the files is not too large), read one of the files and store it in memory as a hash (using the full chomped line as the key). Once this is done, go through the other file and check if the line exists in the hash. If it exists, juste print it to your output file. This will be much faster than your nested foreach loops.