Name Text::WordDiff - Track changes between documents Synopsis use Text::WordDiff; my $diff = word_diff 'file1.txt', 'file2.txt', { STYLE => 'HTML' }; my $diff = word_diff \$string1, \$string2, { STYLE => 'ANSIColor' }; my $diff = word_diff \*FH1, \*FH2; \%options; my $diff = word_diff \&reader1, \&reader2; my $diff = word_diff \@records1, \@records2; # May also mix input types: my $diff = word_diff \@records1, 'file_B.txt'; Description This module is a variation on the lovely Text::Diff module. Rather than generating traditional line-oriented diffs, however, it generates word-oriented diffs. This can be useful for tracking changes in narrative documents or documents with very long lines. To diff source code, one is still best off using Text::Diff. But if you want to see how a short story changed from one version to the next, this module will do the job very nicely. What is a Word? I'm glad you asked! Well, sort of. It's a really hard question to answer. I consulted a number of sources, but really just did my best to punt on the question by reformulating it as, "How do I split text up into individual words?" The short answer is to split on word boundaries. However, every word has two boundaries, one at the beginning and one at the end. So splitting on "/\b/" didn't work so well. What I really wanted to do was to split on the *beginning* of every word. Fortunately, _Mastering Regular Expressions_ has a recipe for that: "/(? "My::Foo""), in which case they will be instantiated by calling the "new()" construcctor and passing in the options hash reference, or as objects ("STYLE => My::Foo->new"). The simplest way to implement your own formatting style is to create a new class that inherits from Text::WordDiff::Base, wherein the "new()" method is already provided, and the "file_header()" returns a Unified diff-style header. All of the other formatting methods simply return empty strings, and are therefore ripe for overriding. * FILENAME_A, MTIME_A, FILENAME_B, MTIME_B The name of the file and the modification time "files" in epoch seconds. Unless a defined value is specified for these options, they will be filled in for each file when word_diff() is passed a filename. If a filename is not passed in and "FILENAME_A" and "FILENAME_B" are not defined, the header will not be printed by the base formatting base class. * OUTPUT The method by which diff output should be, well, *output*. Examples and their equivalent subroutines: OUTPUT => \*FOOHANDLE, # like: sub { print FOOHANDLE shift() } OUTPUT => \$output, # like: sub { $output .= shift } OUTPUT => \@output, # like: sub { push @output, shift } OUTPUT => sub { $output .= shift }, If "OUTPUT" is not defined, word_diff() will simply return the diff as a string. If "OUTPUT" is a code reference, it will be called once with the file header, once for each hunk body, and once for each piece of content. If "OUTPUT" is an IO::Handle-derived object, output will be sent to that handle. * FILENAME_PREFIX_A, FILENAME_PREFIX_B The string to print before the filename in the header. Defaults are "---", "+++". * DIFF_OPTS A hash reference to be passed as the options to "Algorithm::Diff->new". See Algorithm::Diff for details on available options. Formatting Classes Text::WordDiff comes with two formatting classes: Text::WordDiff::ANSIColor This is the default formatting class. It emits a header and then the diff content, with deleted text in bodfaced red and inserted text in boldfaced green. Text::WordDiff::HTML Specify "STYLE => 'HTML'" to take advantage of this formatting class. It outputs the diff content as XHTML, with deleted text in "" elements and inserted text in "" elements. To implement your own formatting class, simply inherit from Text::WordDiff::Base and override its methods as necssary. By default, only the "file_header()" formatting method returns a value. All others simply return empty strings, and are therefore ripe for overriding: package My::WordDiff::Format; use base 'Text::WordDiff::Base'; sub file_footer { return "End of diff\n"; } The methods supplied by the base class are: "new()" Constructs and returns a new formatting object. It takes a single hash reference as its argument, and uses it to construct the object. The nice thing about this is that if you want to support other options in your formatting class, you can just use them in the formatting object constructed by the Text::WordDiff::Base class and document that they can be passed as part of the options hash refernce to word_diff(). "file_header()" Called once for a single call to "word_diff()", this method outputs the header for the whole diff. This is the only formatting method in the base class that returns anything other than an empty string. It collects the filenames from "filname_a()" and "filename_b()" and, if they're defined, uses the relevant prefixes and modification times to return a unified diff-style header. "hunk_header()" This method is called for each diff hunk. It should output any necessary header for the hunk. "same_items()" This method is called for items that have not changed between the two sequnces being compared. The unchanged items will be passed as a list to the method. "delete_items" This method is called for items in the first sequence that are not present in the second sequcne. The deleted items will be passed as a list to the method. "insert_items" This method is called for items in the second sequence that are not present in the first sequcne. The inserted items will be passed as a list to the method. "hunk_footer" This method is called at the end of a hunk. It should output any necessary content to close out the hunk. "file_footer()" This method is called once when the whole diff has been procssed. It should output any necessary content to close out the diff file. "filename_a" This accessor returns the value specified for the "FILENAME_A" option to word_diff(). "filename_b" This accessor returns the value specified for the "FILENAME_B" option to word_diff(). "mtime_a" This accessor returns the value specified for the "MTIME_A" option to word_diff(). "mtime_b" This accessor returns the value specified for the "MTIME_B" option to word_diff(). "filename_prefix_a" This accessor returns the value specified for the "FILENAME_PREFIX_A" option to word_diff(). "filename_prefix_b" This accessor returns the value specified for the "FILENAME_PREFIX_B" option to word_diff(). See Also Text::Diff Inspired the interface and implementation of this module. Thanks Barry! Text::ParagraphDiff A module that attempts to diff paragraphs and the words in them. Algorithm::Diff The module that makes this all possible. Bugs Please send bug reports to . Author David Wheeler Copyright and License Copyright (c) 2005 Kineticode, Inc. All Rights Reserved. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.