Algorithm-Diff

 view release on metacpan or  search on metacpan

diffnew.pl  view on Meta::CPAN

#!/usr/bin/perl
#
# `Diff' program in Perl
# Copyright 1998 M-J. Dominus. (mjd-perl-diff@plover.com)
#
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# Altered to output in `context diff' format (but without context)
# September 1998 Christian Murphy (cpm@muc.de)
#
# Context lines feature added
# Unified, "Old" (Standard UNIX), Ed diff added September 1998
# Reverse_Ed (-f option) added March 1999
# Amir D. Karger (karger@bead.aecom.yu.edu)
#
# Modular functions integrated into program
# February 1999 M-J. Dominus (mjd-perl-diff@plover.com)
#
# In this file, "item" usually means "line of text", and "item number" usually
# means "line number". But theoretically the code could be used more generally
use strict;
use Algorithm::Diff qw(diff);

# GLOBAL VARIABLES  ####
# After we've read up to a certain point in each file, the number of items
# we've read from each file will differ by $FLD (could be 0)
my $File_Length_Difference = 0;

#ed diff outputs hunks *backwards*, so we need to save hunks when doing ed diff
my @Ed_Hunks = ();
########################

my $usage = << "ENDUSAGE";
Usage: $0 [{-c | -C lines -e | -f | -u | -U lines |-q | -i | -w}] oldfile newfile
    -c do a context diff with 3 lines of context
    -C do a context diff with 'lines' lines of context (implies -c)
    -e create a script for the ed editor to change oldfile to newfile
    -f like -e but in reverse order
    -u do a unified diff with 3 lines of context
    -U do a unified diff with 'lines' lines of context (implies -u)
    -q report only whether or not the files differ
    -i ignore differences in Upper/lower-case
    -w ignore differences in white-space (space and TAB characters)

By default it will do an "old-style" diff, with output like UNIX diff
ENDUSAGE

my $Context_Lines = 0; # lines of context to print. 0 for old-style diff
my $Diff_Type = "OLD"; # by default, do standard UNIX diff
my ($opt_c, $opt_u, $opt_e, $opt_f, $opt_q, $opt_i, $opt_w);
my $compareRoutineRef = undef;
while ($ARGV[0] =~ /^-/) {
  my $opt = shift;
  last if $opt eq '--';
  if ($opt =~ /^-C(.*)/) {
    $Context_Lines = $1 || shift;
    $opt_c = 1;
    $Diff_Type = "CONTEXT";
  } elsif ($opt =~ /^-c$/) {
    $Context_Lines = 3;
    $opt_c = 1;
    $Diff_Type = "CONTEXT";
  } elsif ($opt =~ /^-e$/) {
    $opt_e = 1;
    $Diff_Type = "ED";
  } elsif ($opt =~ /^-f$/) {
    $opt_f = 1;
    $Diff_Type = "REVERSE_ED";
  } elsif ($opt =~ /^-U(.*)$/) {
    $Context_Lines = $1 || shift;
    $opt_u = 1;
    $Diff_Type = "UNIFIED";
  } elsif ($opt =~ /^-u$/) {
    $Context_Lines = 3;
    $opt_u = 1;
    $Diff_Type = "UNIFIED";
  } elsif ($opt =~ /^-q$/) {
    $Context_Lines = 0;
    $opt_q = 1;
    $opt_e = 1;
    $Diff_Type = "ED";
  } elsif ($opt =~ /^-i$/) {
    $opt_i = 1;
    $compareRoutineRef = \&compareRoutine;
  } elsif ($opt =~ /^-w$/) {
    $opt_w = 1;
    $compareRoutineRef = \&compareRoutine;
  } else {
    $opt =~ s/^-//;
    bag("Illegal option -- $opt");
  }
}

if ($opt_q and grep($_,($opt_c, $opt_f, $opt_u)) > 1) {
    bag("Combining -q with other options is nonsensical");
}



( run in 0.428 second using v1.01-cache-2.11-cpan-119454b85a5 )