This is a module for computing the difference between two files, two strings, or any other two lists of things. It uses an intelligent algorithm similar to (or identical to) the one used by the Unix `diff' program. It is guaranteed to find the *smallest possible* set of differences. This package contains a few parts: Algorithm::Diff Module that contains the `diff' function, which computes the differences betwen two lists and returns a data structure that represents these differences. You can then use the data structure to generate a formatted output in whatever format you like. The `diff' programs included in this package use Algorithm::Diff::diff to find the differences, and then theyjust format the output. Algorithm::Diff also includes some other useful functions such as `LCS', which computes the longest common subsequence of two lists. A::D is suitable for many other uses. For example, you could use it for finding the smallest set of differences between two strings, or for computing the most efficient way to update the screen if you were replacing `curses'. diff.pl Implementation of `diff' in Perl that is as simple as possible so that you can see how it works. The output format is not compatible with regular `diff'. cdiff.pl `diff' that generates real context diffs in either traditional format or GNU unified format. Original contextless `context' diff supplied by Christian Murphy. Modifications to make it into a real full-featured diff with -c and -u options supplied by Amir D. Karger. Yes, you can use this program to generate patches. OTHER RESOURCES Visit my diff/LCS web page at http://www.plover.com/~mjd/perl/diff/. To join a low-volume mailing list for announcements related to diff and Algorithm::Diff, send an empty mail message to mjd-perl-diff-request@plover.com. `Longest Common Subsequences', at http://www.ics.uci.edu/~eppstein/161/960229.html THANKS SECTION Huge thanks to Amir Karger for adding full context diff supprt to `cdiff.pl', and then for waiting patiently for five months while I let it sit in a closet and didn't release it. Thank you thank you thank you, Amir! Thanks to Christian Murphy for adding the first context diff format suppoort to `cdiff.pl'. Thanks to Tim Bunce for his code that I haven't incorporated yet. Thanks to David Eppstein for his lucid page on dynamic programming and the Longest Common Subsequences problem, which provided 2/3 of the inspiration for this project. Thanks to Abigail for renaming her own modules so that this module could be Algorithm::Diff instead of Algorithms::Diff. Thanks to Nat Torkington for being himself. Thanks to the countless folks who showed up in comp.lang.perl.misc every month asking `how can I do `diff' in Perl' for the other 1/3 of the inspiration. ================================================================ =head1 NAME Algorithm::Diff - Compute `intelligent' differences between two files / lists =head1 SYNOPSIS use Algorithm::Diff qw(diff LCS trverse_sequences); @lcs = LCS(\@seq1, \@seq2, $comparison_function); @diffs = diff(\@seq1, \@seq2, $comparison_function); traverse_sequences(\@seq1, \@seq2, { MATCH => $callback, DISCARD_A => $callback, DISCARD_B => $callback, }, $comparison_function); =head1 INTRODUCTION I once read an article written by the authors of C; they said that they hard worked very hard on the algorithm until they found the right one. I think what they ended up using (and I hope someone will correct me, because I am not very confident about this) was the `longest common subsequence' method. in the LCS problem, you have two sequences of items: a b c d f g h j q z a b c d e f g i j k r x y z and you want to find the longest sequence of items that is present in both original sequences in the same order. That is, you want to find a new sequence I which can be obtained from the first sequence by deleting some items, and from the secend sequence by deleting other items. You also want I to be as long as possible. In this case I is a b c d f g j z From there it's only a small step to get diff-like output: e h i k q r x y + - + + - + + + This module solves the LCS problem. It also includes a canned function to generate C-like output. It might seem from the example above that the LCS of two sequences is always pretty obvious, but that's not always the case, especially when the two sequences have many repeated elements. For example, consider a x b y c z p d q a b c a x b y c z A naive approach might start by matching up the C and C that appear at the beginning of each sequence, like this: a x b y c z p d q a b c a b y c z This finds the common subsequence C. But actually, the LCS is C: a x b y c z p d q a b c a x b y c z =head1 USAGE This module exports three functions, which we'll deal with in ascending order of difficulty: C, C, and C. =head2 C Given references to two lists of items, C returns a list containing their longest common subsequence. In scalar context, it returns a reference to such a list. @lcs = LCS(\@seq1, \@seq2, $comparison_function); $lcsref = LCS(\@seq1, \@seq2, $comparison_function); C<$comparison_function>, if supplied, should be a function that gets an item from each input list and returns true if they are considered equal. It is optional, and if omitted, defaults to `eq'. =head2 C @diffs = diff(\@seq1, \@seq2, $comparison_function); $diffs_ref = diff(\@seq1, \@seq2, $comparison_function); C computes the smallest set of additions and deletions necessary to turn the first sequence into the second, and returns a description of these changes. The description is a list of I; each hunk represents a contiguous section of items which should be added, deleted, or replaced. The return value of C is a list of hunks, or, in scalar context, a reference to such a list. Here is an example: The diff of the following two sequences: a b c e h j l m n p b c d e f j k l m r s t Result: [ [ [ '-', 0, 'a' ] ], [ [ '+', 2, 'd' ] ], [ [ '-', 4, 'h' ] , [ '+', 4, 'f' ] ], [ [ '+', 6, 'k' ] ], [ [ '-', 8, 'n' ], [ '-', 9, 'p' ], [ '+', 9, 'r' ], [ '+', 10, 's' ], [ '+', 11, 't' ], ] ] There are five hunks here. The first hunk says that the C at position 0 of the first sequence should be deleted (C<->). The second hunk says that the C at position 2 of the second sequence should be inserted (C<+>). The third hunk says that the C at position 4 of the first sequence should be removed and replaced with the C from position 4 of the second sequence. The other two hunks similarly. C accepts an optional comparison function; if specified, it will be called with pairs of elements and is expected to return true if the elements are considered equal. If not specified, it defaults to C. =head2 C C is the most general facility provided by this module; C and C are implemented as calls to it. Imagine that there are two arrows. Arrow A points to an element of sequence A, and arrow B points to an element of the sequence B. Initially, the arrows point to the first elements of the respective sequences. C will advance the arrows through the sequences one element at a time, calling an appropriate user-specified callback function before each advance. It willadvance the arrows in such a way that if there are equal elements C<$A[$i]> and C<$B[$j]> which are equal and which are part of the LCS, there will be some moment during the execution of C when arrow A is pointing to C<$A[$i]> and arrow B is pointing to C<$B[$j]>. When this happens, C will call the C callback function and then it will advance both arrows. Otherwise, one of the arrows is pointing to an element of its sequence that is not part of the LCS. C will advance that arrow and will call the C or the C callback, depending on which arrow it advanced. If both arrows point to elements that are not part of the LCS, then C will advance one of them and call the appropriate callback, but it is not specified which it will call. The arguments to C are the two sequences to traverse, and a callback which specifies the callback functions, like this: traverse_sequences(\@seq1, \@seq2, { MATCH => $callback_1, DISCARD_A => $callback_2, DISCARD_B => $callback_3, }, ); Callbacks are invoked with at least the indices of the two arrows as their arguments. They are not expected to return any values. If a callback is omitted from the table, it is not called. If arrow A reaches the end of its sequence, before arrow B does, C will call the C callback when it advances arrow B, if there is such a function; if not it will call C instead. Similarly if arrow B finishes first. C returns when both arrows are at the ends of their respective sequences. It returns true on success and false on failure. At present there is no way to fail. C accepts an optional comparison function; if specified, it will be called with pairs of elements and is expected to return true if the elements are considered equal. If not specified, or if C, it defaults to C. Any additional arguments to C are passed to the callback functions. For examples of how to use this, see the code. the C and C functions are implemented on top of C. =head1 MAILING LIST To join a low-volume mailing list for announcements related to diff and Algorithm::Diff, send an empty mail message to mjd-perl-diff-request@plover.com. =head1 AUTHOR Mark-Jason Dominus, mjd-perl-diff@plover.com. Visit my diff/LCS web page at http://www.plover.com/~mjd/perl/diff/.