I have two text files which need to have the same values.
$ diff A.txt B.txt
4a5
> I have this extra line.
$
Open files in Perl
open (ONE, "<A.txt");
open (TWO, "<B.txt");
How can I do such a diff from within Perl? Does Perl have a inbuilt diff or do I need to use the unix diff
utility? I don't want to implement my own diff algorithm for this.
I do need the information as to where my files differ, but I do not need to use the unix diff
utility necessarily. That was just an example.
We can compare two files to see if they are the same in Perl using the compare() method. It is a method of the subroutine File::Compare . It compares files line by line. If a difference is detected, it stops.
On the File menu, click Compare Files. In the Select First File dialog box, locate and then click a file name for the first file in the comparison, and then click Open. In the Select Second File dialog box, locate and then click a file name for the second file in the comparison, and then click Open.
Comparison Using cmp GNU cmp compares two files byte by byte and prints the location of the first difference. We can pass the -s flag to find out if the files have the same content. Since the contents of file1 and file2 are different, cmp exited with status 1.
You could try using Text::Diff
Alternatively, the UNIX utility could be an option.
If I only needed to know that they were the same (i.e. not discover how they are different), I'd just use Digest::MD5 to see if they come up with the same digest. There's a vanishingly small chance that two different files could have the same MD5 digest, so you might even try Digest::SHA1.
If you want to find out which lines are different, then you can use Algorithm::Diff, perhaps in conjunction with Tie::File. However, there is also a diff program that comes with Algorithm::Diff if you don't have a diff tool on your target platform. Although you can shell out to that, you might just want to copy what it does into a subroutine. Text::Diff is built on top of Algorithm::Diff, so it might already do want you want.
No, Perl doesn't have an inbuilt "diff" facility. Either you use an external module, or use Perl's data structures(hashes, arrays etc) or you create filehandles for both files, and iterate the files using the filehandle (while loops), comparing them line by line. This method assumes your files are sorted. Another not so elegant way is to call "diff" from Perl, but I advise against that.
Lastly, if Perl is not a must, just use the Unix diff
utility (write a shell script).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With