.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Text::WordDiff 3pm" .TH Text::WordDiff 3pm "2022-12-12" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "Name" .IX Header "Name" Text::WordDiff \- Track changes between documents .SH "Synopsis" .IX Header "Synopsis" .Vb 1 \& use Text::WordDiff; \& \& my $diff = word_diff \*(Aqfile1.txt\*(Aq, \*(Aqfile2.txt\*(Aq, { STYLE => \*(AqHTML\*(Aq }; \& my $diff = word_diff \e$string1, \e$string2, { STYLE => \*(AqANSIColor\*(Aq }; \& my $diff = word_diff \e*FH1, \e*FH2; \e%options; \& my $diff = word_diff \e&reader1, \e&reader2; \& my $diff = word_diff \e@records1, \e@records2; \& \& # May also mix input types: \& my $diff = word_diff \e@records1, \*(Aqfile_B.txt\*(Aq; .Ve .SH "Description" .IX Header "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. .SS "What is a Word?" .IX Subsection "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, \*(L"How do I split text up into individual words?\*(R" 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 \&\f(CW\*(C`/\eb/\*(C'\fR didn't work so well. What I really wanted to do was to split on the \&\fIbeginning\fR of every word. Fortunately, _Mastering Regular Expressions_ has a recipe for that: \f(CW\*(C`/(? "My::Foo"\*(C'\fR), in which case they will be instantiated by calling the \f(CW\*(C`new()\*(C'\fR construcctor and passing in the options hash reference, or as objects (\f(CW\*(C`STYLE => My::Foo\->new\*(C'\fR). .Sp The simplest way to implement your own formatting style is to create a new class that inherits from Text::WordDiff::Base, wherein the \f(CW\*(C`new()\*(C'\fR method is already provided, and the \f(CW\*(C`file_header()\*(C'\fR returns a Unified diff-style header. All of the other formatting methods simply return empty strings, and are therefore ripe for overriding. .IP "\(bu" 4 \&\s-1FILENAME_A, MTIME_A, FILENAME_B, MTIME_B\s0 .Sp The name of the file and the modification time \*(L"files\*(R" in epoch seconds. Unless a defined value is specified for these options, they will be filled in for each file when \fBword_diff()\fR is passed a filename. If a filename is not passed in and \f(CW\*(C`FILENAME_A\*(C'\fR and \f(CW\*(C`FILENAME_B\*(C'\fR are not defined, the header will not be printed by the base formatting base class. .IP "\(bu" 4 \&\s-1OUTPUT\s0 .Sp The method by which diff output should be, well, \fIoutput\fR. Examples and their equivalent subroutines: .Sp .Vb 4 \& OUTPUT => \e*FOOHANDLE, # like: sub { print FOOHANDLE shift() } \& OUTPUT => \e$output, # like: sub { $output .= shift } \& OUTPUT => \e@output, # like: sub { push @output, shift } \& OUTPUT => sub { $output .= shift }, .Ve .Sp If \f(CW\*(C`OUTPUT\*(C'\fR is not defined, \fBword_diff()\fR will simply return the diff as a string. If \f(CW\*(C`OUTPUT\*(C'\fR 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 \&\f(CW\*(C`OUTPUT\*(C'\fR is an IO::Handle\-derived object, output will be sent to that handle. .IP "\(bu" 4 \&\s-1FILENAME_PREFIX_A, FILENAME_PREFIX_B\s0 .Sp The string to print before the filename in the header. Defaults are \f(CW"\-\-\-"\fR, \&\f(CW"+++"\fR. .IP "\(bu" 4 \&\s-1DIFF_OPTS\s0 .Sp A hash reference to be passed as the options to \f(CW\*(C`Algorithm::Diff\->new\*(C'\fR. See Algorithm::Diff for details on available options. .SH "Formatting Classes" .IX Header "Formatting Classes" Text::WordDiff comes with two formatting classes: .IP "Text::WordDiff::ANSIColor" 4 .IX Item "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. .IP "Text::WordDiff::HTML" 4 .IX Item "Text::WordDiff::HTML" Specify \f(CW\*(C`STYLE => \*(AqHTML\*(Aq\*(C'\fR to take advantage of this formatting class. It outputs the diff content as \s-1XHTML,\s0 with deleted text in \f(CW\*(C`\*(C'\fR elements and inserted text in \f(CW\*(C`\*(C'\fR elements. .PP To implement your own formatting class, simply inherit from Text::WordDiff::Base and override its methods as necssary. By default, only the \f(CW\*(C`file_header()\*(C'\fR formatting method returns a value. All others simply return empty strings, and are therefore ripe for overriding: .PP .Vb 2 \& package My::WordDiff::Format; \& use base \*(AqText::WordDiff::Base\*(Aq; \& \& sub file_footer { return "End of diff\en"; } .Ve .PP The methods supplied by the base class are: .ie n .IP """new()""" 4 .el .IP "\f(CWnew()\fR" 4 .IX Item "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 \fBword_diff()\fR. .ie n .IP """file_header()""" 4 .el .IP "\f(CWfile_header()\fR" 4 .IX Item "file_header()" Called once for a single call to \f(CW\*(C`word_diff()\*(C'\fR, 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 \f(CW\*(C`filname_a()\*(C'\fR and \f(CW\*(C`filename_b()\*(C'\fR and, if they're defined, uses the relevant prefixes and modification times to return a unified diff-style header. .ie n .IP """hunk_header()""" 4 .el .IP "\f(CWhunk_header()\fR" 4 .IX Item "hunk_header()" This method is called for each diff hunk. It should output any necessary header for the hunk. .ie n .IP """same_items()""" 4 .el .IP "\f(CWsame_items()\fR" 4 .IX Item "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. .ie n .IP """delete_items""" 4 .el .IP "\f(CWdelete_items\fR" 4 .IX Item "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. .ie n .IP """insert_items""" 4 .el .IP "\f(CWinsert_items\fR" 4 .IX Item "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. .ie n .IP """hunk_footer""" 4 .el .IP "\f(CWhunk_footer\fR" 4 .IX Item "hunk_footer" This method is called at the end of a hunk. It should output any necessary content to close out the hunk. .ie n .IP """file_footer()""" 4 .el .IP "\f(CWfile_footer()\fR" 4 .IX Item "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. .ie n .IP """filename_a""" 4 .el .IP "\f(CWfilename_a\fR" 4 .IX Item "filename_a" This accessor returns the value specified for the \f(CW\*(C`FILENAME_A\*(C'\fR option to \fBword_diff()\fR. .ie n .IP """filename_b""" 4 .el .IP "\f(CWfilename_b\fR" 4 .IX Item "filename_b" This accessor returns the value specified for the \f(CW\*(C`FILENAME_B\*(C'\fR option to \fBword_diff()\fR. .ie n .IP """mtime_a""" 4 .el .IP "\f(CWmtime_a\fR" 4 .IX Item "mtime_a" This accessor returns the value specified for the \f(CW\*(C`MTIME_A\*(C'\fR option to \&\fBword_diff()\fR. .ie n .IP """mtime_b""" 4 .el .IP "\f(CWmtime_b\fR" 4 .IX Item "mtime_b" This accessor returns the value specified for the \f(CW\*(C`MTIME_B\*(C'\fR option to \&\fBword_diff()\fR. .ie n .IP """filename_prefix_a""" 4 .el .IP "\f(CWfilename_prefix_a\fR" 4 .IX Item "filename_prefix_a" This accessor returns the value specified for the \f(CW\*(C`FILENAME_PREFIX_A\*(C'\fR option to \fBword_diff()\fR. .ie n .IP """filename_prefix_b""" 4 .el .IP "\f(CWfilename_prefix_b\fR" 4 .IX Item "filename_prefix_b" This accessor returns the value specified for the \f(CW\*(C`FILENAME_PREFIX_B\*(C'\fR option to \fBword_diff()\fR. .SH "See Also" .IX Header "See Also" .IP "Text::Diff" 4 .IX Item "Text::Diff" Inspired the interface and implementation of this module. Thanks Barry! .IP "Text::ParagraphDiff" 4 .IX Item "Text::ParagraphDiff" A module that attempts to diff paragraphs and the words in them. .IP "Algorithm::Diff" 4 .IX Item "Algorithm::Diff" The module that makes this all possible. .SH "Support" .IX Header "Support" This module is stored in an open GitHub repository . Feel free to fork and contribute! .PP Please file bug reports via GitHub Issues or by sending mail to bug\-Text\-WordDiff@rt.cpan.org . .SH "Author" .IX Header "Author" David E. Wheeler .PP Currently maintained by the developers of The Perl Shop . .SH "Copyright and License" .IX Header "Copyright and License" Copyright (c) 2005\-2011 David E. Wheeler. Some Rights Reserved. .PP This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.