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>

In reply to Re: Compare multiple files and output differences by rovf
in thread Compare multiple files and output differences by prescott2006

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.