| 1 | package File::MultipleDiff; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 8 | 2 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 9 | 2 | our @EXPORT = qw(multiple_file_diff); | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Carp ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Algorithm::Diff "sdiff"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Tie::File ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use Term::ANSIColor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Cwd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | ##---------------------------------------------------------------------------## | ||||||||||||||||||||||||||||
| 20 | ## Compares 2 files and finds out amount of differences between them ## | ||||||||||||||||||||||||||||
| 21 | ##---------------------------------------------------------------------------## | ||||||||||||||||||||||||||||
| 22 | sub _CompareFiles | ||||||||||||||||||||||||||||
| 23 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_CompareFiles: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 24 | 0 | my ($file_A, $file_B) = @_ ; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | tie my @A, 'Tie::File', "$file_A" or die "Can't tie $file_A: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | 0 | ▶ | tie my @B, 'Tie::File', "$file_B" or die "Can't tie $file_B: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # printf "CompareFiles: %-12s %-12s : %3d %3d\n", $file_A, $file_B, ($#A + 1), ($#B + 1) ; | ||||||||||||||||||||||||||||
| 30 | 0 | my @sdiffs = sdiff( \@A, \@B ); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | my $diff_counter = 0; | |||||||||||||||||||||||||||
| 33 | 0 | my $aref ; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | for $aref (@sdiffs) | |||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($aref->[0] ne 'u') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | # printf "%-2s | %-40s | %-40s | %3d\n", $aref->[0], $aref->[1], $aref->[2], $diff_counter ; | ||||||||||||||||||||||||||||
| 40 | 0 | $diff_counter ++ ; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | 0 | untie @A; | |||||||||||||||||||||||||||
| 44 | 0 | untie @B; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | return $diff_counter ; | |||||||||||||||||||||||||||
| 47 | } # end of "sub _CompareFiles" | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | ##---------------------------------------------------------------------------## | ||||||||||||||||||||||||||||
| 51 | sub multiple_file_diff | ||||||||||||||||||||||||||||
| 52 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
multiple_file_diff: not called CC=35 SCAR=71.4
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 0 | my ($directory, $file_pattern, $colour, $digits) = @_; | |||||||||||||||||||||||||||
| 54 | 0 | my $File_A; | |||||||||||||||||||||||||||
| 55 | my $File_B; | ||||||||||||||||||||||||||||
| 56 | 0 | my $file ; | |||||||||||||||||||||||||||
| 57 | 0 | my %file ; | |||||||||||||||||||||||||||
| 58 | 0 | my $count ; | |||||||||||||||||||||||||||
| 59 | 0 | my $title ; | |||||||||||||||||||||||||||
| 60 | 0 | my $title_length ; | |||||||||||||||||||||||||||
| 61 | 0 | my $field ; | |||||||||||||||||||||||||||
| 62 | 0 | my $header ; | |||||||||||||||||||||||||||
| 63 | 0 | my $name_length ; | |||||||||||||||||||||||||||
| 64 | 0 | my $max_length =0; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | my $bold_red = qq(<b><font color="#FF0000">) ; | |||||||||||||||||||||||||||
| 67 | 0 | my $blue = qq(<font color="#0000FF">) ; | |||||||||||||||||||||||||||
| 68 | 0 | my $bold_blue = qq(<b><font color="#0000FF">) ; | |||||||||||||||||||||||||||
| 69 | 0 | my $bold_green = qq(<b><font color="#00FF00">) ; | |||||||||||||||||||||||||||
| 70 | 0 | my $color_over = "</font>" ; | |||||||||||||||||||||||||||
| 71 | 0 | my $bold_color_over = "</font></b>" ; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | unless ((defined $file_pattern) && $file_pattern) { $file_pattern = '.'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $file_pattern and $file_pattern
MC/DC0%0/2
defined $file_pattern and $file_pattern
defined $file_pattern
$file_pattern
Truth table0%0/3
A and B
A= defined $file_patternB= $file_pattern
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | unless ((defined $colour) && $colour) { $colour = 'b'; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $colour and $colour
MC/DC0%0/2
defined $colour and $colour
defined $colour
$colour
Truth table0%0/3
A and B
A= defined $colourB= $colour
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if ($colour !~ /^[bc]$/) { croak "ERROR: colour parameter must be either 'c' or 'b' or empty"; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ($colour eq 'b') { $colour = ''; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | unless ((defined $digits) && $digits) { $digits = 2; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $digits and $digits
MC/DC0%0/2
defined $digits and $digits
defined $digits
$digits
Truth table0%0/3
A and B
A= defined $digitsB= $digits
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | my $start_dir = cwd; | |||||||||||||||||||||||||||
| 82 | 0 | chdir $directory ; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | opendir (my $dh, '.') || croak "ERROR: can't opendir $directory: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my @files = grep { /$file_pattern/ && -f "./$_" } readdir($dh); | ||||||||||||||||||||||||||
|
Condition0%0/3
/$file_pattern/ && -f "./$_"
MC/DC0%0/2
/$file_pattern/ && -f "./$_"
/$file_pattern/
-f "./$_"
Truth table0%0/3
A && B
A= /$file_pattern/B= -f "./$_"
| |||||||||||||||||||||||||||||
| 85 | ## foreach $file (<*>) { $file{ $file } = 0 ; } | ||||||||||||||||||||||||||||
| 86 | 0 | closedir $dh; | |||||||||||||||||||||||||||
| 87 | 0 | for (@files) | |||||||||||||||||||||||||||
| 88 | { | ||||||||||||||||||||||||||||
| 89 | 0 | $file{$_} = 0; | |||||||||||||||||||||||||||
| 90 | 0 | $name_length = length ; | |||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ($name_length > $max_length) { $max_length = $name_length; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 95 | 0 | my %count ; # HoH | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 0 | foreach $File_A (sort keys %file) | |||||||||||||||||||||||||||
| 98 | { | ||||||||||||||||||||||||||||
| 99 | 0 | foreach $File_B (sort keys %file) | |||||||||||||||||||||||||||
| 100 | { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ($File_B ge $File_A) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | 0 | $count{$File_A}{$File_B} = &_CompareFiles ($File_A, $File_B) ; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if (($count{$File_A}{$File_B} > 9) && ($digits <= 2)) { $digits = 3; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$count{$File_A}{$File_B} > 9 and $digits <= 2
MC/DC0%0/2
$count{$File_A}{$File_B} > 9 and $digits <= 2
$count{$File_A}{$File_B} > 9
$digits <= 2
Truth table0%0/3
A and B
A= $count{$File_A}{$File_B} > 9B= $digits <= 2
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if (($count{$File_A}{$File_B} > 99) && ($digits <= 3)) { $digits = 4; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$count{$File_A}{$File_B} > 99 and $digits <= 3
MC/DC0%0/2
$count{$File_A}{$File_B} > 99 and $digits <= 3
$count{$File_A}{$File_B} > 99
$digits <= 3
Truth table0%0/3
A and B
A= $count{$File_A}{$File_B} > 99B= $digits <= 3
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if (($count{$File_A}{$File_B} > 999) && ($digits <= 4)) { $digits = 5; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$count{$File_A}{$File_B} > 999 and $digits <= 4
MC/DC0%0/2
$count{$File_A}{$File_B} > 999 and $digits <= 4
$count{$File_A}{$File_B} > 999
$digits <= 4
Truth table0%0/3
A and B
A= $count{$File_A}{$File_B} > 999B= $digits <= 4
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | #------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my %HoA ; | |||||||||||||||||||||||||||
| 113 | 0 | foreach (sort keys %file) { $HoA{ $_ } = [ split (//) ] ; } | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $cnt = 0; | |||||||||||||||||||||||||||
| 116 | 0 | ▶ | while ($cnt < $max_length) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||
| 118 | 0 | $title .= " " x $max_length . " |" ; | |||||||||||||||||||||||||||
| 119 | 0 | foreach (sort keys %file) | |||||||||||||||||||||||||||
| 120 | { | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if ( exists $HoA{ $_ }[$cnt] ) { $title .= sprintf " %${digits}s ", $HoA{ $_ }[$cnt] ; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 0 | else { $title .= sprintf " %${digits}s ", " " ; } | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | unless ($cnt) { $title_length = length $title ; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $title .= "\n" ; | |||||||||||||||||||||||||||
| 126 | 0 | $cnt ++; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 0 | $header = sprintf "%s\n", "-" x $title_length ; | |||||||||||||||||||||||||||
| 130 | 0 | $header .= sprintf "%s", $title ; | |||||||||||||||||||||||||||
| 131 | 0 | $header .= sprintf "%s", "-" x $title_length ; | |||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ($colour) { print colored ['bold blue'], "$header\n" ; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 0 | else { print "$header\n" ; } | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | chdir $start_dir; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | #----------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 138 | 0 | foreach $File_A (sort keys %file) | |||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | 0 | $field = sprintf "%-${max_length}s | ", $File_A ; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($colour) { print colored ['bold blue'], $field ; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | else { print $field ; } | |||||||||||||||||||||||||||
| 143 | 0 | foreach $File_B (sort keys %file) | |||||||||||||||||||||||||||
| 144 | { | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | if ($File_B ge $File_A) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||
| 147 | 0 | $count = $count{$File_A}{$File_B} ; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | $field = sprintf "%${digits}d ", $count ; | |||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($count) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | { | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | if ($colour) { print colored ['bold red'], $field; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | else { print $field; } | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | else | ||||||||||||||||||||||||||||
| 157 | { | ||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if ($colour) { print colored ['bold green'], $field; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 0 | else { print $field ; } | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | else | ||||||||||||||||||||||||||||
| 163 | { | ||||||||||||||||||||||||||||
| 164 | 0 | printf "%${digits}s ", "-" ; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 0 | print "\n"; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } # end of "sub multiple_file_diff" | ||||||||||||||||||||||||||||
| 170 | 2 | 1; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | __END__ | ||||||||||||||||||||||||||||