in reply to Compare multiple files and output differences

As for your question about calculating the differences, you could for instance use Text::Diff from CPAN. This has a pretty general interface and you can use it also for data stored inside your program.

However, your program has some oddities. First of all, by all means add use strict; use warnings; to your code (and then fix all the error- and warning messages you will get after that). Also, always check whether opening the file works (as you did when opening file1). Then, though not forbidden, it is odd that you declare your subroutine grep_pattern inside the while loop. I think you want to make it a closure (using $pattern); however, in the way you wrote it, $pattern is a global variable and would be available to grep_pattern even if you declare the sub outside the block.

There are more issues to your program, though, and I wonder to what extent you have tested the code already; but please fix first the items I have mentioned, and let's discuss the remaining ones afterwards.
-- 
Ronald Fischer <ynnor@mm.st>