Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I am new to programming. I need to use perl two files, compare them and print out the differences in each of those two files along with the line numbers where these two files are different. How can I do this in Perl?
Ella

Replies are listed 'Best First'.
Re: Printing Line Numbers in output file
by locked_user sundialsvc4 (Abbot) on Jan 29, 2010 at 18:14 UTC

    What you are describing ... absolutely to a “t” ... is the diff command.

    If you cannot simply find a copy of that command (it's built-in to Unix/Linux and readily available also for Win32), there are a variety of Perl packages on search.cpan.org which deal specifically with diff operations in many different forms.

    A very closely related technology is called patch.

    Search using these keywords and you'll quickly find exactly what you need.

Re: Printing Line Numbers in output file
by ikegami (Patriarch) on Jan 29, 2010 at 18:37 UTC

    diff:

    system(diff => ( "--unchanged-line-format== %5dn %l\n", "--old-line-format=- %5dn %l\n", "--new-line-format=+ %5dn %l\n", $old_file, $new_file, ));
    or Algorithm::Diff
Re: Printing Line Numbers in output file
by CountZero (Bishop) on Jan 29, 2010 at 21:36 UTC
    Text::Diff accepts two filenames as arguments and has different output formats. It even provides hooks for your own formatting routines..

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James