title: Perl script to compare file contents of dirA with file contents of dirB and output the difference in a separate file
body: <p>I am just a beginner at perl and I need help with the following.</p>
  
  <p>DirA and DirB have files f1 ,f2 ,f3 ,f4, f5 (not necessarily 5 in number). I need to compare the contents of f1 in DirA with contents of f1 in DirB and output the result in f1_diff. Similarly need to do this for all files in directories A and B. So in the above case assuming the contents of all files are different, Script will output 5 files f1_diff,f2_diff,f3_diff,f4_diff,f5_diff which has the missing lines of both f1 DirA and f1 DirB. </p>
  
  <p>For example, if </p>
  
  <pre><code>f1 dir A has  line1, line2,     line3, line4xxxx, line5   
  f1 dir B has  line1, line2xxxx, line3, line4,     line5 
  </code></pre>
  
  <p>f1_diff should have </p>
  
  <pre><code>line2 --&gt; line2xxxx
  
  line4xxxx --&gt;line4    
  </code></pre>
  
  <p>Can someone please help me with this.</p>
  
  <p><strong>Update:</strong>
  I have the below script right now and need to add the following.
  Filename:   # of different lines
  File 1 – 1 
  File 2  - 30
  File 3 – missing in dir1
  File 3a – missing in dir2</p>
  
  <p>Secondly the number of lines differing. Can someone help me modify the same</p>
  
  <pre><code>#!/usr/bin/perl
  
  package Main; { 
  use strict;
  use warnings;
  use Cwd;
  my $DirA;
  my $DirB;
  my $y;
  
  print ("\nChoose one of the entries below\n");
  print ("e1\e2\e3\n\n");
  
  print("Enter here ---&gt;");
  my $dir = &lt;&gt;;  
  chomp($dir);    
  
  if ($dir eq "e1"){
  
  $DirA = "./dir1";
  $DirB = "./dir2";
  }
  
  elsif ($dir eq "e2"){
  $DirA = "./dir3";
  $DirB = "./dir4";
  }
  
  else{
  $DirA = "./dir5";
  $DirB = "./dir6";
  }
  
  opendir my($dh), "$DirA" or die "Couldn't open dir DirA!";
  my @files = readdir $dh;
  closedir $dh;
  system("rm -rf diffs");
  system ("mkdir diffs");
  
  foreach my $file (@files) {
      next if -d $file;
      my $diff = `diff -y --suppress-common-lines "$DirA/$file" "$DirB/$file"`;
      open DIFF_FILE, "&gt;$file"."_diff";
      print DIFF_FILE $diff;
      close DIFF_FILE;#}
  }
      chdir("./diffs/");
      my $cwd = cwd();
      system("mv ../*_diff .");
      foreach(glob('*.*')) {
      unlink if (-f $_) &amp;&amp; !-s _;
  }
   print("Total number of differences = "); system("ls *_diff | wc -l");print("\n");
   }
  </code></pre>