Text-Diff-1.45/000755 000765 000024 00000000000 13145110537 013471 5ustar00neilbstaff000000 000000 Text-Diff-1.45/Changes000644 000765 000024 00000007644 13145110505 014772 0ustar00neilbstaff000000 000000 Revision history for Perl module Text::Diff 1.45 2017-08-16 NEILB - Typo fixed in doc. Patch from Lucas Kanashiro++. RT#112564 - Documentation improvements PR from David Gutteridge++. 1.44 2016-02-26 NEILB - Fixed the handling of text without a trailing newline. This module now adds the same text that the diff tool does, "\n\\ No newline at end of file\n". Previously this module simply produced broken diffs when one or both files had no trailing newline. Fixed by Dave Rolsky. GitHub #1. 1.43 2015-08-21 NEILB - Got rid of the "Redundant argument in sprintf" warnings from Text:Diff::Table on Perl 5.021+. RT#100505 and RT#106602. - Metadata and doc now refer to NEILB's repo rather than OVID's. 1.42 2015-08-20 NEILB - Fixed pod link that was referring to the wrong place. Thanks to KENTNL for RT#106150. - First non-developer released of the changes listed against 1.41_01. 1.41_01 2015-06-02 NEILB - Developer release with changes I submitted for my January PRC assignment. - Updated all modules to "use warnings", and use "our ...", so bumped min perl version to 5.006, and added MIN_PERL_VERSION to Makefile.PL - RT#73505: if a file doesn't exist, you't get a warning about trying to read a closed filehandle. Now we croak. - RT#94895: blank line needed for =head1 OPTIONS to format properly. - RT#51612: the '>' character needed escaping inside pod formatting code. - RT#25283: fixed syntax error in SYNOPSIS of Text::Diff::Table - RT#101553: updated FSF postal address in LICENSE - Added SEE ALSO section to doc, with links to a bunch of Diff modules. - Added link to github repository in the doc - Deleted META.yml from the repo -- EUMM will generate it for us - Reformatted this file as per CPAN::Changes::Spec - Added TODO.md, with two further things I can see need doing. - Removed all tab characters from source 1.41 2011-04-17 OVID - Add Text::Dif::Config to MANIFEST. 1.40 2011-04-16 OVID - Added DIFF_OUTPUT_UNICODE env variable to allow outputting unicode characters. Thanks to Shlomi Fish for the patch. 1.37 2009-07-16 ADAMK - Switching to a production release - Switching to a 1.xx version to indicate this - Added META.yml file 1.36_01 2009-07-13 ADAMK - Cleaning this up to pass tests - Adding author tests 0.35 2002-08-27 RBS - Escape whitespace if a blank line is inserted or removed. This puts a "\n" in the column containing the blank line, for instance. 0.34 2002-07-14 RBS - Improved Table format's escaping - that "\t" and "\\t" are displayed differently - the entire line is escaped consistently if it is escaped at all. 0.33 2002-07-08 RBS - Make diff() return "" instead of 0 when comparing two empty things. Patch from Rolf Grossmann . 0.32 2002-03-14 RBS - Fix escaping of all-whitespace strings. 0.31 2002-02-06 RBS - Remove stray $SIG{__DIE__} that was confessing on every die. 0.3 2001-12-21 RBS - undocumented 0.11 2001-12-10 RBS - Remove hardcoded date string from t/general.t's test data, because localtime is used to generate this in Diff.pm and the local machine's timezone (and locale?) can cause the localtime for a given mtime to be quite different. Reported by Andreas Marcel Riechert of cpan-testers. - Start this Changes file. - Added hunk_header() and hunk_footer() for symmetry in overloading. - Added t/ext_format.t 0.1 2001-12-09 RBS - Initial public release. - Added filename, filehandle, and string I/O options - API resembles Algorithm::Diff's a bit more - Reimplement output formats as classes so that external (user-supplied) can be specified as class names (My::Diff::Format) and so that they may be inherited from. Should probably break out hunk_header() from hunk(). - Add footer() to all formats Text-Diff-1.45/lib/000755 000765 000024 00000000000 13145110537 014237 5ustar00neilbstaff000000 000000 Text-Diff-1.45/LICENSE000644 000765 000024 00000047416 12662404103 014510 0ustar00neilbstaff000000 000000 Terms of Perl itself a) the GNU General Public License as published by the Free Software Foundation; either version 1, or (at your option) any later version, or b) the "Artistic License" ---------------------------------------------------------------------------- The General Public License (GPL) Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS ---------------------------------------------------------------------------- The Artistic License Preamble The intent of this document is to state the conditions under which a Package may be copied, such that the Copyright Holder maintains some semblance of artistic control over the development of the package, while giving the users of the package the right to use and distribute the Package in a more-or-less customary fashion, plus the right to make reasonable modifications. Definitions: - "Package" refers to the collection of files distributed by the Copyright Holder, and derivatives of that collection of files created through textual modification. - "Standard Version" refers to such a Package if it has not been modified, or has been modified in accordance with the wishes of the Copyright Holder. - "Copyright Holder" is whoever is named in the copyright or copyrights for the package. - "You" is you, if you're thinking about copying or distributing this Package. - "Reasonable copying fee" is whatever you can justify on the basis of media cost, duplication charges, time of people involved, and so on. (You will not be required to justify it to the Copyright Holder, but only to the computing community at large as a market that must bear the fee.) - "Freely Available" means that no fee is charged for the item itself, though there may be fees involved in handling the item. It also means that recipients of the item may redistribute it under the same conditions they received it. 1. You may make and give away verbatim copies of the source form of the Standard Version of this Package without restriction, provided that you duplicate all of the original copyright notices and associated disclaimers. 2. You may apply bug fixes, portability fixes and other modifications derived from the Public Domain or from the Copyright Holder. A Package modified in such a way shall still be considered the Standard Version. 3. You may otherwise modify your copy of this Package in any way, provided that you insert a prominent notice in each changed file stating how and when you changed that file, and provided that you do at least ONE of the following: a) place your modifications in the Public Domain or otherwise make them Freely Available, such as by posting said modifications to Usenet or an equivalent medium, or placing the modifications on a major archive site such as ftp.uu.net, or by allowing the Copyright Holder to include your modifications in the Standard Version of the Package. b) use the modified Package only within your corporation or organization. c) rename any non-standard executables so the names do not conflict with standard executables, which must also be provided, and provide a separate manual page for each non-standard executable that clearly documents how it differs from the Standard Version. d) make other distribution arrangements with the Copyright Holder. 4. You may distribute the programs of this Package in object code or executable form, provided that you do at least ONE of the following: a) distribute a Standard Version of the executables and library files, together with instructions (in the manual page or equivalent) on where to get the Standard Version. b) accompany the distribution with the machine-readable source of the Package with your modifications. c) accompany any non-standard executables with their corresponding Standard Version executables, giving the non-standard executables non-standard names, and clearly documenting the differences in manual pages (or equivalent), together with instructions on where to get the Standard Version. d) make other distribution arrangements with the Copyright Holder. 5. You may charge a reasonable copying fee for any distribution of this Package. You may charge any fee you choose for support of this Package. You may not charge a fee for this Package itself. However, you may distribute this Package in aggregate with other (possibly commercial) programs as part of a larger (possibly commercial) software distribution provided that you do not advertise this Package as a product of your own. 6. The scripts and library files supplied as input to or produced as output from the programs of this Package do not automatically fall under the copyright of this Package, but belong to whomever generated them, and may be sold commercially, and may be aggregated with this Package. 7. C or perl subroutines supplied by you and linked into this Package shall not be considered part of this Package. 8. The name of the Copyright Holder may not be used to endorse or promote products derived from this software without specific prior written permission. 9. THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. The End Text-Diff-1.45/Makefile.PL000644 000765 000024 00000001707 12662404103 015446 0ustar00neilbstaff000000 000000 use strict; BEGIN { require 5.005_03; } use ExtUtils::MakeMaker; my $mm_ver = $ExtUtils::MakeMaker::VERSION; if ($mm_ver =~ /_/) { # dev version $mm_ver = eval $mm_ver; die $@ if $@; } WriteMakefile( NAME => 'Text::Diff', ABSTRACT => 'Perform diffs on files and record sets', VERSION_FROM => 'lib/Text/Diff.pm', PREREQ_PM => { 'Exporter' => 0, 'Algorithm::Diff' => '1.19', }, ( $] >= 5.005 ? ( AUTHOR => 'Adam Kennedy ', ) : () ), ( $ExtUtils::MakeMaker::VERSION ge '6.30_00' ? ( LICENSE => 'perl', ) : () ), ( $ExtUtils::MakeMaker::VERSION ge '6.46' ? ( META_MERGE => { build_requires => { Test => 0 } } ) : () ), META_MERGE => { resources => { repository => 'https://github.com/neilbowers/Text-Diff' } }, ($mm_ver >= 6.48 ? (MIN_PERL_VERSION => 5.006) : () ), ); Text-Diff-1.45/MANIFEST000644 000765 000024 00000000622 13145110537 014622 0ustar00neilbstaff000000 000000 Changes lib/Text/Diff.pm lib/Text/Diff/Config.pm lib/Text/Diff/Table.pm LICENSE Makefile.PL MANIFEST This list of files README t/97_meta.t t/98_pod.t t/99_pmv.t t/ext_format.t t/general.t t/inputs.t t/keygen.t t/outputs.t t/table.t META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) Text-Diff-1.45/META.json000644 000765 000024 00000002142 13145110537 015111 0ustar00neilbstaff000000 000000 { "abstract" : "Perform diffs on files and record sets", "author" : [ "Adam Kennedy " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.24, CPAN::Meta::Converter version 2.150005", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Text-Diff", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "Algorithm::Diff" : "1.19", "Exporter" : "0", "perl" : "5.006" } } }, "release_status" : "stable", "resources" : { "repository" : { "url" : "https://github.com/neilbowers/Text-Diff" } }, "version" : "1.45", "x_serialization_backend" : "JSON::PP version 2.27203" } Text-Diff-1.45/META.yml000644 000765 000024 00000001231 13145110537 014737 0ustar00neilbstaff000000 000000 --- abstract: 'Perform diffs on files and record sets' author: - 'Adam Kennedy ' build_requires: ExtUtils::MakeMaker: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.24, CPAN::Meta::Converter version 2.150005' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Text-Diff no_index: directory: - t - inc requires: Algorithm::Diff: '1.19' Exporter: '0' perl: '5.006' resources: repository: https://github.com/neilbowers/Text-Diff version: '1.45' x_serialization_backend: 'CPAN::Meta::YAML version 0.016' Text-Diff-1.45/README000644 000765 000024 00000025404 12452327560 014364 0ustar00neilbstaff000000 000000 NAME Text::Diff - Perform diffs on files and record sets SYNOPSIS use Text::Diff; ## Mix and match filenames, strings, file handles, producer subs, ## or arrays of records; returns diff in a string. ## WARNING: can return B diffs for large files. my $diff = diff "file1.txt", "file2.txt", { STYLE => "Context" }; my $diff = diff \$string1, \$string2, \%options; my $diff = diff \*FH1, \*FH2; my $diff = diff \&reader1, \&reader2; my $diff = diff \@records1, \@records2; ## May also mix input types: my $diff = diff \@records1, "file_B.txt"; DESCRIPTION "diff()" provides a basic set of services akin to the GNU "diff" utility. It is not anywhere near as feature complete as GNU "diff", but it is better integrated with Perl and available on all platforms. It is often faster than shelling out to a system's "diff" executable for small files, and generally slower on larger files. Relies on Algorithm::Diff for, well, the algorithm. This may not produce the same exact diff as a system's local "diff" executable, but it will be a valid diff and comprehensible by "patch". We haven't seen any differences between Algorithm::Diff's logic and GNU diff's, but we have not examined them to make sure they are indeed identical. Note: If you don't want to import the "diff" function, do one of the following: use Text::Diff (); require Text::Diff; That's a pretty rare occurence, so "diff()" is exported by default. =head1 OPTIONS diff() takes two parameters from which to draw input and a set of options to control it's output. The options are: FILENAME_A, MTIME_A, FILENAME_B, MTIME_B The name of the file and the modification time "files" These are filled in automatically for each file when diff() is passed a filename, unless a defined value is passed in. If a filename is not passed in and FILENAME_A and FILENAME_B are not provided or "undef", the header will not be printed. Unused on "OldStyle" diffs. OFFSET_A, OFFSET_B The index of the first line / element. These default to 1 for all parameter types except ARRAY references, for which the default is 0. This is because ARRAY references are presumed to be data structures, while the others are line oriented text. STYLE "Unified", "Context", "OldStyle", or an object or class reference for a class providing "file_header()", "hunk_header()", "hunk()", "hunk_footer()" and "file_footer()" methods. The two footer() methods are provided for overloading only; none of the formats provide them. Defaults to "Unified" (unlike standard "diff", but Unified is what's most often used in submitting patches and is the most human readable of the three. If the package indicated by the STYLE has no hunk() method, c will load it automatically (lazy loading). Since all such packages should inherit from Text::Diff::Base, this should be marvy. Styles may be specified as class names ("STYLE =" "Foo"), in which case they will be "new()"ed with no parameters, or as objects ("STYLE =" Foo->new>). CONTEXT How many lines before and after each diff to display. Ignored on old-style diffs. Defaults to 3. 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 no "OUTPUT" is supplied, returns the diffs in a string. If "OUTPUT" is a "CODE" ref, it will be called once with the (optional) file header, and once for each hunk body with the text to emit. If "OUTPUT" is an IO::Handle, output will be emitted to that handle. FILENAME_PREFIX_A, FILENAME_PREFIX_B The string to print before the filename in the header. Unused on "OldStyle" diffs. Defaults are "---", "+++" for Unified and "***", "+++" for Context. KEYGEN, KEYGEN_ARGS These are passed to "traverse_sequences" in Algorithm::Diff. Note: if neither "FILENAME_" option is defined, the header will not be printed. If at one is present, the other and both MTIME_ options must be present or "Use of undefined variable" warnings will be generated (except on "OldStyle" diffs, which ignores these options). Formatting Classes These functions implement the output formats. They are grouped in to classes so diff() can use class names to call the correct set of output routines and so that you may inherit from them easily. There are no constructors or instance methods for these classes, though subclasses may provide them if need be. Each class has file_header(), hunk_header(), hunk(), and footer() methods identical to those documented in the Text::Diff::Unified section. header() is called before the hunk() is first called, footer() afterwards. The default footer function is an empty method provided for overloading: sub footer { return "End of patch\n" } Some output formats are provided by external modules (which are loaded automatically), such as Text::Diff::Table. These are are documented here to keep the documentation simple. Text::Diff::Base Returns "" for all methods (other than "new()"). Text::Diff::Unified --- A Mon Nov 12 23:49:30 2001 +++ B Mon Nov 12 23:49:30 2001 @@ -2,13 +2,13 @@ 2 3 4 -5d +5a 6 7 8 9 +9a 10 11 -11d 12 13 file_header $s = Text::Diff::Unified->file_header( $options ); Returns a string containing a unified header. The sole parameter is the options hash passed in to diff(), containing at least: FILENAME_A => $fn1, MTIME_A => $mtime1, FILENAME_B => $fn2, MTIME_B => $mtime2 May also contain FILENAME_PREFIX_A => "---", FILENAME_PREFIX_B => "+++", to override the default prefixes (default values shown). hunk_header Text::Diff::Unified->hunk_header( \@ops, $options ); Returns a string containing the output of one hunk of unified diff. Text::Diff::Unified::hunk Text::Diff::Unified->hunk( \@seq_a, \@seq_b, \@ops, $options ); Returns a string containing the output of one hunk of unified diff. Text::Diff::Table +--+----------------------------------+--+------------------------------+ | |../Test-Differences-0.2/MANIFEST | |../Test-Differences/MANIFEST | | |Thu Dec 13 15:38:49 2001 | |Sat Dec 15 02:09:44 2001 | +--+----------------------------------+--+------------------------------+ | | * 1|Changes * | 1|Differences.pm | 2|Differences.pm | | 2|MANIFEST | 3|MANIFEST | | | * 4|MANIFEST.SKIP * | 3|Makefile.PL | 5|Makefile.PL | | | * 6|t/00escape.t * | 4|t/00flatten.t | 7|t/00flatten.t | | 5|t/01text_vs_data.t | 8|t/01text_vs_data.t | | 6|t/10test.t | 9|t/10test.t | +--+----------------------------------+--+------------------------------+ This format also goes to some pains to highlight "invisible" characters on differing elements by selectively escaping whitespace: +--+--------------------------+--------------------------+ | |demo_ws_A.txt |demo_ws_B.txt | | |Fri Dec 21 08:36:32 2001 |Fri Dec 21 08:36:50 2001 | +--+--------------------------+--------------------------+ | 1|identical |identical | * 2| spaced in | also spaced in * * 3|embedded space |embedded tab * | 4|identical |identical | * 5| spaced in |\ttabbed in * * 6|trailing spaces\s\s\n |trailing tabs\t\t\n * | 7|identical |identical | * 8|lf line\n |crlf line\r\n * * 9|embedded ws |embedded\tws * +--+--------------------------+--------------------------+ See "Text::Diff::Table" for more details, including how the whitespace escaping works. Text::Diff::Context *** A Mon Nov 12 23:49:30 2001 --- B Mon Nov 12 23:49:30 2001 *************** *** 2,14 **** 2 3 4 ! 5d 6 7 8 9 10 11 - 11d 12 13 --- 2,14 ---- 2 3 4 ! 5a 6 7 8 9 + 9a 10 11 12 13 Note: hunk_header() returns only "***************\n". Text::Diff::OldStyle 5c5 < 5d --- > 5a 9a10 > 9a 12d12 < 11d Note: no file_header(). LIMITATIONS Must suck both input files entirely in to memory and store them with a normal amount of Perlish overhead (one array location) per record. This is implied by the implementation of Algorithm::Diff, which takes two arrays. If Algorithm::Diff ever offers an incremental mode, this can be changed (contact the maintainers of Algorithm::Diff and Text::Diff if you need this; it shouldn't be too terribly hard to tie arrays in this fashion). Does not provide most of the more refined GNU diff options: recursive directory tree scanning, ignoring blank lines / whitespace, etc., etc. These can all be added as time permits and need arises, many are rather easy; patches quite welcome. Uses closures internally, this may lead to leaks on "perl" versions 5.6.1 and prior if used many times over a process' life time. AUTHOR Adam Kennedy Barrie Slaymaker COPYRIGHT Some parts copyright 2009 Adam Kennedy. Copyright 2001 Barrie Slaymaker. All Rights Reserved. You may use this under the terms of either the Artistic License or GNU Public License v 2.0 or greater. Text-Diff-1.45/t/000755 000765 000024 00000000000 13145110537 013734 5ustar00neilbstaff000000 000000 Text-Diff-1.45/t/97_meta.t000644 000765 000024 00000001073 12452327560 015375 0ustar00neilbstaff000000 000000 #!/usr/bin/perl # Test that our META.yml file matches the current specification. use strict; BEGIN { $| = 1; $^W = 1; } my $MODULE = 'Test::CPAN::Meta 0.12'; # Don't run tests for installs use Test::More; unless ( $ENV{AUTOMATED_TESTING} or $ENV{RELEASE_TESTING} ) { plan( skip_all => "Author tests not required for installation" ); } # Load the testing module eval "use $MODULE"; if ( $@ ) { $ENV{RELEASE_TESTING} ? die( "Failed to load required release-testing module $MODULE" ) : plan( skip_all => "$MODULE not available for testing" ); } meta_yaml_ok(); Text-Diff-1.45/t/98_pod.t000644 000765 000024 00000001167 12452327560 015236 0ustar00neilbstaff000000 000000 #!/usr/bin/perl # Test that the syntax of our POD documentation is valid use strict; BEGIN { $| = 1; $^W = 1; } my @MODULES = ( 'Pod::Simple 3.07', 'Test::Pod 1.26', ); # Don't run tests for installs use Test::More; unless ( $ENV{AUTOMATED_TESTING} or $ENV{RELEASE_TESTING} ) { plan( skip_all => "Author tests not required for installation" ); } # Load the testing modules foreach my $MODULE ( @MODULES ) { eval "use $MODULE"; if ( $@ ) { $ENV{RELEASE_TESTING} ? die( "Failed to load required release-testing module $MODULE" ) : plan( skip_all => "$MODULE not available for testing" ); } } all_pod_files_ok(); Text-Diff-1.45/t/99_pmv.t000644 000765 000024 00000001247 12452327560 015256 0ustar00neilbstaff000000 000000 #!/usr/bin/perl # Test that our declared minimum Perl version matches our syntax use strict; BEGIN { $| = 1; $^W = 1; } my @MODULES = ( 'Perl::MinimumVersion 1.20', 'Test::MinimumVersion 0.008', ); # Don't run tests for installs use Test::More; unless ( $ENV{AUTOMATED_TESTING} or $ENV{RELEASE_TESTING} ) { plan( skip_all => "Author tests not required for installation" ); } # Load the testing modules foreach my $MODULE ( @MODULES ) { eval "use $MODULE"; if ( $@ ) { $ENV{RELEASE_TESTING} ? die( "Failed to load required release-testing module $MODULE" ) : plan( skip_all => "$MODULE not available for testing" ); } } all_minimum_version_from_metayml_ok(); Text-Diff-1.45/t/ext_format.t000644 000765 000024 00000002546 12452327560 016306 0ustar00neilbstaff000000 000000 #!/usr/bin/perl use strict ; use Test ; use Text::Diff ; use Algorithm::Diff qw( traverse_sequences ) ; my @A = map "$_\n", qw( 1 a 2 b 3 ) ; my @B = map "$_\n", qw( 1 A 2 B 3 ) ; ## This tests both that we can overload all 5 methods and that all 5 ## methods are called by diff() (and in the right order :) my $f = "My::Diff::Format" ; my $diff = diff \@A, \@B, { CONTEXT => 0, STYLE => $f } ; my @tests = ( sub { if ( $diff =~ /(^${f}::.*){8}/sm ) { ok 1 ; } else { ok $diff, "8 lines of output" ; } }, sub { if ( $diff =~ m{ file_header.* hunk_header.* hunk.* hunk_footer.* hunk_header.* hunk.* hunk_footer.* file_footer }sx ) { ok 1 ; } else { ok $diff, "proper ordering (see test source)" ; } }, ) ; plan tests => scalar @tests ; $_->() for @tests ; package My::Diff::Format ; use Data::Dumper ; sub _dump { my $prefix = (caller(1))[3] ; local $Data::Dumper::Indent = 0 ; local $Data::Dumper::Terse = 1 ; join( "", map { s/^/$prefix: /mg ; $_ ; } join ", ", map { my $s = ref $_ ? Dumper $_ : $_ ; $s =~ s/([\000-\026])/sprintf "\0x%02x", ord $1/ge ; $s ; } @_ ) . "\n" ; } sub file_header { &_dump } sub hunk_header { &_dump } sub hunk { &_dump } sub hunk_footer { &_dump } sub file_footer { &_dump } Text-Diff-1.45/t/general.t000644 000765 000024 00000012743 12452327560 015553 0ustar00neilbstaff000000 000000 #!/usr/bin/perl use strict; use Test; use Text::Diff; use Algorithm::Diff qw( traverse_sequences ); ## Each test specifies options to pass to "diff" when the --update option ## is present in @ARGV and options to pass to Text::Diff::diff when the ## tests are run. my @tests= ( ["-u", '' ], ["-c", 'STYLE => "Context"' ], ["-C0", 'STYLE => "Context", CONTEXT => 0' ], ["-U0", 'STYLE => "Unified", CONTEXT => 0' ], ["", 'STYLE => "OldStyle"' ], ); my @A = map "$_\n", qw( 1 2 3 4 5d 6 7 8 9 10 11 11d 12 13 ); my @B = map "$_\n", qw( 1 2 3 4 5a 6 7 8 9 9a 10 11 12 13 ); my $sep = ( "----8<----" x 7 ) . "\n"; if ( grep "--update", @ARGV ) { my $version = `diff -v`; die "Could not determine your diff's version" unless defined $version && length $version; chomp $version; die "Requires GNU's diff, not '$version'" unless $version =~ /GNU/; ## Here are the two files to feed to diff open A, ">A" or die $!; print A @A; close A; open B, ">B" or die $!; print B @B; close B; my $mtime_A = time; my $mtime_B = $mtime_A + 1; utime $mtime_A, $mtime_A, "A" or die $!; utime $mtime_B, $mtime_B, "B" or die $!; my $file_options = < "A", MTIME_A => $mtime_A, FILENAME_B => "B", MTIME_B => $mtime_B, END_OPTIONS open ME, "<$0" or die $!; my $me = join( "", ); close ME or die $!; open BAK, ">$0.bak" or die $!; print BAK $me or die $!; close BAK or die $!; my @diffs = map scalar `diff $_->[0] A B`, @tests; for ( @diffs ) { s/(Sun|Mon|Tue|Wed|Thu|Fri|Sat).*//m; s/(Sun|Mon|Tue|Wed|Thu|Fri|Sat).*//m; } $me =~ s/^(__DATA__\n).*//ms; open ME, ">$0" or die $!; print ME $me, "__DATA__\n", join $sep, "$file_options\n", @diffs or die $!; close ME or die $!; # unlink "A" or warn "$! unlinking A"; # unlink "B" or warn "$! unlinking B"; exit 0; } ## Ok, we're not updating, so run the tests... my @data = split $sep, join "", ; close DATA or die $!; die "Found " . @data, " elements, not ", ( @tests + 1 ), ", time to --update?\n" unless @data == @tests + 1; my @file_options = eval "(" . shift( @data ) . ")"; die if $@; my ( $mtime_A, $mtime_B ); { my %o = @file_options; $mtime_A = $o{MTIME_A}; $mtime_B = $o{MTIME_B}; } plan tests => scalar @tests; for ( @tests ) { my ( $diff_opts, $Diff_opts ) = @$_; my $expect = shift @data; $expect =~ s//localtime $mtime_A/e; $expect =~ s//localtime $mtime_B/e; my @Diff_opts = eval "($Diff_opts)"; die if $@; my $output = diff \@A, \@B, { @file_options, @Diff_opts }; if ( $output eq $expect ) { ok( 1 ); } else { ok( 0 ); warn "# diff options: $diff_opts\n"; warn "# my options: $Diff_opts\n"; ## Merge the outputs using A::D my @E = split /^/, $expect; my @G = split /^/, $output; my $w = length "Expected"; for ( @E, @G ) { s/\n/\\n/g; $w = length if length > $w; } my $fmt = "# %-${w}s %-2s %-${w}s\n"; printf STDERR $fmt, "Expected", " ", "Got"; print STDERR "# ", "-" x ( $w * 2 + 4 ), "\n"; my ( $E_start, $G_start ); my $print_diff = sub { my ( $E_end, $G_end ) = @_; if ( defined $E_start || defined $G_start ) { while ( $E_start < $E_end || $G_start < $G_end ) { printf STDERR ( $fmt, $E_start < $E_end ? $E[$E_start] : "", "!=", $G_start < $G_end ? $G[$G_start] : "" ); ++$E_start; ++$G_start; } $E_start = $G_start = undef; } }; my $dis = sub { $E_start = $_[0] unless defined $E_start; $G_start = $_[1] unless defined $G_start; }; traverse_sequences( \@E, \@G, { MATCH => sub { $print_diff->( @_ ); printf STDERR $fmt, $E[$_[0]], "==", $G[$_[1]]; }, DISCARD_A => $dis, DISCARD_B => $dis, } ); $print_diff->( scalar @E, scalar @G ); print STDERR "# ", "-" x ( $w * 2 + 4 ), "\n"; print STDERR "#\n"; } } __DATA__ FILENAME_A => "A", MTIME_A => 1007983243, FILENAME_B => "B", MTIME_B => 1007983244, ----8<--------8<--------8<--------8<--------8<--------8<--------8<---- --- A +++ B @@ -2,13 +2,13 @@ 2 3 4 -5d +5a 6 7 8 9 +9a 10 11 -11d 12 13 ----8<--------8<--------8<--------8<--------8<--------8<--------8<---- *** A --- B *************** *** 2,14 **** 2 3 4 ! 5d 6 7 8 9 10 11 - 11d 12 13 --- 2,14 ---- 2 3 4 ! 5a 6 7 8 9 + 9a 10 11 12 13 ----8<--------8<--------8<--------8<--------8<--------8<--------8<---- *** A --- B *************** *** 5 **** ! 5d --- 5 ---- ! 5a *************** *** 9 **** --- 10 ---- + 9a *************** *** 12 **** - 11d --- 12 ---- ----8<--------8<--------8<--------8<--------8<--------8<--------8<---- --- A +++ B @@ -5 +5 @@ -5d +5a @@ -9,0 +10 @@ +9a @@ -12 +12,0 @@ -11d ----8<--------8<--------8<--------8<--------8<--------8<--------8<---- 5c5 < 5d --- > 5a 9a10 > 9a 12d12 < 11d Text-Diff-1.45/t/inputs.t000644 000765 000024 00000002361 12452327560 015453 0ustar00neilbstaff000000 000000 #!/usr/bin/perl use strict; use Test; use Text::Diff; my @A = map "$_\n", qw( 1 2 3 4 ); my @B = map "$_\n", qw( 1 2 3 5 ); my $A = join "", @A; my $B = join "", @B; my $Af = "io_A"; my $Bf = "io_B"; open A, ">$Af" or die $!; print A @A or die $! ; close A or die $! ; open B, ">$Bf" or die $!; print B @B or die $! ; close B or die $! ; my @tests = ( sub { ok !diff \@A, \@A }, sub { my $d = diff \@A, \@B; $d =~ /-4.*\+5/s ? ok 1 : ok $d, "a valid diff"; }, sub { ok !diff \$A, \$A }, sub { my $d = diff \$A, \$B; $d =~ /-4.*\+5/s ? ok 1 : ok $d, "a valid diff"; }, sub { ok !diff $Af, $Af }, sub { my $d = diff $Af, $Bf; $d =~ /-4.*\+5/s ? ok 1 : ok $d, "a valid diff"; }, sub { open A1, "<$Af" or die $!; open A2, "<$Af" or die $!; ok !diff \*A1, \*A2; close A1; close A2; }, sub { open A, "<$Af" or die $!; open B, "<$Bf" or die $!; my $d = diff \*A, \*B; $d =~ /-4.*\+5/s ? ok 1 : ok $d, "a valid diff"; close A; close B; }, sub { ok !diff sub { \@A}, sub { \@A }; }, sub { my $d = diff sub { \@A }, sub { \@B }; $d =~ /-4.*\+5/s ? ok 1 : ok $d, "a valid diff"; }, ); plan tests => scalar @tests; $_->() for @tests; unlink "io_A" or warn $!; unlink "io_B" or warn $!; Text-Diff-1.45/t/keygen.t000644 000765 000024 00000000617 12452327560 015415 0ustar00neilbstaff000000 000000 #!/usr/bin/perl use strict ; use Test ; use Text::Diff ; my @A = map "$_\n", qw( 1 2 3_ 4_ ) ; my @B = map "$_\n", qw( 1 2_ 3 4_ ) ; my @tests = ( sub { ok !diff \@A, \@B, { KEYGEN => sub { local $_ = shift ; s/_+//g ; return $_ . shift ; }, KEYGEN_ARGS => [ "args" ], } ; }, ) ; plan tests => scalar @tests ; $_->() for @tests ; Text-Diff-1.45/t/outputs.t000644 000765 000024 00000002111 12452327560 015645 0ustar00neilbstaff000000 000000 #!/usr/bin/perl use strict; use Test; use Text::Diff; my @A = map "$_\n", qw( 1 2 3 4 ); my @B = map "$_\n", qw( 1 2 3 5 ); sub _d($) { diff \@A, \@B, { OUTPUT => shift }; } sub slurp { open SLURP, "<$_[0]" or die $!; my $string = join "", ; close SLURP; return $string; } my $expected = _d undef; my @tests = ( sub { ok $expected =~ tr/\n// }, sub { my $o; _d sub { $o .= shift }; ok $o, $expected }, sub { my @o; _d \@o; ok join( "", @o ), $expected; }, sub { open F, ">output.foo" or die $!; _d \*F; close F or die $!; ok slurp "output.foo", $expected; unlink "output.foo" or warn $!; }, sub { require IO::File; my $fh = IO::File->new( ">output.bar" ); _d $fh; $fh->close; $fh = undef; ok slurp "output.bar", $expected; unlink "output.bar" or warn $!; }, sub { ok 0 < index( diff( \"\n", \"", { STYLE => "Table" } ), "\\n" ); }, # Test for bug reported by Ilya Martynov sub { ok diff( \"", \"" ), ""; }, sub { ok diff( \"A", \"A" ), ""; }, ); plan tests => scalar @tests; $_->() for @tests; Text-Diff-1.45/t/table.t000644 000765 000024 00000001432 12452327560 015216 0ustar00neilbstaff000000 000000 #!/usr/bin/perl use strict ; use Test ; use Text::Diff ; sub t($$$$) { my ( $a, $b, $exp_a, $exp_b ) = @_; my $d = diff \$a, \$b, { STYLE => "Table" }; my $re = qr/^\*.*\|\Q$exp_a\E\s*\|\Q$exp_b\E\s*\*$/m; ## Older Test.pms don't support ok( $foo, qr// ); $d =~ $re ? ok 1 : ok "\n" . $d, "a match for " . $re; } sub slurp { open SLURP, "<" . shift or die $! ; return join "", } my @tests = ( sub { t " ", "\t", "\\s", "\\t" }, sub { t " a", "\ta", " a", "\\ta" }, sub { t "a ", "a\t", "a\\s", "a\\t" }, sub { t "\t", "\\t", "\\t", "\\\\t" }, sub { t "\ta", "\tb", " a", " b" }, sub { t "-\ta", "-\tb", "- a", "- b" }, sub { t "\\ta", "\\tb", "\\ta", "\\tb" }, ) ; plan tests => scalar @tests ; $_->() for @tests ; Text-Diff-1.45/lib/Text/000755 000765 000024 00000000000 13145110537 015163 5ustar00neilbstaff000000 000000 Text-Diff-1.45/lib/Text/Diff/000755 000765 000024 00000000000 13145110537 016033 5ustar00neilbstaff000000 000000 Text-Diff-1.45/lib/Text/Diff.pm000644 000765 000024 00000053540 13145110372 016375 0ustar00neilbstaff000000 000000 package Text::Diff; use 5.006; use strict; use warnings; use Carp qw/ croak confess /; use Exporter (); use Algorithm::Diff (); our $VERSION = '1.45'; our @ISA = qw/ Exporter /; our @EXPORT = qw/ diff /; ## Hunks are made of ops. An op is the starting index for each ## sequence and the opcode: use constant A => 0; # Array index before match/discard use constant B => 1; use constant OPCODE => 2; # "-", " ", "+" use constant FLAG => 3; # What to display if not OPCODE "!" my %internal_styles = ( Unified => undef, Context => undef, OldStyle => undef, Table => undef, ## "internal", but in another module ); sub diff { my @seqs = ( shift, shift ); my $options = shift || {}; for my $i ( 0 .. 1 ) { my $seq = $seqs[$i]; my $type = ref $seq; while ( $type eq "CODE" ) { $seqs[$i] = $seq = $seq->( $options ); $type = ref $seq; } my $AorB = !$i ? "A" : "B"; if ( $type eq "ARRAY" ) { ## This is most efficient :) $options->{"OFFSET_$AorB"} = 0 unless defined $options->{"OFFSET_$AorB"}; } elsif ( $type eq "SCALAR" ) { $seqs[$i] = [split( /^/m, $$seq )]; $options->{"OFFSET_$AorB"} = 1 unless defined $options->{"OFFSET_$AorB"}; } elsif ( ! $type ) { $options->{"OFFSET_$AorB"} = 1 unless defined $options->{"OFFSET_$AorB"}; $options->{"FILENAME_$AorB"} = $seq unless defined $options->{"FILENAME_$AorB"}; $options->{"MTIME_$AorB"} = (stat($seq))[9] unless defined $options->{"MTIME_$AorB"}; local $/ = "\n"; open F, "<$seq" or croak "$!: $seq"; $seqs[$i] = []; close F; } elsif ( $type eq "GLOB" || UNIVERSAL::isa( $seq, "IO::Handle" ) ) { $options->{"OFFSET_$AorB"} = 1 unless defined $options->{"OFFSET_$AorB"}; local $/ = "\n"; $seqs[$i] = [<$seq>]; } else { confess "Can't handle input of type ", ref; } } ## Config vars my $output; my $output_handler = $options->{OUTPUT}; my $type = ref $output_handler ; if ( ! defined $output_handler ) { $output = ""; $output_handler = sub { $output .= shift }; } elsif ( $type eq "CODE" ) { ## No problems, mate. } elsif ( $type eq "SCALAR" ) { my $out_ref = $output_handler; $output_handler = sub { $$out_ref .= shift }; } elsif ( $type eq "ARRAY" ) { my $out_ref = $output_handler; $output_handler = sub { push @$out_ref, shift }; } elsif ( $type eq "GLOB" || UNIVERSAL::isa $output_handler, "IO::Handle" ) { my $output_handle = $output_handler; $output_handler = sub { print $output_handle shift }; } else { croak "Unrecognized output type: $type"; } my $style = $options->{STYLE}; $style = "Unified" unless defined $options->{STYLE}; $style = "Text::Diff::$style" if exists $internal_styles{$style}; if ( ! $style->can( "hunk" ) ) { eval "require $style; 1" or die $@; } $style = $style->new if ! ref $style && $style->can( "new" ); my $ctx_lines = $options->{CONTEXT}; $ctx_lines = 3 unless defined $ctx_lines; $ctx_lines = 0 if $style->isa( "Text::Diff::OldStyle" ); my @keygen_args = $options->{KEYGEN_ARGS} ? @{$options->{KEYGEN_ARGS}} : (); ## State vars my $diffs = 0; ## Number of discards this hunk my $ctx = 0; ## Number of " " (ctx_lines) ops pushed after last diff. my @ops; ## ops (" ", +, -) in this hunk my $hunks = 0; ## Number of hunks my $emit_ops = sub { $output_handler->( $style->file_header( @seqs, $options ) ) unless $hunks++; $output_handler->( $style->hunk_header( @seqs, @_, $options ) ); $output_handler->( $style->hunk ( @seqs, @_, $options ) ); $output_handler->( $style->hunk_footer( @seqs, @_, $options ) ); }; ## We keep 2*ctx_lines so that if a diff occurs ## at 2*ctx_lines we continue to grow the hunk instead ## of emitting diffs and context as we go. We ## need to know the total length of both of the two ## subsequences so the line count can be printed in the ## header. my $dis_a = sub {push @ops, [@_[0,1],"-"]; ++$diffs ; $ctx = 0 }; my $dis_b = sub {push @ops, [@_[0,1],"+"]; ++$diffs ; $ctx = 0 }; Algorithm::Diff::traverse_sequences( @seqs, { MATCH => sub { push @ops, [@_[0,1]," "]; if ( $diffs && ++$ctx > $ctx_lines * 2 ) { $emit_ops->( [ splice @ops, 0, $#ops - $ctx_lines ] ); $ctx = $diffs = 0; } ## throw away context lines that aren't needed any more shift @ops if ! $diffs && @ops > $ctx_lines; }, DISCARD_A => $dis_a, DISCARD_B => $dis_b, }, $options->{KEYGEN}, # pass in user arguments for key gen function @keygen_args, ); if ( $diffs ) { $#ops -= $ctx - $ctx_lines if $ctx > $ctx_lines; $emit_ops->( \@ops ); } $output_handler->( $style->file_footer( @seqs, $options ) ) if $hunks; return defined $output ? $output : $hunks; } sub _header { my ( $h ) = @_; my ( $p1, $fn1, $t1, $p2, $fn2, $t2 ) = @{$h}{ "FILENAME_PREFIX_A", "FILENAME_A", "MTIME_A", "FILENAME_PREFIX_B", "FILENAME_B", "MTIME_B" }; ## remember to change Text::Diff::Table if this logic is tweaked. return "" unless defined $fn1 && defined $fn2; return join( "", $p1, " ", $fn1, defined $t1 ? "\t" . localtime $t1 : (), "\n", $p2, " ", $fn2, defined $t2 ? "\t" . localtime $t2 : (), "\n", ); } ## _range encapsulates the building of, well, ranges. Turns out there are ## a few nuances. sub _range { my ( $ops, $a_or_b, $format ) = @_; my $start = $ops->[ 0]->[$a_or_b]; my $after = $ops->[-1]->[$a_or_b]; ## The sequence indexes in the lines are from *before* the OPCODE is ## executed, so we bump the last index up unless the OP indicates ## it didn't change. ++$after unless $ops->[-1]->[OPCODE] eq ( $a_or_b == A ? "+" : "-" ); ## convert from 0..n index to 1..(n+1) line number. The unless modifier ## handles diffs with no context, where only one file is affected. In this ## case $start == $after indicates an empty range, and the $start must ## not be incremented. my $empty_range = $start == $after; ++$start unless $empty_range; return $start == $after ? $format eq "unified" && $empty_range ? "$start,0" : $start : $format eq "unified" ? "$start,".($after-$start+1) : "$start,$after"; } sub _op_to_line { my ( $seqs, $op, $a_or_b, $op_prefixes ) = @_; my $opcode = $op->[OPCODE]; return () unless defined $op_prefixes->{$opcode}; my $op_sym = defined $op->[FLAG] ? $op->[FLAG] : $opcode; $op_sym = $op_prefixes->{$op_sym}; return () unless defined $op_sym; $a_or_b = $op->[OPCODE] ne "+" ? 0 : 1 unless defined $a_or_b; my @line = ( $op_sym, $seqs->[$a_or_b][$op->[$a_or_b]] ); unless ( $line[1] =~ /(?:\n|\r\n)$/ ) { $line[1] .= "\n\\ No newline at end of file\n"; } return @line; } SCOPE: { package Text::Diff::Base; sub new { my $proto = shift; return bless { @_ }, ref $proto || $proto; } sub file_header { return "" } sub hunk_header { return "" } sub hunk { return "" } sub hunk_footer { return "" } sub file_footer { return "" } } @Text::Diff::Unified::ISA = qw( Text::Diff::Base ); sub Text::Diff::Unified::file_header { shift; ## No instance data my $options = pop ; _header( { FILENAME_PREFIX_A => "---", FILENAME_PREFIX_B => "+++", %$options } ); } sub Text::Diff::Unified::hunk_header { shift; ## No instance data pop; ## Ignore options my $ops = pop; return join( "", "@@ -", _range( $ops, A, "unified" ), " +", _range( $ops, B, "unified" ), " @@\n", ); } sub Text::Diff::Unified::hunk { shift; ## No instance data pop; ## Ignore options my $ops = pop; my $prefixes = { "+" => "+", " " => " ", "-" => "-" }; return join "", map _op_to_line( \@_, $_, undef, $prefixes ), @$ops } @Text::Diff::Context::ISA = qw( Text::Diff::Base ); sub Text::Diff::Context::file_header { _header { FILENAME_PREFIX_A=>"***", FILENAME_PREFIX_B=>"---", %{$_[-1]} }; } sub Text::Diff::Context::hunk_header { return "***************\n"; } sub Text::Diff::Context::hunk { shift; ## No instance data pop; ## Ignore options my $ops = pop; ## Leave the sequences in @_[0,1] my $a_range = _range( $ops, A, "" ); my $b_range = _range( $ops, B, "" ); ## Sigh. Gotta make sure that differences that aren't adds/deletions ## get prefixed with "!", and that the old opcodes are removed. my $after; for ( my $start = 0; $start <= $#$ops ; $start = $after ) { ## Scan until next difference $after = $start + 1; my $opcode = $ops->[$start]->[OPCODE]; next if $opcode eq " "; my $bang_it; while ( $after <= $#$ops && $ops->[$after]->[OPCODE] ne " " ) { $bang_it ||= $ops->[$after]->[OPCODE] ne $opcode; ++$after; } if ( $bang_it ) { for my $i ( $start..($after-1) ) { $ops->[$i]->[FLAG] = "!"; } } } my $b_prefixes = { "+" => "+ ", " " => " ", "-" => undef, "!" => "! " }; my $a_prefixes = { "+" => undef, " " => " ", "-" => "- ", "!" => "! " }; return join( "", "*** ", $a_range, " ****\n", map( _op_to_line( \@_, $_, A, $a_prefixes ), @$ops ), "--- ", $b_range, " ----\n", map( _op_to_line( \@_, $_, B, $b_prefixes ), @$ops ), ); } @Text::Diff::OldStyle::ISA = qw( Text::Diff::Base ); sub _op { my $ops = shift; my $op = $ops->[0]->[OPCODE]; $op = "c" if grep $_->[OPCODE] ne $op, @$ops; $op = "a" if $op eq "+"; $op = "d" if $op eq "-"; return $op; } sub Text::Diff::OldStyle::hunk_header { shift; ## No instance data pop; ## ignore options my $ops = pop; my $op = _op $ops; return join "", _range( $ops, A, "" ), $op, _range( $ops, B, "" ), "\n"; } sub Text::Diff::OldStyle::hunk { shift; ## No instance data pop; ## ignore options my $ops = pop; ## Leave the sequences in @_[0,1] my $a_prefixes = { "+" => undef, " " => undef, "-" => "< " }; my $b_prefixes = { "+" => "> ", " " => undef, "-" => undef }; my $op = _op $ops; return join( "", map( _op_to_line( \@_, $_, A, $a_prefixes ), @$ops ), $op eq "c" ? "---\n" : (), map( _op_to_line( \@_, $_, B, $b_prefixes ), @$ops ), ); } 1; __END__ =head1 NAME Text::Diff - Perform diffs on files and record sets =head1 SYNOPSIS use Text::Diff; ## Mix and match filenames, strings, file handles, producer subs, ## or arrays of records; returns diff in a string. ## WARNING: can return B diffs for large files. my $diff = diff "file1.txt", "file2.txt", { STYLE => "Context" }; my $diff = diff \$string1, \$string2, \%options; my $diff = diff \*FH1, \*FH2; my $diff = diff \&reader1, \&reader2; my $diff = diff \@records1, \@records2; ## May also mix input types: my $diff = diff \@records1, "file_B.txt"; =head1 DESCRIPTION C provides a basic set of services akin to the GNU C utility. It is not anywhere near as feature complete as GNU C, but it is better integrated with Perl and available on all platforms. It is often faster than shelling out to a system's C executable for small files, and generally slower on larger files. Relies on L for, well, the algorithm. This may not produce the same exact diff as a system's local C executable, but it will be a valid diff and comprehensible by C. We haven't seen any differences between L's logic and GNU C's, but we have not examined them to make sure they are indeed identical. B: If you don't want to import the C function, do one of the following: use Text::Diff (); require Text::Diff; That's a pretty rare occurrence, so C is exported by default. If you pass a filename, but the file can't be read, then C will C. =head1 OPTIONS C takes two parameters from which to draw input and a set of options to control its output. The options are: =over =item FILENAME_A, MTIME_A, FILENAME_B, MTIME_B The name of the file and the modification time "files". These are filled in automatically for each file when C is passed a filename, unless a defined value is passed in. If a filename is not passed in and FILENAME_A and FILENAME_B are not provided or are C, the header will not be printed. Unused on C diffs. =item OFFSET_A, OFFSET_B The index of the first line / element. These default to 1 for all parameter types except ARRAY references, for which the default is 0. This is because ARRAY references are presumed to be data structures, while the others are line-oriented text. =item STYLE "Unified", "Context", "OldStyle", or an object or class reference for a class providing C, C, C, C and C methods. The two footer() methods are provided for overloading only; none of the formats provide them. Defaults to "Unified" (unlike standard C, but Unified is what's most often used in submitting patches and is the most human readable of the three. If the package indicated by the STYLE has no C method, C will load it automatically (lazy loading). Since all such packages should inherit from C, this should be marvy. Styles may be specified as class names (C