diffりたかったの(´・ω・`)


どうもなんちゃらPerlはあってもUnixじゃない環境らしいので...。
Text::DiffとAlgorithm::Diffって何が違うんだろ?
なんとなくAlgorithm::Diffにしてみたんだけど...使い方あってんのかなぁ?
...あっちゃこっちゃからググってコピペしてきたのはいいがあっちゃこっちゃが多すぎてどこだったか忘れた。
ごめんなさい(´・ω・`)

#!/usr/bin/perl -w
use lib qw(./lib);
use Algorithm::Diff;
use String::Diff;
use File::Compare;

if ( @ARGV != 2 ) {
  die "usage: $0 file1 file2 \n"
}

if (File::Compare::compare($ARGV[0], $ARGV[1]) == 0) {
  print "the files ($ARGV[0] , $ARGV[1]) are equal\n";
} else {
  my @file1 = openFile($ARGV[0]);
  my @file2 = openFile($ARGV[1]);

  printf "%-45s \t \t %-45s\n", $ARGV[0], $ARGV[1];
  my @sdiffs = Algorithm::Diff::sdiff(\@file1, \@file2);

  foreach my $diff (@sdiffs) {

    my $op = $diff->[0];
    my $dat1 = $diff->[1];
    my $dat2 = $diff->[2];
    chomp $dat1;
    chomp $dat2;

    if ($op eq "u") {
      printf "%-45s \t \t %-45s\n", $dat1,  $dat2;
    } elsif ($op eq "+") {
      printf "%-45s \t>\t %-45s\n", $dat1,  $dat2;
    } elsif ($op eq "-") {
      printf "%-45s \t<\t %-45s\n", $dat1,  $dat2;
    } else {
      my $linediff = String::Diff::diff($dat1, $dat2,
                                        remove_open => '<',
                                        remove_close => '>',
                                        append_open => '[',
                                        append_close => ']',
                                       );
      printf "%-45s \t|\t %-45s\n", $linediff->[0], $linediff->[1];
    }
  }
}

sub openFile {
  my $fname = shift;
  open(fileIN,"$fname") || die('FILE OPEN ERROR!');
  my @file = <fileIN>;
  close(fileIN);
  return @file;
}
[air:~/diff] youichi% cat -n original
     1	1  This part of the
     2	2  document has stayed the
     3	3  same from version to
     4	4  version.  It shouldn't
     5	5  be shown if it doesn't
     6	6  change.  Otherwise, that
     7	7  would not be helping to
     8	8  compress the size of the
     9	9  changes.
    10	10 
    11	11 This paragraph contains
    12	12 text that is outdated.
    13	13 It will be deleted in the
    14	14 near future.
    15	15 
    16	16 It is important to spell
    17	17 check this dokument. On
    18	18 the other hand, a
    19	19 misspelled word isn't
    20	20 the end of the world.
    21	21 Nothing in the rest of
    22	22 this paragraph needs to
    23	23 be changed. Things can
    24	24 be added after it.
[air:~/diff] youichi% cat -n new 
     1	1  This is an important
     2	2  notice! It should
     3	3  therefore be located at
     4	4  the beginning of this
     5	5  document!
     6	6  
     7	7  This part of the
     8	8  document has stayed the
     9	9  same from version to
    10	10 version.  It shouldn't
    11	11 be shown if it doesn't
    12	12 change.  Otherwise, that
    13	13 would not be helping to
    14	14 compress anything.
    15	15 
    16	16 It is important to spell
    17	17 check this document. On
    18	18 the other hand, a
    19	19 misspelled word isn't
    20	21 Nothing in the rest of
    21	22 this paragraph needs to
    22	23 be changed. Things can
    23	24 be added after it.
    24	25 
    25	26 This paragraph contains
    26	27 important new additions
    27	28 to this document.
[air:~/diff] youichi% ./diff.pl original new
original                                      	 	 new                                          
1  This <p>a<rt> o<f >t<he>                   	|	 1  This [is ]a[n] [imp]o[r]t[ant]            
2  <d>oc<um>e<nt> <has s>t<ayed> <t>h<e>      	|	 2  [n]o[ti]ce[!] [I]t [s]h[ould]             
3  <sam>e< f>ro<m> <v>e<rsi>o<n> t<o>         	|	 3  [th]er[ef]o[re] [b]e[ l]o[cated] [a]t     
4  <v>e<rs>i<o>n<.>  <I>t< s>h<ouldn't>       	|	 4  [th]e[ beg]in[ning] [of] th[is]           
5  <be shown if it >doe<s>n<'>t               	|	 5  do[cum]ent[!]                             
6  <change.  Otherwise, that>                 	|	 6                                            
7  <would> <no>t <be> he<lping to>            	|	 7  [This] [par]t [of] [t]he                  
8  <c>om<pr>e<ss >th<e> s<iz>e< of> the       	|	 8  [d]o[cu]me[n]t[ ]h[as] s[tay]e[d] the     
9  <ch>a<ng>es<.>                             	|	 9  [s]a[m]e[ from ver]s[ion to]              
10                                            	|	 10 [version.  It shouldn't]                  
11 <T>hi<s> <paragraph> <c>ont<ains>          	|	 11 [be s]h[own ]i[f] [it] [d]o[es]n[']t      
12 <t>e<xt> th<at >is <ou>t<d>at<ed.>         	|	 12 [chang]e[.] [ O]th[erw]is[e,] t[h]at      
13 <It >w<i>l<l> be <d>el<eted >in t<he>      	|	 13 w[ou]l[d] [not ]be [h]el[p]in[g] t[o]     
14 <nea>r <fu>t<ure>.                         	|	 14 [comp]r[ess] [any]t[hing].                
15                                            	 	 15                                           
16 It is important to spell                   	 	 16 It is important to spell                  
17 check this do<k>ument. On                  	|	 17 check this do[c]ument. On                 
18 the other hand, a                          	 	 18 the other hand, a                         
19 misspelled word isn't                      	 	 19 misspelled word isn't                     
20 the end of the world.                      	<	                                              
21 Nothing in the rest of                     	 	 21 Nothing in the rest of                    
22 this paragraph needs to                    	 	 22 this paragraph needs to                   
23 be changed. Things can                     	 	 23 be changed. Things can                    
24 be added after it.                         	 	 24 be added after it.                        
                                              	>	 25                                           
                                              	>	 26 This paragraph contains                   
                                              	>	 27 important new additions                   
                                              	>	 28 to this document.